support numbers (#31)

This commit is contained in:
Antonio Schiavon 2022-09-07 12:46:32 -03:00 committed by GitHub
parent 0f776b8e6d
commit 8c69bf577c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 1 deletions

View File

@ -198,7 +198,12 @@ struct Ascii<'a>(&'a [u8]);
impl<'a> Ascii<'a> {
fn new(s: &'a str) -> Result<Self, InvalidCharacter> {
let bytes = s.as_bytes();
match bytes.iter().all(|b| b.is_ascii_lowercase()) {
let valid = bytes
.iter()
.all(|b| b.is_ascii_lowercase() || b.is_ascii_digit());
match valid {
true => Ok(Self(bytes)),
false => Err(InvalidCharacter),
}
@ -244,5 +249,10 @@ pub mod tests {
Ascii::new("Can't buy me love!").unwrap_err();
let text = Ascii::new("cantbuymelove").unwrap();
assert_eq!(&text[0..text.len()], "cantbuymelove");
let text_with_numbers = Ascii::new("c4ntbuym3l0v3").unwrap();
assert_eq!(
&text_with_numbers[0..text_with_numbers.len()],
"c4ntbuym3l0v3"
);
}
}