From 6303c5e845d536a9b98c2fba5126c6a90d8e7216 Mon Sep 17 00:00:00 2001 From: Ricky Atkins Date: Tue, 21 May 2024 11:21:10 +0100 Subject: [PATCH] impl TryFrom for Name Allows for re-using the allocated memory if you already have the owned type --- src/name.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/name.rs b/src/name.rs index 8a114766..688d4d8e 100644 --- a/src/name.rs +++ b/src/name.rs @@ -101,6 +101,18 @@ impl FromStr for Name { } } +impl TryFrom for Name { + type Error = NamingError; + + fn try_from(value: String) -> Result { + if is_valid(&value) { + Ok(Name(value.into())) + } else { + Err(NamingError::Invalid(value)) + } + } +} + impl<'de> Deserialize<'de> for Name { fn deserialize(deserializer: D) -> Result where