trailers: add Token constructor

During the reorganisation of the codebase, the constructors for Token
were removed.

Add back a constructor via the `TryFrom<&'a str>` trait.

Signed-off-by: Fintan Halpenny <fintan.halpenny@gmail.com>
X-Clacks-Overhead: GNU Terry Pratchett
This commit is contained in:
Fintan Halpenny
2023-05-04 11:54:21 +01:00
parent 15014923c7
commit 956bf070d4
+13
View File
@@ -130,6 +130,19 @@ impl Deref for Token<'_> {
}
}
impl<'a> TryFrom<&'a str> for Token<'a> {
type Error = &'static str;
fn try_from(s: &'a str) -> Result<Self, Self::Error> {
let is_token = s.chars().all(|c| c.is_alphanumeric() || c == '-');
if is_token {
Ok(Token(s))
} else {
Err("token contains invalid characters")
}
}
}
pub struct Display<'a> {
trailer: &'a Trailer<'a>,
separator: &'a str,