From 08deccf7031b9c769a556cd9ebb0bc9cac988272 Mon Sep 17 00:00:00 2001 From: Josiah Parry Date: Thu, 11 Jan 2024 11:16:39 -0500 Subject: [PATCH] provide as_str() method to return the alphabet characters (#264) * provide as_string() method to return the alphabet characters * add docs for as_string() * put as_string() behind std feature flag * make as_string() -> as_str() and no_std compatible * use core::str over std::str --- src/alphabet.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/alphabet.rs b/src/alphabet.rs index cfbb402..56c6205 100644 --- a/src/alphabet.rs +++ b/src/alphabet.rs @@ -1,7 +1,7 @@ //! Provides [Alphabet] and constants for alphabets commonly used in the wild. use crate::PAD_BYTE; -use core::{convert, fmt}; +use core::{convert, fmt, primitive::str}; #[cfg(any(feature = "std", test))] use std::error; @@ -123,6 +123,11 @@ impl Alphabet { Ok(Self::from_str_unchecked(alphabet)) } + + /// Create a `&str` from the symbols in the `Alphabet` + pub fn as_str(&self) -> &str { + core::str::from_utf8(&self.symbols).unwrap() + } } impl convert::TryFrom<&str> for Alphabet { @@ -270,4 +275,11 @@ mod tests { .unwrap() ); } + + #[test] + fn str_same_as_input() { + let alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + let a = Alphabet::try_from(alphabet).unwrap(); + assert_eq!(alphabet, a.as_str()) + } }