Skip to content

Commit

Permalink
feat: [#870] implement traits Dispaly and FromStr for TrackerMode
Browse files Browse the repository at this point in the history
  • Loading branch information
josecelano committed May 21, 2024
1 parent 74d8f79 commit 0c9da2f
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions packages/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
//! by the tracker server crate, but also by other crates in the Torrust
//! ecosystem.
use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;
use std::time::Duration;

use info_hash::InfoHash;
Expand Down Expand Up @@ -92,6 +94,32 @@ impl Default for TrackerMode {
}

Check warning on line 94 in packages/primitives/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/primitives/src/lib.rs#L92-L94

Added lines #L92 - L94 were not covered by tests
}

impl fmt::Display for TrackerMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let display_str = match self {
TrackerMode::Public => "public",
TrackerMode::Listed => "listed",
TrackerMode::Private => "private",
TrackerMode::PrivateListed => "private_listed",

Check warning on line 103 in packages/primitives/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/primitives/src/lib.rs#L98-L103

Added lines #L98 - L103 were not covered by tests
};
write!(f, "{display_str}")
}

Check warning on line 106 in packages/primitives/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/primitives/src/lib.rs#L105-L106

Added lines #L105 - L106 were not covered by tests
}

impl FromStr for TrackerMode {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"public" => Ok(TrackerMode::Public),
"listed" => Ok(TrackerMode::Listed),
"private" => Ok(TrackerMode::Private),
"private_listed" => Ok(TrackerMode::PrivateListed),
_ => Err(format!("Unknown tracker mode: {s}")),

Check warning on line 118 in packages/primitives/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/primitives/src/lib.rs#L112-L118

Added lines #L112 - L118 were not covered by tests
}
}

Check warning on line 120 in packages/primitives/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

packages/primitives/src/lib.rs#L120

Added line #L120 was not covered by tests
}

impl TrackerMode {
#[must_use]
pub fn is_open(&self) -> bool {
Expand Down

0 comments on commit 0c9da2f

Please sign in to comment.