Skip to content

Commit

Permalink
Remove unused jiter_lite methods
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Nov 30, 2024
1 parent 72a9988 commit 13efbba
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 214 deletions.
10 changes: 0 additions & 10 deletions merde_json/src/jiter_lite/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ macro_rules! json_err {
};
}

use crate::jiter_lite::jiter::Jiter;
pub(crate) use json_err;

/// Enum representing all JSON types.
Expand Down Expand Up @@ -232,15 +231,6 @@ impl JiterError {
Self { error_type, index }
}

pub fn get_position(&self, jiter: &Jiter) -> LinePosition {
jiter.error_position(self.index)
}

pub fn description(&self, jiter: &Jiter) -> String {
let position = self.get_position(jiter);
format!("{} at {}", self.error_type, position)
}

pub(crate) fn wrong_type(expected: JsonType, actual: JsonType, index: usize) -> Self {
Self::new(JiterErrorType::WrongType { expected, actual }, index)
}
Expand Down
194 changes: 3 additions & 191 deletions merde_json/src/jiter_lite/jiter.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::jiter_lite as jiter;

use jiter::errors::{json_error, JiterError, JsonError, JsonErrorType, JsonType, LinePosition};
use jiter::number_decoder::{NumberAny, NumberFloat, NumberInt, NumberRange};
use jiter::errors::{json_error, JiterError, JsonError, JsonType};
use jiter::number_decoder::{NumberAny, NumberFloat};
use jiter::parse::{Parser, Peek};
use jiter::string_decoder::{StringDecoder, StringDecoderRange, Tape};
use jiter::string_decoder::{StringDecoder, Tape};

pub type JiterResult<T> = Result<T, JiterError>;

Expand Down Expand Up @@ -46,68 +46,17 @@ impl<'j> Jiter<'j> {
}
}

pub fn with_allow_inf_nan(mut self) -> Self {
self.allow_inf_nan = true;
self
}

pub fn with_allow_partial_strings(mut self) -> Self {
self.allow_partial_strings = true;
self
}

/// Get the current [LinePosition] of the parser.
pub fn current_position(&self) -> LinePosition {
self.parser.current_position()
}

/// Get the current index of the parser.
pub fn current_index(&self) -> usize {
self.parser.index
}

/// Get a slice of the underlying JSON data from `start` to `current_index`.
pub fn slice_to_current(&self, start: usize) -> &'j [u8] {
&self.data[start..self.current_index()]
}

/// Convert an error index to a [LinePosition].
///
/// # Arguments
/// - `index`: The index of the error to find the position of.
pub fn error_position(&self, index: usize) -> LinePosition {
LinePosition::find(self.data, index)
}

/// Peek at the next JSON value without consuming it.
pub fn peek(&mut self) -> JiterResult<Peek> {
self.parser.peek().map_err(Into::into)
}

/// Assuming the next value is `null`, consume it. Error if it is not `null`, or is invalid JSON.
pub fn next_null(&mut self) -> JiterResult<()> {
let peek = self.peek()?;
match peek {
Peek::Null => self.known_null(),
_ => Err(self.wrong_type(JsonType::Null, peek)),
}
}

/// Knowing the next value is `null`, consume it.
pub fn known_null(&mut self) -> JiterResult<()> {
self.parser.consume_null()?;
Ok(())
}

/// Assuming the next value is `true` or `false`, consume it. Error if it is not a boolean, or is invalid JSON.
///
/// # Returns
/// The boolean value.
pub fn next_bool(&mut self) -> JiterResult<bool> {
let peek = self.peek()?;
self.known_bool(peek)
}

