Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide as_string() method to return the alphabet characters #264

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/alphabet.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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())
}
}