Skip to content

Commit

Permalink
Switch left and right assert_eq! args in examples
Browse files Browse the repository at this point in the history
This reads a bit more natural when left is the actual and right is the
expected value.

However, the tests still have the expected argument left, see also #29.
  • Loading branch information
Marcono1234 committed Dec 23, 2023
1 parent d2bd11e commit e7fbcc7
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 52 deletions.
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@ let json = r#"{"a": [1, true]}"#;
let mut json_reader = JsonStreamReader::new(json.as_bytes());

json_reader.begin_object()?;
assert_eq!("a", json_reader.next_name()?);
assert_eq!(json_reader.next_name()?, "a");

json_reader.begin_array()?;
assert_eq!(1_u32, json_reader.next_number()??);
assert_eq!(true, json_reader.next_bool()?);
assert_eq!(json_reader.next_number::<u32>()??, 1);
assert_eq!(json_reader.next_bool()?, true);
json_reader.end_array()?;

json_reader.end_object()?;
Expand Down Expand Up @@ -81,7 +81,8 @@ json_writer.end_object()?;
// Ensures that the JSON document is complete and flushes the buffer
json_writer.finish_document()?;

assert_eq!(r#"{"a":[1,true]}"#, String::from_utf8(writer)?);
let json = String::from_utf8(writer)?;
assert_eq!(json, r#"{"a":[1,true]}"#);
```

## Serde integration
Expand Down
9 changes: 5 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@
//! let mut json_reader = JsonStreamReader::new(json.as_bytes());
//!
//! json_reader.begin_object()?;
//! assert_eq!("a", json_reader.next_name()?);
//! assert_eq!(json_reader.next_name()?, "a");
//!
//! json_reader.begin_array()?;
//! assert_eq!(1_u32, json_reader.next_number()??);
//! assert_eq!(true, json_reader.next_bool()?);
//! assert_eq!(json_reader.next_number::<u32>()??, 1);
//! assert_eq!(json_reader.next_bool()?, true);
//! json_reader.end_array()?;
//!
//! json_reader.end_object()?;
Expand Down Expand Up @@ -79,7 +79,8 @@
//! // Ensures that the JSON document is complete and flushes the buffer
//! json_writer.finish_document()?;
//!
//! assert_eq!(r#"{"a":[1,true]}"#, String::from_utf8(writer)?);
//! let json = String::from_utf8(writer)?;
//! assert_eq!(json, r#"{"a":[1,true]}"#);
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
//!
Expand Down
43 changes: 22 additions & 21 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,9 @@ pub enum TransferError {
/// assert_eq!(json_reader.next_name()?, "a");
///
/// json_reader.begin_array()?;
/// assert_eq!(1_u32, json_reader.next_number()??);
/// assert_eq!(json_reader.next_number::<u32>()??, 1);
/// json_reader.skip_value()?;
/// assert_eq!(true, json_reader.next_bool()?);
/// assert_eq!(json_reader.next_bool()?, true);
/// json_reader.end_array()?;
///
/// json_reader.end_object()?;
Expand Down Expand Up @@ -1078,7 +1078,7 @@ pub trait JsonReader {
///
/// json_reader.skip_value()?;
/// // Array does not have a next item anymore
/// assert_eq!(false, json_reader.has_next()?);
/// assert_eq!(json_reader.has_next()?, false);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -1114,7 +1114,7 @@ pub trait JsonReader {
/// # use struson::reader::*;
/// let mut json_reader = JsonStreamReader::new(r#"{"a": 1}"#.as_bytes());
/// json_reader.begin_object()?;
/// assert_eq!("a", json_reader.next_name()?);
/// assert_eq!(json_reader.next_name()?, "a");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -1158,7 +1158,7 @@ pub trait JsonReader {
/// ```
/// # use struson::reader::*;
/// let mut json_reader = JsonStreamReader::new(r#""text with \"quotes\"""#.as_bytes());
/// assert_eq!("text with \"quotes\"", json_reader.next_str()?);
/// assert_eq!(json_reader.next_str()?, "text with \"quotes\"");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -1267,7 +1267,7 @@ pub trait JsonReader {
/// ```
/// # use struson::reader::*;
/// let mut json_reader = JsonStreamReader::new("12".as_bytes());
/// assert_eq!(12_u32, json_reader.next_number()??);
/// assert_eq!(json_reader.next_number::<u32>()??, 12);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -1320,7 +1320,7 @@ pub trait JsonReader {
/// ```
/// # use struson::reader::*;
/// let mut json_reader = JsonStreamReader::new("12.0e5".as_bytes());
/// assert_eq!("12.0e5", json_reader.next_number_as_str()?);
/// assert_eq!(json_reader.next_number_as_str()?, "12.0e5");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -1360,7 +1360,7 @@ pub trait JsonReader {
/// ```
/// # use struson::reader::*;
/// let mut json_reader = JsonStreamReader::new("true".as_bytes());
/// assert_eq!(true, json_reader.next_bool()?);
/// assert_eq!(json_reader.next_bool()?, true);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -1450,8 +1450,8 @@ pub trait JsonReader {
/// json_reader.consume_trailing_whitespace()?;
///
/// assert_eq!(
/// MyStruct { text: "some text".to_owned(), number: 5 },
/// value
/// value,
/// MyStruct { text: "some text".to_owned(), number: 5 }
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Expand Down Expand Up @@ -1497,7 +1497,7 @@ pub trait JsonReader {
/// // Skip member name "a"
/// json_reader.skip_name()?;
///
/// assert_eq!("1", json_reader.next_number_as_str()?);
/// assert_eq!(json_reader.next_number_as_str()?, "1");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -1530,12 +1530,12 @@ pub trait JsonReader {
/// let mut json_reader = JsonStreamReader::new(r#"{"a": [{}], "b": 1}"#.as_bytes());
/// json_reader.begin_object()?;
///
/// assert_eq!("a", json_reader.next_name()?);
/// assert_eq!(json_reader.next_name()?, "a");
///
/// // Skip member value [{}]
/// json_reader.skip_value()?;
///
/// assert_eq!("b", json_reader.next_name()?);
/// assert_eq!(json_reader.next_name()?, "b");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand All @@ -1561,11 +1561,11 @@ pub trait JsonReader {
/// in the path. Once this method returns successfully the reader will be positioned
/// before the last element specified by the path.
///
/// For example for the JSON path `foo[2]` it will start consuming a JSON object, skipping members
/// until it finds one with name "foo". Then it starts consuming the member value, expecting that
/// it is a JSON array, until right before the array item with (starting at 0) index 2.
/// If multiple members in a JSON object have the same name (for example `{"a": 1, "a": 2}`)
/// this method will seek to the first occurrence.
/// For example for the JSON path `json_path!["foo", 2]` it will start consuming a JSON object,
/// skipping members until it finds one with name "foo". Then it starts consuming the member
/// value, expecting that it is a JSON array, until right before the array item with (starting
/// at 0) index 2. If multiple members in a JSON object have the same name (for example
/// `{"a": 1, "a": 2}`) this method will seek to the first occurrence.
///
/// Seeking to a specific location can be useful when parts of the processed JSON document
/// are not relevant for the application processing it.
Expand All @@ -1580,7 +1580,7 @@ pub trait JsonReader {
/// json_reader.seek_to(&json_path!["foo", 2])?;
///
/// // Can now consume the value to which the call seeked to
/// assert_eq!("c", json_reader.next_str()?);
/// assert_eq!(json_reader.next_str()?, "c");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -1682,7 +1682,7 @@ pub trait JsonReader {
/// json_reader.seek_to(&json_path!["foo", 2])?;
///
/// // Consume the value to which the call seeked to
/// assert_eq!("c", json_reader.next_str()?);
/// assert_eq!(json_reader.next_str()?, "c");
///
/// // Skip the remainder of the document
/// json_reader.skip_to_top_level()?;
Expand Down Expand Up @@ -1731,7 +1731,8 @@ pub trait JsonReader {
/// json_writer.end_object()?;
/// json_writer.finish_document()?;
///
/// assert_eq!(r#"{"embedded":[1,2]}"#, String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, r#"{"embedded":[1,2]}"#);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down
4 changes: 2 additions & 2 deletions src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,8 @@ impl From<ParseFloatError> for DeserializerError {
/// json_reader.consume_trailing_whitespace()?;
///
/// assert_eq!(
/// MyStruct { text: "some text".to_owned(), number: 5 },
/// value
/// value,
/// MyStruct { text: "some text".to_owned(), number: 5 }
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Expand Down
9 changes: 5 additions & 4 deletions src/serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@
//! // Ensures that the JSON document is complete and flushes the buffer
//! json_writer.finish_document()?;
//!
//! let json = String::from_utf8(writer)?;
//! assert_eq!(
//! r#"{"outer":{"text":"some text","number":5}}"#,
//! String::from_utf8(writer)?
//! json,
//! r#"{"outer":{"text":"some text","number":5}}"#
//! );
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
Expand Down Expand Up @@ -93,8 +94,8 @@
//! json_reader.consume_trailing_whitespace()?;
//!
//! assert_eq!(
//! MyStruct { text: "some text".to_owned(), number: 5 },
//! value
//! value,
//! MyStruct { text: "some text".to_owned(), number: 5 }
//! );
//! # Ok::<(), Box<dyn std::error::Error>>(())
//! ```
Expand Down
5 changes: 3 additions & 2 deletions src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,10 @@ impl serde::ser::Error for SerializerError {
/// // Ensures that the JSON document is complete and flushes the buffer
/// json_writer.finish_document()?;
///
/// let json = String::from_utf8(writer)?;
/// assert_eq!(
/// r#"{"text":"some text","number":5}"#,
/// String::from_utf8(writer)?
/// json,
/// r#"{"text":"some text","number":5}"#
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Expand Down
43 changes: 28 additions & 15 deletions src/writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ type IoError = std::io::Error;
/// // Ensures that the JSON document is complete and flushes the buffer
/// json_writer.finish_document()?;
///
/// assert_eq!(r#"{"a":[1,true]}"#, String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, r#"{"a":[1,true]}"#);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -107,7 +108,8 @@ pub trait JsonWriter {
///
/// json_writer.finish_document()?;
///
/// assert_eq!(r#"{"a":1}"#, String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, r#"{"a":1}"#);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -148,7 +150,8 @@ pub trait JsonWriter {
///
/// json_writer.finish_document()?;
///
/// assert_eq!("[1]", String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, "[1]");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -194,7 +197,8 @@ pub trait JsonWriter {
///
/// json_writer.finish_document()?;
///
/// assert_eq!(r#"{"a":1}"#, String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, r#"{"a":1}"#);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand All @@ -218,7 +222,8 @@ pub trait JsonWriter {
///
/// json_writer.finish_document()?;
///
/// assert_eq!("null", String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, "null");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand All @@ -241,7 +246,8 @@ pub trait JsonWriter {
///
/// json_writer.finish_document()?;
///
/// assert_eq!("true", String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, "true");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -274,7 +280,8 @@ pub trait JsonWriter {
///
/// json_writer.finish_document()?;
///
/// assert_eq!(r#""text with \"quotes\"""#, String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, r#""text with \"quotes\"""#);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -316,7 +323,8 @@ pub trait JsonWriter {
///
/// json_writer.finish_document()?;
///
/// assert_eq!(r#""text with \"quotes\"""#, String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, r#""text with \"quotes\"""#);
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -357,7 +365,8 @@ pub trait JsonWriter {
///
/// json_writer.finish_document()?;
///
/// assert_eq!("123.0e10", String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, "123.0e10");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -395,7 +404,8 @@ pub trait JsonWriter {
///
/// json_writer.finish_document()?;
///
/// assert_eq!("123", String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, "123");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand Down Expand Up @@ -424,7 +434,8 @@ pub trait JsonWriter {
///
/// json_writer.finish_document()?;
///
/// assert_eq!("4.5", String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, "4.5");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
///
Expand All @@ -440,7 +451,7 @@ pub trait JsonWriter {
/// usage by the user.
/*
* TODO: Maybe give this method a better name?
* TODO: Maybe also support writing in scientific notation, e.g. `4.1e20`, see also https://doc.rust-lang.org/std/fmt/trait.LowerExp.html
* TODO: Maybe also support writing in scientific notation? e.g. `4.1e20`, see also https://doc.rust-lang.org/std/fmt/trait.LowerExp.html
*/
fn fp_number_value<N: FloatingPointNumber>(&mut self, value: N) -> Result<(), JsonNumberError>;

Expand Down Expand Up @@ -486,9 +497,10 @@ pub trait JsonWriter {
/// // Ensures that the JSON document is complete and flushes the buffer
/// json_writer.finish_document()?;
///
/// let json = String::from_utf8(writer)?;
/// assert_eq!(
/// r#"{"outer":{"text":"some text","number":5}}"#,
/// String::from_utf8(writer)?
/// json,
/// r#"{"outer":{"text":"some text","number":5}}"#
/// );
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
Expand Down Expand Up @@ -587,7 +599,8 @@ pub trait StringValueWriter: Write {
///
/// json_writer.finish_document()?;
///
/// assert_eq!("\"one, two, three and four\"", String::from_utf8(writer)?);
/// let json = String::from_utf8(writer)?;
/// assert_eq!(json, "\"one, two, three and four\"");
/// # Ok::<(), Box<dyn std::error::Error>>(())
/// ```
fn write_str(&mut self, s: &str) -> Result<(), IoError> {
Expand Down

0 comments on commit e7fbcc7

Please sign in to comment.