difftreelog
fix(nixlike) string roundtrips
in: trunk
6 files changed
crates/nixlike/fuzz/.gitignorediffbeforeafterbothno changes
crates/nixlike/fuzz/Cargo.lockdiffbeforeafterbothno changes
crates/nixlike/fuzz/Cargo.tomldiffbeforeafterbothno changes
crates/nixlike/fuzz/fuzz_targets/fuzz_target_1.rsdiffbeforeafterbothno changes
crates/nixlike/src/lib.rsdiffbeforeafterboth40 rule string_char() -> &'input str40 rule string_char() -> &'input str41 = "\\\"" { "\"" }41 = "\\\"" { "\"" }42 / "\\\\" { "\\" }42 / "\\\\" { "\\" }43 / "\\n" { "\n" }44 / "\\t" { "\t" }45 / "\\r" { "\r" }46 / "''$" { "$" }43 / c:$([_]) { c }47 / c:$([_]) { c }44 rule string() -> String48 rule string() -> String45 = quiet! { "\"" v:(!"\"" c:string_char() {c})* "\"" { v.into_iter().collect() } } / expected!("<string>")49 = quiet! { "\"" v:(!"\"" c:string_char() {c})* "\"" { v.into_iter().collect() } } / expected!("<string>")49 rule indent() -> String53 rule indent() -> String50 = quiet! { s:$(['a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-']+) { s.to_owned() } } / expected!("<identifier>")54 = quiet! {55 s:$(['a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-']+) { s.to_owned() }56 / "\"" s:$(['a'..='z' | 'A'..='Z' | '0'..='9' | '_' | '-' | '.']+) "\"" { s.to_owned() }57 } / expected!("<identifier>")51 rule object() -> LinkedHashMap<String, Value>58 rule object() -> LinkedHashMap<String, Value>52 = "{" _59 = "{" _111118112#[test]119#[test]113fn test() {120fn test() {114 let v: serde_json::Value = parse_str(115 r#"116 {117 b.c = 2;118 b.d = "hello";119 c = {120 k = 123;121 p = 231;122 ll = [1 2 3 [] [[4 5 6]] ];123 };124 }125 "#,126 )127 .unwrap();128 let s: String = serialize(v).unwrap();121 assert_eq!(serialize("Hello\nworld").unwrap(), "\"Hello\\nworld\"");129 println!("{}", s);130}122}131123crates/nixlike/src/to_string.rsdiffbeforeafterboth5};5};667fn write_nix_obj_key_buf(k: &str, v: &Value, out: &mut PrintItems) {7fn write_nix_obj_key_buf(k: &str, v: &Value, out: &mut PrintItems) {8 if k.contains(".") {9 out.push_str("\"");8 out.push_str(k);10 out.push_str(k);11 out.push_str("\"");12 } else {13 out.push_str(k);14 }9 match v {15 match v {10 Value::Object(o) if o.len() == 1 => {16 Value::Object(o) if o.len() == 1 => {11 let (k, v) = o.iter().next().unwrap();17 let (k, v) = o.iter().next().unwrap();26 Value::Null => out.push_str("null"),32 Value::Null => out.push_str("null"),27 Value::Boolean(v) => out.push_str(if *v { "true" } else { "false" }),33 Value::Boolean(v) => out.push_str(if *v { "true" } else { "false" }),28 Value::Number(n) => out.push_str(&format!("{}", n)),34 Value::Number(n) => out.push_str(&format!("{}", n)),29 Value::String(s) => out.push_str(&format!("{:?}", s)),35 Value::String(s) => out.push_str(&format!(36 "\"{}\"",37 s.replace('\\', "\\\\")38 .replace('"', "\\\"")39 .replace('\n', "\\n")40 .replace('\t', "\\t")41 .replace('\r', "\\r")42 .replace("$", "''$")43 )),30 Value::Array(a) => {44 Value::Array(a) => {31 if a.is_empty() {45 if a.is_empty() {