/// Knowing the next value is `true` or `false`, parse it.
pub fn known_bool(&mut self, peek: Peek) -> JiterResult<bool> {
match peek {
Expand All @@ -123,80 +72,13 @@ impl<'j> Jiter<'j> {
}
}

/// Assuming the next value is a number, consume it. Error if it is not a number, or is invalid JSON.
///
/// # Returns
/// A [NumberAny] representing the number.
pub fn next_number(&mut self) -> JiterResult<NumberAny> {
let peek = self.peek()?;
self.known_number(peek)
}

/// Knowing the next value is a number, parse it.
pub fn known_number(&mut self, peek: Peek) -> JiterResult<NumberAny> {
self.parser
.consume_number::<NumberAny>(peek.into_inner(), self.allow_inf_nan)
.map_err(|e| self.maybe_number_error(e, JsonType::Int, peek))
}

/// Assuming the next value is an integer, consume it. Error if it is not an integer, or is invalid JSON.
pub fn next_int(&mut self) -> JiterResult<NumberInt> {
let peek = self.peek()?;
self.known_int(peek)
}

/// Knowing the next value is an integer, parse it.
pub fn known_int(&mut self, peek: Peek) -> JiterResult<NumberInt> {
self.parser
.consume_number::<NumberInt>(peek.into_inner(), self.allow_inf_nan)
.map_err(|e| {
if e.error_type == JsonErrorType::FloatExpectingInt {
JiterError::wrong_type(JsonType::Int, JsonType::Float, self.parser.index)
} else {
self.maybe_number_error(e, JsonType::Int, peek)
}
})
}

/// Assuming the next value is a float, consume it. Error if it is not a float, or is invalid JSON.
pub fn next_float(&mut self) -> JiterResult<f64> {
let peek = self.peek()?;
self.known_float(peek)
}

/// Knowing the next value is a float, parse it.
pub fn known_float(&mut self, peek: Peek) -> JiterResult<f64> {
self.parser
.consume_number::<NumberFloat>(peek.into_inner(), self.allow_inf_nan)
.map_err(|e| self.maybe_number_error(e, JsonType::Float, peek))
}

/// Assuming the next value is a number, consume it and return bytes from the original JSON data.
pub fn next_number_bytes(&mut self) -> JiterResult<&[u8]> {
let peek = self.peek()?;
self.known_number_bytes(peek)
}

/// Knowing the next value is a number, parse it and return bytes from the original JSON data.
fn known_number_bytes(&mut self, peek: Peek) -> JiterResult<&[u8]> {
match self
.parser
.consume_number::<NumberRange>(peek.into_inner(), self.allow_inf_nan)
{
Ok(numbe_range) => Ok(&self.data[numbe_range.range]),
Err(e) => Err(self.maybe_number_error(e, JsonType::Float, peek)),
}
}

/// Assuming the next value is a string, consume it. Error if it is not a string, or is invalid JSON.
pub fn next_str(&mut self) -> JiterResult<&str> {
let peek = self.peek()?;
match peek {
Peek::String => self.known_str(),
_ => Err(self.wrong_type(JsonType::String, peek)),
}
}

/// Knowing the next value is a string, parse it.
pub fn known_str(&mut self) -> JiterResult<&str> {
match self
Expand All @@ -208,36 +90,6 @@ impl<'j> Jiter<'j> {
}
}

/// Assuming the next value is a string, consume it and return bytes from the original JSON data.
pub fn next_bytes(&mut self) -> JiterResult<&[u8]> {
let peek = self.peek()?;
match peek {
Peek::String => self.known_bytes(),
_ => Err(self.wrong_type(JsonType::String, peek)),
}
}

/// Knowing the next value is a string, parse it and return bytes from the original JSON data.
pub fn known_bytes(&mut self) -> JiterResult<&[u8]> {
let range = self
.parser
.consume_string::<StringDecoderRange>(&mut self.tape, self.allow_partial_strings)?;
Ok(&self.data[range])
}

/// Assuming the next value is an array, peek at the first value.
/// Error if it is not an array, or is invalid JSON.
///
/// # Returns
/// The `Some(peek)` of the first value in the array is not empty, `None` if it is empty.
pub fn next_array(&mut self) -> JiterResult<Option<Peek>> {
let peek = self.peek()?;
match peek {
Peek::Array => self.known_array(),
_ => Err(self.wrong_type(JsonType::Array, peek)),
}
}

/// Assuming the next value is an array, peek at the first value.
pub fn known_array(&mut self) -> JiterResult<Option<Peek>> {
self.parser.array_first().map_err(Into::into)
Expand All @@ -248,58 +100,18 @@ impl<'j> Jiter<'j> {
self.parser.array_step().map_err(Into::into)
}

/// Assuming the next value is an object, consume the first key.
/// Error if it is not an object, or is invalid JSON.
///
/// # Returns
/// The `Some(key)` of the first key in the object is not empty, `None` if it is empty.
pub fn next_object(&mut self) -> JiterResult<Option<&str>> {
let peek = self.peek()?;
match peek {
Peek::Object => self.known_object(),
_ => Err(self.wrong_type(JsonType::Object, peek)),
}
}

/// Assuming the next value is an object, conssume the first key and return bytes from the original JSON data.
pub fn known_object(&mut self) -> JiterResult<Option<&str>> {
let op_str = self.parser.object_first::<StringDecoder>(&mut self.tape)?;
Ok(op_str.map(|s| s.as_str()))
}

/// Assuming the next value is an object, peek at the first key.
pub fn next_object_bytes(&mut self) -> JiterResult<Option<&[u8]>> {
let peek = self.peek()?;
match peek {
Peek::Object => {
let op_range = self
.parser
.object_first::<StringDecoderRange>(&mut self.tape)?;
Ok(op_range.map(|r| &self.data[r]))
}
_ => Err(self.wrong_type(JsonType::Object, peek)),
}
}

/// Get the next key in an object, or `None` if there are no more keys.
pub fn next_key(&mut self) -> JiterResult<Option<&str>> {
let strs = self.parser.object_step::<StringDecoder>(&mut self.tape)?;
Ok(strs.map(|s| s.as_str()))
}

/// Get the next key in an object as bytes, or `None` if there are no more keys.
pub fn next_key_bytes(&mut self) -> JiterResult<Option<&[u8]>> {
let op_range = self
.parser
.object_step::<StringDecoderRange>(&mut self.tape)?;
Ok(op_range.map(|r| &self.data[r]))
}

/// Finish parsing the JSON data. Error if there is more data to be parsed.
pub fn finish(&mut self) -> JiterResult<()> {
self.parser.finish().map_err(Into::into)
}

fn wrong_type(&self, expected: JsonType, peek: Peek) -> JiterError {
match peek {
Peek::True | Peek::False => {
Expand Down
14 changes: 1 addition & 13 deletions merde_json/src/jiter_lite/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::ops::Range;

use crate::jiter_lite as jiter;

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

Expand Down Expand Up @@ -76,10 +76,6 @@ impl<'j> Parser<'j> {
self.data.get(range)
}

pub fn current_position(&self) -> LinePosition {
LinePosition::find(self.data, self.index)
}

pub fn peek(&mut self) -> JsonResult<Peek> {
if let Some(next) = self.eat_whitespace() {
Ok(Peek::new(next))
Expand Down Expand Up @@ -178,14 +174,6 @@ impl<'j> Parser<'j> {
}
}

pub fn finish(&mut self) -> JsonResult<()> {
if self.eat_whitespace().is_none() {
Ok(())
} else {
json_err!(TrailingCharacters, self.index)
}
}

pub fn consume_true(&mut self) -> JsonResult<()> {
self.consume_ident(TRUE_REST)
}
Expand Down

0 comments on commit 13efbba

Please sign in to comment.