Skip to content

Commit

Permalink
Also consider underscore as a valid character when making a quoting d…
Browse files Browse the repository at this point in the history
…ecision
  • Loading branch information
ancwrd1 committed Dec 28, 2024
1 parent f37ef6f commit 934d9cf
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions snxcore/src/sexpr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fn to_json_value(v: &str) -> Value {
}

fn format_value(value: &str) -> String {
if value.contains(|c: char| !c.is_alphanumeric()) {
if value.contains(|c: char| !c.is_ascii_alphanumeric() && c != '_') {
format!("(\"{}\")", value)
} else {
format!("({})", value)
Expand Down Expand Up @@ -392,14 +392,32 @@ mod tests {
key: String,
}
let data = Data {
key: "Helloworld!".to_owned(),
key: "Hello_world!".to_owned(),
};
let expr = SExpression::from(&data);

let inner = expr.get("key").unwrap().as_value().unwrap();
assert_eq!(inner, "Helloworld!");
assert_eq!(inner, "Hello_world!");

let encoded = format!("{}", expr);
assert_eq!(encoded, "(\n\t:key (\"Helloworld!\"))");
assert_eq!(encoded, "(\n\t:key (\"Hello_world!\"))");
}

#[test]
fn test_non_quoted_value() {
#[derive(Serialize)]
struct Data {
key: String,
}
let data = Data {
key: "Hello_world".to_owned(),
};
let expr = SExpression::from(&data);

let inner = expr.get("key").unwrap().as_value().unwrap();
assert_eq!(inner, "Hello_world");

let encoded = format!("{}", expr);
assert_eq!(encoded, "(\n\t:key (Hello_world))");
}
}

0 comments on commit 934d9cf

Please sign in to comment.