Skip to content

Commit 4708122

Browse files
authored
feat: support api for deserializer to check trailing chars (#177)
* feat: support api for deserializer to check trailing chars * chore: update version * chore: cargo clippy
1 parent 063186c commit 4708122

File tree

20 files changed

+105
-75
lines changed

20 files changed

+105
-75
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ license = "Apache-2.0"
1010
name = "sonic-rs"
1111
readme = "README.md"
1212
repository = "https://github.com/cloudwego/sonic-rs"
13-
version = "0.5.0"
13+
version = "0.5.1"
1414

1515
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1616

examples/get_many.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn main() {
2626
}
2727
}
2828
Err(e) => {
29-
println!("err: {:?}", e)
29+
println!("err: {e:?}")
3030
}
3131
}
3232
}

examples/handle_error.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,41 +11,41 @@ fn main() {
1111
// deal with Eof errors
1212
let err = from_str::<Foo>("{\"a\": [").unwrap_err();
1313
assert!(err.is_eof());
14-
eprintln!("{}", err);
14+
eprintln!("{err}");
1515
// EOF while parsing at line 1 column 6
1616

1717
// {"a": [
1818
// ......^
1919
assert_eq!(
20-
format!("{}", err),
20+
format!("{err}"),
2121
"EOF while parsing at line 1 column 6\n\n\t{\"a\": [\n\t......^\n"
2222
);
2323

2424
// deal with unmatched type errors
2525
let err = from_str::<Foo>("{ \"b\":[]}").unwrap_err();
26-
eprintln!("{}", err);
26+
eprintln!("{err}");
2727
assert!(err.is_unmatched_type());
2828
// println as follows:
2929
// missing field `a` at line 1 column 8
3030
//
3131
// { "b":[]}
3232
// ........^
3333
assert_eq!(
34-
format!("{}", err),
34+
format!("{err}"),
3535
"missing field `a` at line 1 column 8\n\n\t{ \"b\":[]}\n\t........^\n"
3636
);
3737

3838
// deal with Syntax errors
3939
let err = from_slice::<Foo>(b"{\"b\":\"\x80\"}").unwrap_err();
40-
eprintln!("{}", err);
40+
eprintln!("{err}");
4141
assert!(err.is_syntax());
4242
// println as follows:
4343
// Invalid UTF-8 characters in json at line 1 column 6
4444
//
4545
// {"b":"�"}
4646
// ......^...
4747
assert_eq!(
48-
format!("{}", err),
48+
format!("{err}"),
4949
"Invalid UTF-8 characters in json at line 1 column 6\n\n\t{\"b\":\"\"}\n\t......^..\n"
5050
);
5151
}

src/error.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ impl Error {
332332
0
333333
};
334334
let mask = ".".repeat(left) + "^" + &".".repeat(right);
335-
let descript = format!("\n\n\t{}\n\t{}\n", fragment, mask);
335+
let descript = format!("\n\n\t{fragment}\n\t{mask}\n");
336336

