From 956bf070d45538e3b68a2b39dc88c173341d2bad Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Thu, 4 May 2023 11:54:21 +0100 Subject: [PATCH] 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 X-Clacks-Overhead: GNU Terry Pratchett --- radicle-git-ext/src/commit/trailers.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/radicle-git-ext/src/commit/trailers.rs b/radicle-git-ext/src/commit/trailers.rs index 7265d81..00b4c42 100644 --- a/radicle-git-ext/src/commit/trailers.rs +++ b/radicle-git-ext/src/commit/trailers.rs @@ -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 { + 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,