File tree Expand file tree Collapse file tree 2 files changed +34
-8
lines changed Expand file tree Collapse file tree 2 files changed +34
-8
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,23 @@ impl McfHash {
60
60
Ok ( Self ( s) )
61
61
}
62
62
63
+ /// Create an [`McfHash`] from an identifier.
64
+ ///
65
+ /// # Returns
66
+ ///
67
+ /// Error if the identifier is invalid.
68
+ ///
69
+ /// Allowed characters match the regex: `[a-z0-9\-]`, where the first and last characters do NOT
70
+ /// contain a `-`.
71
+ pub fn from_id ( id : & str ) -> Result < McfHash > {
72
+ validate_id ( id) ?;
73
+
74
+ let mut hash = String :: with_capacity ( 1 + id. len ( ) ) ;
75
+ hash. push ( fields:: DELIMITER ) ;
76
+ hash. push_str ( id) ;
77
+ Ok ( Self ( hash) )
78
+ }
79
+
63
80
/// Get the contained string as a `str`.
64
81
pub fn as_str ( & self ) -> & str {
65
82
& self . 0
@@ -125,6 +142,11 @@ impl str::FromStr for McfHash {
125
142
/// Perform validations that the given string is well-formed MCF.
126
143
#[ cfg( feature = "alloc" ) ]
127
144
fn validate ( s : & str ) -> Result < ( ) > {
145
+ // Disallow trailing `$`
146
+ if s. ends_with ( fields:: DELIMITER ) {
147
+ return Err ( Error { } ) ;
148
+ }
149
+
128
150
// Validates the hash begins with a leading `$`
129
151
let mut fields = Fields :: new ( s) ?;
130
152
@@ -133,17 +155,10 @@ fn validate(s: &str) -> Result<()> {
133
155
validate_id ( id. as_str ( ) ) ?;
134
156
135
157
// Validate the remaining fields have an appropriate format
136
- let mut any = false ;
137
158
for field in fields {
138
- any = true ;
139
159
field. validate ( ) ?;
140
160
}
141
161
142
- // Must have at least one field.
143
- if !any {
144
- return Err ( Error { } ) ;
145
- }
146
-
147
162
Ok ( ( ) )
148
163
}
149
164
Original file line number Diff line number Diff line change @@ -12,11 +12,16 @@ const EXAMPLE_HASH: &[u8] = &hex!(
12
12
"0d358cad62739eb554863c183aef27e6390368fe061fc5fcb1193a392d60dcad4594fa8d383ab8fc3f0dc8088974602668422e6a58edfa1afe24831b10be69be"
13
13
) ;
14
14
15
+ #[ test]
16
+ fn from_id ( ) {
17
+ let mcf_hash = McfHash :: from_id ( "6" ) . unwrap ( ) ;
18
+ assert_eq ! ( "$6" , mcf_hash. as_str( ) ) ;
19
+ }
20
+
15
21
#[ test]
16
22
fn parse_malformed ( ) {
17
23
assert ! ( "Hello, world!" . parse:: <McfHash >( ) . is_err( ) ) ;
18
24
assert ! ( "$" . parse:: <McfHash >( ) . is_err( ) ) ;
19
- assert ! ( "$foo" . parse:: <McfHash >( ) . is_err( ) ) ;
20
25
assert ! ( "$$" . parse:: <McfHash >( ) . is_err( ) ) ;
21
26
assert ! ( "$$foo" . parse:: <McfHash >( ) . is_err( ) ) ;
22
27
assert ! ( "$foo$" . parse:: <McfHash >( ) . is_err( ) ) ;
@@ -25,6 +30,12 @@ fn parse_malformed() {
25
30
assert ! ( "$-foo$bar" . parse:: <McfHash >( ) . is_err( ) ) ;
26
31
}
27
32
33
+ #[ test]
34
+ fn parse_id_only ( ) {
35
+ let hash: McfHash = "$6" . parse ( ) . unwrap ( ) ;
36
+ assert_eq ! ( "6" , hash. id( ) ) ;
37
+ }
38
+
28
39
#[ test]
29
40
fn parse_sha512_hash ( ) {
30
41
let hash: McfHash = SHA512_HASH . parse ( ) . unwrap ( ) ;
You can’t perform that action at this time.
0 commit comments