Skip to content

Commit

Permalink
refactor: use fromstr trait
Browse files Browse the repository at this point in the history
Signed-off-by: hi-rustin <[email protected]>
  • Loading branch information
Rustin170506 committed Feb 7, 2024
1 parent 43f8608 commit edda420
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions tokio-console/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ pub(crate) enum KnownWarnings {
NeverYielded,
}

impl From<&str> for KnownWarnings {
fn from(s: &str) -> Self {
impl FromStr for KnownWarnings {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"self-wakes" => KnownWarnings::SelfWakes,
"lost-waker" => KnownWarnings::LostWaker,
"never-yielded" => KnownWarnings::NeverYielded,
_ => panic!("unknown warning: {}", s),
"self-wakes" => Ok(KnownWarnings::SelfWakes),
"lost-waker" => Ok(KnownWarnings::LostWaker),
"never-yielded" => Ok(KnownWarnings::NeverYielded),
_ => Err(format!("unknown warning: {}", s)),
}
}
}
Expand Down Expand Up @@ -188,16 +190,19 @@ pub(crate) enum AllowedWarnings {
Some(BTreeSet<KnownWarnings>),
}

impl From<&str> for AllowedWarnings {
fn from(s: &str) -> Self {
impl FromStr for AllowedWarnings {
type Err = String;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"all" => AllowedWarnings::All,
"all" => Ok(AllowedWarnings::All),
_ => {
let warnings = s
.split(',')
.map(KnownWarnings::from)
.collect::<BTreeSet<_>>();
AllowedWarnings::Some(warnings)
.map(|s| s.parse::<KnownWarnings>())
.collect::<Result<BTreeSet<_>, _>>()
.map_err(|err| format!("failed to parse warning: {}", err))?;
Ok(AllowedWarnings::Some(warnings))
}
}
}
Expand Down

0 comments on commit edda420

Please sign in to comment.