From 0c0799994df13db091f4f06e01d32f555dbc2438 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Sun, 14 Nov 2021 01:46:22 +0100 Subject: [PATCH] pkstrings: impl TryFrom for PKString MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- pkstrings/src/lib.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkstrings/src/lib.rs b/pkstrings/src/lib.rs index ef14f2d..0d0906e 100644 --- a/pkstrings/src/lib.rs +++ b/pkstrings/src/lib.rs @@ -122,6 +122,21 @@ impl fmt::Display for PKString { } } +impl TryFrom for PKString { + type Error = Error; + + fn try_from(ord: u8) -> Result { + let mut buf = String::with_capacity(1); + + match hextostr(ord) { + Some(chr) => buf.push(chr.clone()), + None => return Err(Error::InvalidCharacter), + } + + Ok(PKString(buf)) + } +} + impl TryFrom<&[u8]> for PKString { type Error = Error;