git.delta.rocks / jrsonnet / refs/commits / 52a24f69aae7

difftreelog

fix(nixlike) string roundtrips

Yaroslav Bolyukin2021-10-01parent: #a9e03a3.patch.diff
in: trunk

6 files changed

addedcrates/nixlike/fuzz/.gitignorediffbeforeafterboth

no changes

addedcrates/nixlike/fuzz/Cargo.lockdiffbeforeafterboth

no changes

addedcrates/nixlike/fuzz/Cargo.tomldiffbeforeafterboth

no changes

addedcrates/nixlike/fuzz/fuzz_targets/fuzz_target_1.rsdiffbeforeafterboth

no changes

modifiedcrates/nixlike/src/lib.rsdiffbeforeafterboth
40 rule string_char() -> &'input str40 rule string_char() -> &'input str
41 = "\\\"" { "\"" }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() -> String
45 = 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() -> String
50 = 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 = "{" _
111118
112#[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}
131123
modifiedcrates/nixlike/src/to_string.rsdiffbeforeafterboth
5};5};
66
7fn 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() {