Skip to content

Commit 4293dac

Browse files
feat: use new error variants
1 parent fc7e5b2 commit 4293dac

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/lib.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ enum ErrorCause {
2222
ProhibitedCharacter(char),
2323
/// Violates stringprep rules for bidirectional text.
2424
ProhibitedBidirectionalText,
25+
/// Starts with a combining character
26+
StartsWithCombiningCharacter,
27+
/// Empty String
28+
EmptyString,
2529
}
2630

2731
/// An error performing the stringprep algorithm.
@@ -33,6 +37,8 @@ impl fmt::Display for Error {
3337
match self.0 {
3438
ErrorCause::ProhibitedCharacter(c) => write!(fmt, "prohibited character `{}`", c),
3539
ErrorCause::ProhibitedBidirectionalText => write!(fmt, "prohibited bidirectional text"),
40+
ErrorCause::StartsWithCombiningCharacter => write!(fmt, "starts with combining character"),
41+
ErrorCause::EmptyString => write!(fmt, "empty string"),
3642
}
3743
}
3844
}
@@ -323,6 +329,9 @@ fn x520_mapped_to_space(c: char) -> bool {
323329
/// spaces as described in Section 7.6, because the characters needing removal
324330
/// will vary across the matching rules and ASN.1 syntaxes used.
325331
pub fn x520prep(s: &str, case_fold: bool) -> Result<Cow<'_, str>, Error> {
332+
if s.len() == 0 {
333+
return Err(Error(ErrorCause::EmptyString));
334+
}
326335
if s.chars().all(|c| matches!(c, ' '..='~') && (!case_fold || c.is_ascii_lowercase())) {
327336
return Ok(Cow::Borrowed(s));
328337
}
@@ -359,10 +368,10 @@ pub fn x520prep(s: &str, case_fold: bool) -> Result<Cow<'_, str>, Error> {
359368
let first_char = s.chars().next();
360369
if let Some(c) = first_char {
361370
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));
365372
}
373+
} else {
374+
return Err(Error(ErrorCause::EmptyString));
366375
}
367376

368377
// 5. Check bidi

0 commit comments

Comments
 (0)