Skip to content

Commit

Permalink
better debug for Peek (#86)
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Apr 23, 2024
1 parent ad047bf commit 65d3333
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
21 changes: 20 additions & 1 deletion crates/jiter/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use std::fmt;

use crate::errors::{json_err, JsonResult, LinePosition};
use crate::number_decoder::AbstractNumberDecoder;
use crate::string_decoder::{AbstractStringDecoder, Tape};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct Peek(u8);

#[allow(non_upper_case_globals)] // while testing
Expand All @@ -18,6 +20,23 @@ impl Peek {
pub const Object: Self = Self(b'{');
}

impl fmt::Debug for Peek {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.0 {
b'n' => write!(f, "Null"),
b't' => write!(f, "True"),
b'f' => write!(f, "False"),
b'-' => write!(f, "Minus"),
b'I' => write!(f, "Infinity"),
b'N' => write!(f, "NaN"),
b'"' => write!(f, "String"),
b'[' => write!(f, "Array"),
b'{' => write!(f, "Object"),
_ => write!(f, "Peek({:?})", self.0 as char),
}
}
}

impl Peek {
pub const fn new(next: u8) -> Self {
Self(next)
Expand Down
7 changes: 7 additions & 0 deletions crates/jiter/tests/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1173,6 +1173,13 @@ fn jiter_wrong_types() {
expect_wrong_type!(next_float, JsonType::Float);
}

#[test]
fn peek_debug() {
assert_eq!(format!("{:?}", Peek::True), "True");
assert_eq!(format!("{:?}", Peek::False), "False");
assert_eq!(format!("{:?}", Peek::new(b'4')), "Peek('4')");
}

#[test]
fn jiter_invalid_numbers() {
let mut jiter = Jiter::new(b" -a", false);
Expand Down

0 comments on commit 65d3333

Please sign in to comment.