konduto/types/address/
zip_code.rs1use crate::impl_nutype_error_conversion;
2use nutype::nutype;
3
4#[nutype(
29 sanitize(trim),
30 validate(not_empty, len_char_max = 100),
31 derive(
32 Debug,
33 Clone,
34 PartialEq,
35 Eq,
36 Display,
37 AsRef,
38 Deref,
39 Serialize,
40 Deserialize,
41 )
42)]
43pub struct ZipCode(String);
44
45impl ZipCode {
46 pub fn as_str(&self) -> &str {
48 self.as_ref()
49 }
50}
51
52impl_nutype_error_conversion!(ZipCodeError);
53
54#[cfg(test)]
55mod tests {
56 use super::*;
57
58 #[test]
59 fn test_valid_zip_br() {
60 let zip = ZipCode::try_new("01234-567").unwrap();
61 assert_eq!(zip.as_str(), "01234-567");
62 }
63
64 #[test]
65 fn test_valid_zip_us() {
66 let zip = ZipCode::try_new("12345-6789").unwrap();
67 assert_eq!(zip.as_str(), "12345-6789");
68 }
69
70 #[test]
71 fn test_zip_with_whitespace() {
72 let zip = ZipCode::try_new(" 90210 ").unwrap();
73 assert_eq!(zip.as_str(), "90210");
74 }
75
76 #[test]
77 fn test_empty_zip() {
78 let result = ZipCode::try_new("");
79 assert!(result.is_err());
80 }
81
82 #[test]
83 fn test_zip_too_long() {
84 let long_zip = "a".repeat(101);
85 let result = ZipCode::try_new(long_zip);
86 assert!(result.is_err());
87 }
88
89 #[test]
90 fn test_zip_max_length() {
91 let zip = "a".repeat(100);
92 let result = ZipCode::try_new(zip);
93 assert!(result.is_ok());
94 }
95}