Skip to content

Commit

Permalink
Reuse key helpers in serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
apasel422 committed Feb 20, 2025
1 parent 2575987 commit 3fef363
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
15 changes: 7 additions & 8 deletions src/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,21 @@ impl Serializer {
pub(crate) fn serialize_key(input_key: &str, output: &mut String) -> SFVResult<()> {
// https://httpwg.org/specs/rfc8941.html#ser-key

match input_key.chars().next() {
let mut bytes = input_key.bytes();

match bytes.next() {
None => return Err(Error::new("serialize_key: key is empty")),
Some(char) => {
if !(char.is_ascii_lowercase() || char == '*') {
Some(c) => {
if !utils::is_allowed_start_key_char(c) {
return Err(Error::new(
"serialize_key: first character is not lcalpha or '*'",
));
}
}
}

let disallowed_chars =
|c: char| !(c.is_ascii_lowercase() || c.is_ascii_digit() || "_-*.".contains(c));

if input_key.chars().any(disallowed_chars) {
return Err(Error::new("serialize_key: disallowed character in input"));
if bytes.any(|c| !utils::is_allowed_inner_key_char(c)) {
return Err(Error::new("serialize_key: disallowed character"));
}

output.push_str(input_key);
Expand Down
2 changes: 1 addition & 1 deletion src/test_serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ fn serialize_key_errors() -> Result<(), Box<dyn StdError>> {
Serializer::serialize_key("", &mut buf)
);
assert_eq!(
Err(Error::new("serialize_key: disallowed character in input")),
Err(Error::new("serialize_key: disallowed character")),
Serializer::serialize_key("aND", &mut buf)
);
assert_eq!(
Expand Down

0 comments on commit 3fef363

Please sign in to comment.