Skip to content

Commit

Permalink
impl TryFrom<String> for Name
Browse files Browse the repository at this point in the history
Allows for re-using the allocated memory if you already have the owned type
  • Loading branch information
RickyDaMa committed May 21, 2024
1 parent e8ea936 commit 6303c5e
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,18 @@ impl FromStr for Name {
}
}

impl TryFrom<String> for Name {
type Error = NamingError;

fn try_from(value: String) -> Result<Self, Self::Error> {
if is_valid(&value) {
Ok(Name(value.into()))
} else {
Err(NamingError::Invalid(value))
}
}
}

impl<'de> Deserialize<'de> for Name {
fn deserialize<D>(deserializer: D) -> Result<Name, D::Error>
where
Expand Down

0 comments on commit 6303c5e

Please sign in to comment.