Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmt: format all source files #46

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions benches/deserialize_struct.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#[macro_use]
extern crate criterion;

use criterion::SamplingMode;
use criterion::{criterion_group, BatchSize, Criterion, Throughput};
use std::fs::File;
use std::io::Read;
use std::str::from_utf8_unchecked;
use std::{fs::File, io::Read, str::from_utf8_unchecked};

use criterion::{criterion_group, BatchSize, Criterion, SamplingMode, Throughput};

#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
Expand Down Expand Up @@ -138,9 +136,7 @@ macro_rules! bench_file {
};
}

use json_benchmark::{citm_catalog::CitmCatalog, twitter::Twitter};

use json_benchmark::canada::Canada;
use json_benchmark::{canada::Canada, citm_catalog::CitmCatalog, twitter::Twitter};

bench_file!(
json: twitter,
Expand Down
11 changes: 5 additions & 6 deletions benches/deserialize_value.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#[macro_use]
extern crate criterion;

use criterion::SamplingMode;
use criterion::{criterion_group, BatchSize, Criterion, Throughput};
use std::fs::File;
use std::io::Read;
use std::str::from_utf8_unchecked;
use std::{fs::File, io::Read, str::from_utf8_unchecked};

use criterion::{criterion_group, BatchSize, Criterion, SamplingMode, Throughput};

#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
Expand Down Expand Up @@ -146,6 +144,7 @@ bench_file!(citm_catalog);
bench_file!(twitter);
bench_file!(github_events);

// criterion_group!(benches, canada, otfcc, citm_catalog, twitter, lottie, github_events, twitterescaped, book, poet, fgo);
// criterion_group!(benches, canada, otfcc, citm_catalog, twitter, lottie, github_events,
// twitterescaped, book, poet, fgo);
criterion_group!(benches, twitter, canada, citm_catalog);
criterion_main!(benches);
3 changes: 2 additions & 1 deletion benches/get_from.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#[macro_use]
extern crate criterion;
use criterion::{criterion_group, BatchSize, Criterion};
use std::io::Read;

use criterion::{criterion_group, BatchSize, Criterion};

fn bench_get(c: &mut Criterion) {
let core_ids = core_affinity::get_core_ids().unwrap();
core_affinity::set_for_current(core_ids[0]);
Expand Down
13 changes: 7 additions & 6 deletions benches/serialize_struct.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#[macro_use]
extern crate criterion;

use criterion::SamplingMode;
use criterion::{criterion_group, BatchSize, Criterion, Throughput};
use std::fs::File;
use std::io::Read;
use std::{fs::File, io::Read};

use criterion::{criterion_group, BatchSize, Criterion, SamplingMode, Throughput};

#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
Expand Down Expand Up @@ -101,8 +100,10 @@ macro_rules! bench_file {
};
}

use json_benchmark::canada::Canada;
use json_benchmark::copy::{citm_catalog::CitmCatalog, twitter::Twitter};
use json_benchmark::{
canada::Canada,
copy::{citm_catalog::CitmCatalog, twitter::Twitter},
};

bench_file!(
json: twitter,
Expand Down
10 changes: 5 additions & 5 deletions benches/serialize_value.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#[macro_use]
extern crate criterion;

use criterion::SamplingMode;
use criterion::{criterion_group, BatchSize, Criterion, Throughput};
use std::fs::File;
use std::io::Read;
use std::{fs::File, io::Read};

use criterion::{criterion_group, BatchSize, Criterion, SamplingMode, Throughput};

#[global_allocator]
static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc;
Expand Down Expand Up @@ -106,6 +105,7 @@ bench_file!(citm_catalog);
bench_file!(twitter);
bench_file!(github_events);

// criterion_group!(benches, canada, otfcc, citm_catalog, twitter, lottie, github_events, twitterescaped, book, poet, fgo);
// criterion_group!(benches, canada, otfcc, citm_catalog, twitter, lottie, github_events,
// twitterescaped, book, poet, fgo);
criterion_group!(benches, twitter, citm_catalog, canada);
criterion_main!(benches);
3 changes: 1 addition & 2 deletions examples/get_from.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use sonic_rs::JsonValueTrait;
use sonic_rs::{get, get_unchecked, pointer};
use sonic_rs::{get, get_unchecked, pointer, JsonValueTrait};

fn main() {
let path = pointer!["a", "b", "c", 1];
Expand Down
6 changes: 3 additions & 3 deletions examples/iterator.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use bytes::Bytes;
use faststr::FastStr;
use sonic_rs::JsonValueTrait;
use sonic_rs::{to_array_iter, to_object_iter_unchecked};
use sonic_rs::{to_array_iter, to_object_iter_unchecked, JsonValueTrait};
fn main() {
let json = Bytes::from(r#"[1, 2, 3, 4, 5, 6]"#);
let iter = to_array_iter(&json);
Expand All @@ -18,7 +17,8 @@ fn main() {
if elem.is_err() {
assert_eq!(
elem.err().unwrap().to_string(),
"Expected this character to be either a ',' or a ']' while parsing at line 1 column 17"
"Expected this character to be either a ',' or a ']' while parsing at line 1 \
column 17"
);
}
}
Expand Down
9 changes: 4 additions & 5 deletions examples/json_filter.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use faststr::FastStr;
use serde::ser::SerializeMap;
use serde::Serializer;
use sonic_rs::to_object_iter;
use sonic_rs::writer::WriteExt;
use std::collections::HashSet;

use faststr::FastStr;
use serde::{ser::SerializeMap, Serializer};
use sonic_rs::{to_object_iter, writer::WriteExt};

#[allow(clippy::mutable_key_type)]
fn filter_json<W: WriteExt>(json: &str, keys: HashSet<FastStr>, w: W) -> sonic_rs::Result<()> {
// create a new serialize from writer
Expand Down
9 changes: 1 addition & 8 deletions examples/json_number.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
use sonic_rs::{Deserialize, Serialize};

use sonic_rs::Number;
use sonic_rs::RawNumber;

use sonic_rs::from_str;
use sonic_rs::to_string;
use sonic_rs::JsonNumberTrait;
use sonic_rs::{from_str, to_string, Deserialize, JsonNumberTrait, Number, RawNumber, Serialize};

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct TestJsonNumber {
Expand Down
4 changes: 1 addition & 3 deletions examples/value.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Parse json into sonic_rs `Value`.

use sonic_rs::JsonValueMutTrait;
use sonic_rs::{from_str, json};
use sonic_rs::{pointer, JsonValueTrait, Value};
use sonic_rs::{from_str, json, pointer, JsonValueMutTrait, JsonValueTrait, Value};

fn main() {
let json = r#"{
Expand Down
15 changes: 6 additions & 9 deletions fuzz/fuzz_targets/from_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

use libfuzzer_sys::fuzz_target;
use serde_json::Value as JValue;
use sonic_rs::from_slice;
use sonic_rs::from_str;
use sonic_rs::value::JsonContainerTrait;
use sonic_rs::JsonNumberTrait;
use sonic_rs::JsonValueTrait;
use sonic_rs::Value;
use sonic_rs::{to_array_iter, to_array_iter_unchecked, to_object_iter, to_object_iter_unchecked};
use sonic_rs::{
from_slice, from_str, to_array_iter, to_array_iter_unchecked, to_object_iter,
to_object_iter_unchecked, value::JsonContainerTrait, JsonNumberTrait, JsonValueTrait, Value,
};

macro_rules! test_struct {
($ty:ty, $data:expr) => {
Expand Down Expand Up @@ -161,10 +158,10 @@ fn compare_value(jv: &JValue, sv: &sonic_rs::Value) -> bool {
true
}

use std::{borrow::Cow, collections::HashMap, hash::Hash, marker::PhantomData};

use faststr::FastStr;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::{collections::HashMap, hash::Hash, marker::PhantomData};

#[derive(Debug, Deserialize, Serialize, PartialEq)]
struct Foo {
Expand Down
9 changes: 9 additions & 0 deletions fuzz/rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
comment_width = 100
wrap_comments = true
format_code_in_doc_comments = true
format_strings = true
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
newline_style = "Unix"
reorder_imports = true
reorder_modules = true
9 changes: 9 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
comment_width = 100
wrap_comments = true
format_code_in_doc_comments = true
format_strings = true
group_imports = "StdExternalCrate"
imports_granularity = "Crate"
newline_style = "Unix"
reorder_imports = true
reorder_modules = true
36 changes: 22 additions & 14 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

// The code is cloned from [serde_json](https://github.com/serde-rs/json) and modified necessary parts.

use core::fmt::{self, Debug, Display};
use core::result;
use serde::{de, ser};
use std::error;
use std::str::FromStr;
use std::string::{String, ToString};
use core::{
fmt::{self, Debug, Display},
result,
};
use std::{
error,
str::FromStr,
string::{String, ToString},
};

use serde::{de, ser};
use thiserror::Error as ErrorTrait;

use crate::reader::Position;
Expand Down Expand Up @@ -48,7 +52,6 @@ impl Error {

/// The kind reported by the underlying standard library I/O error, if this
/// error was caused by a failure to read or write bytes on an I/O stream.
///
pub fn io_error_kind(&self) -> Option<std::io::ErrorKind> {
if let ErrorCode::Io(io_error) = &self.err.code {
Some(io_error.kind())
Expand Down Expand Up @@ -141,7 +144,6 @@ impl From<Error> for std::io::Error {
///
/// JSON syntax and data errors are turned into `InvalidData` I/O errors.
/// EOF errors are turned into `UnexpectedEof` I/O errors.
///
fn from(j: Error) -> Self {
match j.err.code {
ErrorCode::Io(err) => err,
Expand All @@ -165,7 +167,8 @@ pub enum Category {

/// The error was caused when the input data is unmatched for expected type.
///
/// For example, JSON containing a number when the type being deserialized into holds a String.
/// For example, JSON containing a number when the type being deserialized into holds a
/// String.
TypeUnmatched,

/// The error was caused when the target field was not found from JSON.
Expand Down Expand Up @@ -511,25 +514,29 @@ mod test {
println!("{}", err);
assert_eq!(
format!("{}", err),
"Expected this character to be either a ',' or a ']' while parsing at line 1 column 11\n\n\t\": [1, 2x, 3, 4,\n\t........^.......\n"
"Expected this character to be either a ',' or a ']' while parsing at line 1 column \
11\n\n\t\": [1, 2x, 3, 4,\n\t........^.......\n"
);

let err = from_str::<Foo>("{\"a\": null}").unwrap_err();
assert_eq!(
format!("{}", err),
"invalid type: null, expected a sequence at line 1 column 9\n\n\t\"a\": null}\n\t........^.\n"
"invalid type: null, expected a sequence at line 1 column 9\n\n\t\"a\": \
null}\n\t........^.\n"
);

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

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

let err = from_str::<Foo>("{\"a\": [").unwrap_err();
Expand All @@ -541,7 +548,8 @@ mod test {
let err = from_str::<Foo>("{\"a\": [000]}").unwrap_err();
assert_eq!(
format!("{}", err),
"Expected this character to be either a ',' or a ']' while parsing at line 1 column 8\n\n\t{\"a\": [000]}\n\t........^...\n"
"Expected this character to be either a ',' or a ']' while parsing at line 1 column \
8\n\n\t{\"a\": [000]}\n\t........^...\n"
);

let err = from_str::<Foo>("{\"a\": [-]}").unwrap_err();
Expand Down
4 changes: 2 additions & 2 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// The code is cloned from [serde_json](https://github.com/serde-rs/json) and modified necessary parts.

use crate::util::string::format_string;
use crate::writer::WriteExt;
use std::io::{self, Write};

use crate::{util::string::format_string, writer::WriteExt};

// We only use our own error type; no need for From conversions provided by the
// standard library's try! macro. This reduces lines of LLVM IR by 4%.
macro_rules! tri {
Expand Down
19 changes: 9 additions & 10 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::util::private::Sealed;
use crate::util::reborrow::DormantMutRef;
use crate::value::object::DEFAULT_OBJ_CAP;
use crate::value::shared::Shared;
use crate::value::shared::SharedCtxGuard;
use crate::JsonValueTrait;
use crate::PointerNode;
use crate::{JsonValueMutTrait, Value};
use std::convert::Into;

use crate::{
util::{private::Sealed, reborrow::DormantMutRef},
value::{
object::DEFAULT_OBJ_CAP,
shared::{Shared, SharedCtxGuard},
},
JsonValueMutTrait, JsonValueTrait, PointerNode, Value,
};

impl<I> std::ops::Index<I> for Value
where
I: Index,
Expand Down Expand Up @@ -91,7 +92,6 @@ impl<I: Index> std::ops::IndexMut<I> for Value {
/// "a": { "b": {"c": {"d": true}}},
/// "z": {"zz": "insert in null"}
/// }));
///
/// ```
#[inline]
fn index_mut(&mut self, index: I) -> &mut Value {
Expand All @@ -100,7 +100,6 @@ impl<I: Index> std::ops::IndexMut<I> for Value {
}

/// An indexing trait for JSON.
///
pub trait Index: Sealed {
/// Return None if the index is not already in the array or object.
#[doc(hidden)]
Expand Down
5 changes: 2 additions & 3 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::parser::as_str;
use crate::util::private::Sealed;
use bytes::Bytes;
use faststr::FastStr;

use crate::{parser::as_str, util::private::Sealed};

/// JsonSlice is a wrapper for different json input.
///
#[doc(hidden)]
#[non_exhaustive]
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
Expand Down
Loading
Loading