Skip to content

Commit

Permalink
Add Parse::parse_from_slice.
Browse files Browse the repository at this point in the history
  • Loading branch information
timothee-haudebourg committed Feb 21, 2024
1 parent 16f5e79 commit 876d5e7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "json-syntax"
version = "0.11.7"
version = "0.12.0"
edition = "2021"
authors = ["Timothée Haudebourg <author@haudebourg.net>"]
description = "Strict JSON parsing and mapping library"
Expand Down Expand Up @@ -34,6 +34,7 @@ contextual = { version = "0.1.1", optional = true }
ryu-js = { version = "0.2.2", optional = true }
serde = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
utf8-decode = "1.0.1"

[dev-dependencies]
serde = { version = "1.0", features = [ "derive" ] }
Expand Down
31 changes: 30 additions & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use decoded_char::DecodedChar;
use locspan::{Meta, Span};
use std::fmt;
use std::{fmt, io};

mod array;
mod boolean;
Expand Down Expand Up @@ -58,6 +58,16 @@ impl Default for Options {
}

pub trait Parse: Sized {
fn parse_slice(content: &[u8]) -> Result<(Self, CodeMap), Error> {
Self::parse_utf8(utf8_decode::Decoder::new(content.iter().copied()))
.map_err(Error::io_into_utf8)
}

fn parse_slice_with(content: &[u8], options: Options) -> Result<(Self, CodeMap), Error> {
Self::parse_utf8_with(utf8_decode::Decoder::new(content.iter().copied()), options)
.map_err(Error::io_into_utf8)
}

fn parse_str(content: &str) -> Result<(Self, CodeMap), Error> {
Self::parse_utf8(content.chars().map(Ok))
}
Expand Down Expand Up @@ -267,6 +277,9 @@ pub enum Error<E = core::convert::Infallible> {
///
/// The first parameter is the span at which the error occurred.
InvalidLowSurrogate(Span, u16, u32),

/// UTF-8 encoding error.
InvalidUtf8(usize),
}

impl<E> Error<E> {
Expand All @@ -284,6 +297,7 @@ impl<E> Error<E> {
Self::InvalidUnicodeCodePoint(span, _) => span.start(),
Self::MissingLowSurrogate(span, _) => span.start(),
Self::InvalidLowSurrogate(span, _, _) => span.start(),
Self::InvalidUtf8(p) => *p,
}
}

Expand All @@ -294,6 +308,20 @@ impl<E> Error<E> {
Self::InvalidUnicodeCodePoint(span, _) => *span,
Self::MissingLowSurrogate(span, _) => *span,
Self::InvalidLowSurrogate(span, _, _) => *span,
Self::InvalidUtf8(p) => Span::new(*p, *p),
}
}
}

impl Error<io::Error> {
fn io_into_utf8(self) -> Error {
match self {
Self::Stream(p, _) => Error::InvalidUtf8(p),
Self::Unexpected(p, e) => Error::Unexpected(p, e),
Self::InvalidUnicodeCodePoint(s, e) => Error::InvalidUnicodeCodePoint(s, e),
Self::MissingLowSurrogate(s, e) => Error::MissingLowSurrogate(s, e),
Self::InvalidLowSurrogate(s, a, b) => Error::InvalidLowSurrogate(s, a, b),
Self::InvalidUtf8(p) => Error::InvalidUtf8(p),
}
}
}
Expand All @@ -307,6 +335,7 @@ impl<E: fmt::Display> fmt::Display for Error<E> {
Self::InvalidUnicodeCodePoint(_, c) => write!(f, "invalid Unicode code point {:x}", *c),
Self::MissingLowSurrogate(_, _) => write!(f, "missing low surrogate"),
Self::InvalidLowSurrogate(_, _, _) => write!(f, "invalid low surrogate"),
Self::InvalidUtf8(_) => write!(f, "invalid UTF-8"),
}
}
}
Expand Down

0 comments on commit 876d5e7

Please sign in to comment.