use regex_macro::regex; default_derive!{ pub struct SightingId { id: String } } impl TryFrom<&str> for SightingId { type Error = &'static str; fn try_from(value: &str) -> Result { let re = regex!("^[[:digit:]]+$"); if value.len() <= 10 && re.is_match(value) { Ok(SightingId { id: value.to_string() }) } else { Err("Failed to parse SightingId") } } } impl Into for SightingId { fn into(self) -> String { self.id } } #[test] fn empty() { let id: Result = "".try_into(); assert!(id.is_err()) } #[test] fn oversized() { let id: Result = "12345678910".try_into(); assert!(id.is_err()) } #[test] fn forbidden_char() { let id: Result = "1954a".try_into(); assert!(id.is_err()) } #[test] fn valid1() { let id: Result = "12345".try_into(); assert_eq!(id, Ok(SightingId { id: "12345".to_string() })) } #[test] fn valid2() { let id: Result = "0123456789".try_into(); assert_eq!(id, Ok(SightingId { id: "0123456789".to_string() })) }