337337
Error {
338338
err: Box::new(ErrorImpl {
@@ -426,9 +426,9 @@ impl de::Error for Error {
426426
#[cold]
427427
fn invalid_type(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self {
428428
if let de::Unexpected::Unit = unexp {
429-
Error::custom(format_args!("invalid type: null, expected {}", exp))
429+
Error::custom(format_args!("invalid type: null, expected {exp}"))
430430
} else {
431-
Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
431+
Error::custom(format_args!("invalid type: {unexp}, expected {exp}"))
432432
}
433433
}
434434
}
@@ -520,73 +520,73 @@ mod test {
520520

521521
let err = from_str::<Foo>("{ \"b\":[]}").unwrap_err();
522522
assert_eq!(
523-
format!("{}", err),
523+
format!("{err}"),
524524
"missing field `a` at line 1 column 8\n\n\t{ \"b\":[]}\n\t........^\n"
525525
);
526526

527527
let err = from_str::<Foo>("{\"a\": [1, 2x, 3, 4, 5]}").unwrap_err();
528-
println!("{}", err);
528+
println!("{err}");
529529
assert_eq!(
530-
format!("{}", err),
530+
format!("{err}"),
531531
"Expected this character to be either a ',' or a ']' while parsing at line 1 column \
532532
11\n\n\t\": [1, 2x, 3, 4,\n\t........^.......\n"
533533
);
534534

535535
let err = from_str::<Foo>("{\"a\": null}").unwrap_err();
536536
assert_eq!(
537-
format!("{}", err),
537+
format!("{err}"),
538538
"invalid type: null, expected a sequence at line 1 column 9\n\n\t\"a\": \
539539
null}\n\t........^.\n"
540540
);
541541

542542
let err = from_str::<Foo>("{\"a\": [1,2,3 }").unwrap_err();
543543
assert_eq!(
544-
format!("{}", err),
544+
format!("{err}"),
545545
"Expected this character to be either a ',' or a ']' while parsing at line 1 column \
546546
14\n\n\t[1,2,3 }\n\t........^\n"
547547
);
548548

549549
let err = from_str::<Foo>("{\"a\": [\"123\"]}").unwrap_err();
550550
assert_eq!(
551-
format!("{}", err),
551+
format!("{err}"),
552552
"invalid type: string \"123\", expected i32 at line 1 column 11\n\n\t\": \
553553
[\"123\"]}\n\t........^..\n"
554554
);
555555

556556
let err = from_str::<Foo>("{\"a\": [").unwrap_err();
557557
assert_eq!(
558-
format!("{}", err),
558+
format!("{err}"),
559559
"EOF while parsing at line 1 column 6\n\n\t{\"a\": [\n\t......^\n"
560560
);
561561

562562
let err = from_str::<Foo>("{\"a\": [000]}").unwrap_err();
563563
assert_eq!(
564-
format!("{}", err),
564+
format!("{err}"),
565565
"Expected this character to be either a ',' or a ']' while parsing at line 1 column \
566566
8\n\n\t{\"a\": [000]}\n\t........^...\n"
567567
);
568568

569569
let err = from_str::<Foo>("{\"a\": [-]}").unwrap_err();
570570
assert_eq!(
571-
format!("{}", err),
571+
format!("{err}"),
572572
"Invalid number at line 1 column 7\n\n\t{\"a\": [-]}\n\t.......^..\n"
573573
);
574574

575575
let err = from_str::<Foo>("{\"a\": [-1.23e]}").unwrap_err();
576576
assert_eq!(
577-
format!("{}", err),
577+
format!("{err}"),
578578
"Invalid number at line 1 column 12\n\n\t: [-1.23e]}\n\t........^..\n"
579579
);
580580

581581
let err = from_str::<Foo>("{\"c\": \"哈哈哈哈哈哈}").unwrap_err();
582582
assert_eq!(
583-
format!("{}", err),
583+
format!("{err}"),
584584
"EOF while parsing at line 1 column 25\n\n\t哈哈哈}\n\t.........^\n"
585585
);
586586

587587
let err = from_slice::<Foo>(b"{\"b\":\"\x80\"}").unwrap_err();
588588
assert_eq!(
589-
format!("{}", err),
589+
format!("{err}"),
590590
"Invalid UTF-8 characters in json at line 1 column 6\n\n\t{\"b\":\"\"}\n\t......^..\n"
591591
);
592592
}
@@ -595,7 +595,7 @@ mod test {
595595
fn test_other_errors() {
596596
let err = crate::Value::try_from(f64::NAN).unwrap_err();
597597
assert_eq!(
598-
format!("{}", err),
598+
format!("{err}"),
599599
"NaN or Infinity is not a valid JSON value"
600600
);
601601
}

src/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl Index for usize {
142142
let typ = v.get_type();
143143
let len = v.len();
144144
v.as_array_mut()
145-
.unwrap_or_else(|| panic!("cannot access index in non-array value type {:?}", typ))
145+
.unwrap_or_else(|| panic!("cannot access index in non-array value type {typ:?}"))
146146
.0
147147
.get_index_mut(*self)
148148
.unwrap_or_else(|| panic!("index {} out of bounds (len: {})", *self, len))

src/lazyvalue/get.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ mod test {
569569
fn test_get_from_json_failed() {
570570
fn test_get_failed(json: &[u8], path: &JsonPointer) {
571571
let out = get_from_slice(json, path);
572-
assert!(out.is_err(), "json is {:?}", json);
572+
assert!(out.is_err(), "json is {json:?}");
573573

574574
// test for SIMD codes
575575
let json = unsafe { from_utf8_unchecked(json) }.to_string() + &" ".repeat(1000);

src/lazyvalue/iterator.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ impl<'de> ObjectJsonIter<'de> {
8888
}
8989

9090
pub(crate) fn new<I: JsonInput<'de>>(input: I, skip_strict: bool) -> Self {
91-
let validate_utf8 = skip_strict
92-
.then_some(input.need_utf8_valid())
93-
.unwrap_or_default();
91+
let validate_utf8 = if skip_strict {
92+
input.need_utf8_valid()
93+
} else {
94+
Default::default()
95+
};
9496

9597
Self {
9698
parser: Parser::new(Read::new_in(input.to_json_slice(), validate_utf8)),
@@ -147,9 +149,11 @@ impl<'de> ArrayJsonIter<'de> {
147149
}
148150

149151
pub(crate) fn new<I: JsonInput<'de>>(input: I, skip_strict: bool) -> Self {
150-
let validate_utf8 = skip_strict
151-
.then_some(input.need_utf8_valid())
152-
.unwrap_or_default();
152+
let validate_utf8 = if skip_strict {
153+
input.need_utf8_valid()
154+
} else {
155+
Default::default()
156+
};
153157

154158
Self {
155159
parser: Parser::new(Read::new_in(input.to_json_slice(), validate_utf8)),
@@ -394,8 +398,7 @@ mod test {
394398
assert_eq!(
395399
ret.1.as_raw_str().as_bytes(),
396400
val.as_bytes(),
397-
"key is {} ",
398-
key
401+
"key is {key} ",
399402
);
400403
assert_eq!(ret.1.get_type(), typ);
401404

@@ -404,8 +407,7 @@ mod test {
404407
assert_eq!(
405408
ret.1.as_raw_str().as_bytes(),
406409
val.as_bytes(),
407-
"key is {} ",
408-
key
410+
"key is {key} ",
409411
);
410412
assert_eq!(ret.1.get_type(), typ);
411413
};
@@ -521,7 +523,7 @@ mod test {
521523
let json = Bytes::from(r#"[1, true, "hello", null, 5, 6]"#);
522524
let iter = to_array_iter(&json);
523525
let out: Vec<JsonType> = iter.map(|e| e.get_type()).collect();
524-
println!("array elem type is {:?}", out);
526+
println!("array elem type is {out:?}");
525527
}
526528

527529
#[test]

src/lazyvalue/ser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ mod test {
6161
};
6262
assert_eq!(to_string(&data).unwrap(), to_string(&data2).unwrap());
6363
let json = json.trim();
64-
let expect: String = format!("{{\"borrowed_lv\":{},\"owned_lv\":{}}}", json, json);
64+
let expect: String = format!("{{\"borrowed_lv\":{json},\"owned_lv\":{json}}}");
6565
let serialized = to_string(&data).expect(json);
6666
assert_eq!(expect, serialized);
6767
assert_eq!(serialized, to_string(&data).unwrap());
@@ -85,7 +85,7 @@ mod test {
8585
fn test_raw_value_failed() {
8686
fn test_json_failed(json: &str) {
8787
let ret: Result<LazyValue<'_>> = from_str(json);
88-
assert!(ret.is_err(), "invalid json is {}", json);
88+
assert!(ret.is_err(), "invalid json is {json}");
8989
}
9090
test_json_failed(r#"""#);
9191
test_json_failed(r#""raw " value""#);

src/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ impl<'b, 'c> From<Reference<'b, 'c, str>> for Cow<'b, str> {
6060
impl<'b, 'c, T: Debug + ?Sized + 'static> Debug for Reference<'b, 'c, T> {
6161
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
6262
match self {
63-
Self::Borrowed(c) => write!(f, "Borrowed({:?})", c),
64-
Self::Copied(c) => write!(f, "Copied({:?})", c),
63+
Self::Borrowed(c) => write!(f, "Borrowed({c:?})"),
64+
Self::Copied(c) => write!(f, "Copied({c:?})"),
6565
}
6666
}
6767
}

src/pointer/point.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ mod test {
5858
#[test]
5959
fn test_json_pointer() {
6060
let pointers = pointer![];
61-
println!("{:?}", pointers);
61+
println!("{pointers:?}");
6262
let mut pointers = pointer![1, 2, 3, "foo", "bar"].to_vec();
6363
pointers.push(123.into());
64-
println!("{:?}", pointers);
64+
println!("{pointers:?}");
6565
}
6666
}

0 commit comments

Comments
 (0)