Skip to content

Commit 4075c10

Browse files
authored
Fix panic at TryFrom<&str> (#29)
1 parent 668c684 commit 4075c10

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

src/tsid/conversions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::tsid::{ParseErrorReason, TsidError, REVERSE, TSID};
1+
use crate::tsid::{ParseErrorReason, TsidError, REVERSE, TSID, ALPHABET};
22

33
impl From<TSID> for u64 {
44
fn from(val: TSID) -> Self {
@@ -28,7 +28,7 @@ impl TryFrom<&str> for TSID {
2828
type Error = TsidError;
2929

3030
fn try_from(value: &str) -> Result<Self, Self::Error> {
31-
if value.len() != 13 {
31+
if value.len() != 13 || !value.chars().all(|c| ALPHABET.contains(&c)) {
3232
return Err(TsidError::ParseError(ParseErrorReason::InvalidLength));
3333
}
3434
let chars = value.as_bytes();

src/tsid/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,4 +148,10 @@ mod tests {
148148
let id1 = TSID::new(496830748901259172);
149149
println!("{}", bson::doc! {"id": id1 })
150150
}
151+
152+
#[test]
153+
fn test_regression_panic_try_from_str() {
154+
// Was panicking at src/tsid/conversions.rs: invalid key
155+
assert!(TSID::try_from("-------------").is_err());
156+
}
151157
}

0 commit comments

Comments
 (0)