Skip to content

Commit

Permalink
provide as_str() method to return the alphabet characters (#264)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
JosiahParry committed Jan 11, 2024
1 parent 1cd75a1 commit 08deccf
Showing 1 changed file with 13 additions and 1 deletion.
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())
}
}

0 comments on commit 08deccf

Please sign in to comment.