@@ -22,6 +22,10 @@ enum ErrorCause {
22
22
ProhibitedCharacter ( char ) ,
23
23
/// Violates stringprep rules for bidirectional text.
24
24
ProhibitedBidirectionalText ,
25
+ /// Starts with a combining character
26
+ StartsWithCombiningCharacter ,
27
+ /// Empty String
28
+ EmptyString ,
25
29
}
26
30
27
31
/// An error performing the stringprep algorithm.
@@ -33,6 +37,8 @@ impl fmt::Display for Error {
33
37
match self . 0 {
34
38
ErrorCause :: ProhibitedCharacter ( c) => write ! ( fmt, "prohibited character `{}`" , c) ,
35
39
ErrorCause :: ProhibitedBidirectionalText => write ! ( fmt, "prohibited bidirectional text" ) ,
40
+ ErrorCause :: StartsWithCombiningCharacter => write ! ( fmt, "starts with combining character" ) ,
41
+ ErrorCause :: EmptyString => write ! ( fmt, "empty string" ) ,
36
42
}
37
43
}
38
44
}
@@ -323,6 +329,9 @@ fn x520_mapped_to_space(c: char) -> bool {
323
329
/// spaces as described in Section 7.6, because the characters needing removal
324
330
/// will vary across the matching rules and ASN.1 syntaxes used.
325
331
pub fn x520prep ( s : & str , case_fold : bool ) -> Result < Cow < ' _ , str > , Error > {
332
+ if s. len ( ) == 0 {
333
+ return Err ( Error ( ErrorCause :: EmptyString ) ) ;
334
+ }
326
335
if s. chars ( ) . all ( |c| matches ! ( c, ' ' ..='~' ) && ( !case_fold || c. is_ascii_lowercase ( ) ) ) {
327
336
return Ok ( Cow :: Borrowed ( s) ) ;
328
337
}
@@ -359,10 +368,10 @@ pub fn x520prep(s: &str, case_fold: bool) -> Result<Cow<'_, str>, Error> {
359
368
let first_char = s. chars ( ) . next ( ) ;
360
369
if let Some ( c) = first_char {
361
370
if c. is_mark ( ) {
362
- // I do think this ought to be considered a different error, but adding
363
- // another enum variant would be a breaking change, so this is "good"
364
- return Err ( Error ( ErrorCause :: ProhibitedCharacter ( first_char. unwrap ( ) ) ) ) ;
371
+ return Err ( Error ( ErrorCause :: StartsWithCombiningCharacter ) ) ;
365
372
}
373
+ } else {
374
+ return Err ( Error ( ErrorCause :: EmptyString ) ) ;
366
375
}
367
376
368
377
// 5. Check bidi
0 commit comments