Removed custom to_string implementation with fmt::Display

Just for kicks I ran clippy through the program, and just changed it so that fmt::Display was implemented.
This commit is contained in:
William Batista 2021-05-20 12:51:06 -04:00
parent efd10aa82b
commit 04cb175b8a
No known key found for this signature in database
GPG Key ID: 5F69AE52CA83B3F6
1 changed files with 46 additions and 45 deletions

View File

@ -1,4 +1,5 @@
use regex::Regex;
use std::fmt;
#[macro_use]
extern crate lazy_static;
@ -162,51 +163,6 @@ pub struct Magnet {
}
impl Magnet {
/**
This generates a magnet url string given a Magnet struct
*/
pub fn to_string(&self) -> String {
let mut magnet_string = String::from("magnet:?");
if self.xt != String::new() {
magnet_string = format!("{}{}{}:{}", magnet_string, "xt=urn:", self.hash_type, self.xt);
}
let add_to_mag_string = |p_name: String, p_val: &String| -> String {
if p_val != &String::new() {
format!("&{}={}", p_name, p_val)
} else {
String::new()
}
};
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("dn"), &self.dn));
if self.xl != -1 {
magnet_string = format!("{}&xl={}", magnet_string, &self.xl);
}
magnet_string = {
let mut tr_string = String::new();
for tracker in &self.tr {
tr_string = format!("{}&tr={}", tr_string, tracker);
}
format!("{}{}", magnet_string, tr_string)
};
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("ws"), &self.ws));
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("xs"), &self.xs));
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("kt"), &self.kt));
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("as"), &self.acceptable_source));
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("mt"), &self.mt));
magnet_string
}
///Just calls new_no_validation, but validates the string given before running it through.
/// This is the recommended way of creating a nw Magnet struct, if you're unsure of the quality
/// of the magnet url's you input.
@ -278,6 +234,51 @@ impl Magnet {
}
}
impl fmt::Display for Magnet {
/*
This generates a magnet url string given a Magnet struct
*/
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let mut magnet_string = String::from("magnet:?");
if self.xt != String::new() {
magnet_string = format!("{}{}{}:{}", magnet_string, "xt=urn:", self.hash_type, self.xt);
}
let add_to_mag_string = |p_name: String, p_val: &String| -> String {
if p_val != &String::new() {
format!("&{}={}", p_name, p_val)
} else {
String::new()
}
};
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("dn"), &self.dn));
if self.xl != -1 {
magnet_string = format!("{}&xl={}", magnet_string, &self.xl);
}
magnet_string = {
let mut tr_string = String::new();
for tracker in &self.tr {
tr_string = format!("{}&tr={}", tr_string, tracker);
}
format!("{}{}", magnet_string, tr_string)
};
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("ws"), &self.ws));
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("xs"), &self.xs));
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("kt"), &self.kt));
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("as"), &self.acceptable_source));
magnet_string = format!("{}{}", magnet_string, add_to_mag_string(String::from("mt"), &self.mt));
write!(f, "{}", magnet_string)
}
}
#[cfg(test)]
mod tests {
use crate::Magnet;