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

Add a Positioned type to fix RawValue error position. #933

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
55 changes: 55 additions & 0 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use crate::error::{Error, ErrorCode, Result};
use crate::lexical;
use crate::number::Number;
use crate::read::{self, Fused, Reference};
#[cfg(feature = "raw_value")]
use crate::Position;
use alloc::string::String;
use alloc::vec::Vec;
#[cfg(feature = "float_roundtrip")]
Expand Down Expand Up @@ -1757,6 +1759,7 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
self.deserialize_seq(visitor)
}

#[cfg(not(feature = "raw_value"))]
fn deserialize_tuple_struct<V>(
self,
_name: &'static str,
Expand All @@ -1769,6 +1772,58 @@ impl<'de, 'a, R: Read<'de>> de::Deserializer<'de> for &'a mut Deserializer<R> {
self.deserialize_seq(visitor)
}

#[cfg(feature = "raw_value")]
fn deserialize_tuple_struct<V>(
self,
name: &'static str,
len: usize,
visitor: V,
) -> Result<V::Value>
where
V: de::Visitor<'de>,
{
if name == crate::read::TOKEN && len == 2 {
struct PosAccess(u8, Position);
impl<'de> serde::de::SeqAccess<'de> for PosAccess {
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
where
T: de::DeserializeSeed<'de>,
{
let res = match self.0 {
0 => {
seed.deserialize(serde::de::value::UsizeDeserializer::new(self.1.line))?
}
1 => seed
.deserialize(serde::de::value::UsizeDeserializer::new(self.1.column))?,
_ => return Ok(None),
};
self.0 += 1;
Ok(Some(res))
}
}
return visitor.visit_seq(PosAccess(0, self.read.position()));
} else if name == crate::positioned::TOKEN && len == 2 {
struct PosAccess<'a, R: 'a>(u8, &'a mut Deserializer<R>);
impl<'de, 'a, R: Read<'de> + 'a> serde::de::SeqAccess<'de> for PosAccess<'a, R> {
type Error = Error;
fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
where
T: de::DeserializeSeed<'de>,
{
let res = match self.0 {
0 | 1 => seed.deserialize(&mut *self.1)?,
_ => return Ok(None),
};
self.0 += 1;
Ok(Some(res))
}
}
return visitor.visit_seq(PosAccess(0, self));
}
self.deserialize_seq(visitor)
}

fn deserialize_map<V>(self, visitor: V) -> Result<V::Value>
where
V: de::Visitor<'de>,
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -417,5 +417,12 @@ mod lexical;
mod number;
mod read;

#[cfg(feature = "raw_value")]
mod positioned;
#[cfg(feature = "raw_value")]
mod raw;

#[cfg(feature = "raw_value")]
pub use positioned::Positioned;
#[cfg(feature = "raw_value")]
pub use read::Position;
76 changes: 76 additions & 0 deletions src/positioned.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use core::marker::PhantomData;

use serde::{
de::{SeqAccess, Visitor},
Deserialize, Deserializer, Serialize,
};

use crate::read::Position;
#[cfg(feature = "raw_value")]
use crate::{de::StrRead, read::PositionedRead, value::RawValue};

pub const TOKEN: &str = "$serde_json::private::Positioned";

/// A value that is saved together with its position in the input.
pub struct Positioned<T> {
/// The position in the input.
pub position: Position,
/// The actual deserialized value.
pub value: T,
}

impl<'de, T> Deserialize<'de> for Positioned<T>
where
T: Deserialize<'de>,
{
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct PosVisitor<T>(PhantomData<T>);

impl<'de, T: Deserialize<'de>> Visitor<'de> for PosVisitor<T> {
type Value = Positioned<T>;
fn expecting(&self, formatter: &mut alloc::fmt::Formatter) -> alloc::fmt::Result {
write!(formatter, "positioned value")
}

fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
Ok(Positioned {
position: seq.next_element()?.unwrap(),
value: seq.next_element()?.unwrap(),
})
}
}

deserializer.deserialize_tuple_struct(TOKEN, 2, PosVisitor(PhantomData))
}
}

impl<T: Serialize> Serialize for Positioned<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
self.value.serialize(serializer)
}
}

#[cfg(all(feature = "raw_value", feature = "std"))]
impl Positioned<Box<RawValue>> {
/// Read from a positioned RawValue.
pub fn read(&self) -> PositionedRead<StrRead<'_>> {
PositionedRead::new(self.position.clone(), StrRead::new(self.value.get()))
}
}

#[cfg(feature = "raw_value")]
impl<'a> Positioned<&'a RawValue> {
/// Read from a positioned RawValue.
pub fn read(&self) -> PositionedRead<StrRead<'a>> {
PositionedRead::new(self.position.clone(), StrRead::new(self.value.get()))
}
}
154 changes: 154 additions & 0 deletions src/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ use core::char;
use core::cmp;
use core::ops::Deref;
use core::str;
#[cfg(feature = "raw_value")]
use serde::de::SeqAccess;
#[cfg(feature = "raw_value")]
use serde::ser::SerializeStruct;
#[cfg(feature = "raw_value")]
use serde::Deserialize;
#[cfg(feature = "raw_value")]
use serde::Serialize;

#[cfg(feature = "std")]
use crate::io;
Expand Down Expand Up @@ -114,8 +122,12 @@ pub trait Read<'de>: private::Sealed {
fn set_failed(&mut self, failed: &mut bool);
}

/// The position in the input stream.
#[derive(Clone)]
pub struct Position {
/// The current line number.
pub line: usize,
/// The current column number.
pub column: usize,
}

Expand Down Expand Up @@ -1002,3 +1014,145 @@ fn decode_hex_val(val: u8) -> Option<u16> {
Some(n)
}
}

#[cfg(feature = "raw_value")]
pub(crate) const TOKEN: &str = "$serde_json::private::Position";

#[cfg(feature = "raw_value")]
impl<'de> Deserialize<'de> for Position {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct PosVisitor;

impl<'de> Visitor<'de> for PosVisitor {
type Value = Position;

fn expecting(&self, f: &mut alloc::fmt::Formatter) -> alloc::fmt::Result {
write!(f, "position indicator")
}

fn visit_seq<A>(self, mut seq: A) -> core::result::Result<Self::Value, A::Error>
where
A: SeqAccess<'de>,
{
Ok(Position {
line: seq.next_element()?.unwrap(),
column: seq.next_element()?.unwrap(),
})
}
}

deserializer.deserialize_tuple_struct(TOKEN, 2, PosVisitor)
}
}

#[cfg(feature = "raw_value")]
impl Serialize for Position {
fn serialize<S>(&self, serializer: S) -> core::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut s = serializer.serialize_struct("position", 2)?;
s.serialize_field("line", &self.line)?;
s.serialize_field("column", &self.column)?;
s.end()
}
}

#[cfg(feature = "raw_value")]
pub struct PositionedRead<R> {
start: Position,
read: R,
}

#[cfg(feature = "raw_value")]
impl<'de, R: Read<'de>> private::Sealed for PositionedRead<R> {}

#[cfg(feature = "raw_value")]
impl<'de, R: Read<'de>> PositionedRead<R> {
pub fn new(start: Position, read: R) -> Self {
Self { start, read }
}

fn adjust_pos(&self, pos: Position) -> Position {
match pos.line == 1 {
true => Position {
line: self.start.line,
column: self.start.column + pos.column + 1,
},
false => Position {
line: self.start.line + pos.line - 1,
column: pos.column,
},
}
}
}

#[cfg(feature = "raw_value")]
impl<'de, R: Read<'de>> Read<'de> for PositionedRead<R> {
fn next(&mut self) -> Result<Option<u8>> {
self.read.next()
}

fn peek(&mut self) -> Result<Option<u8>> {
self.read.peek()
}

fn discard(&mut self) {
self.read.discard()
}

fn position(&self) -> Position {
self.adjust_pos(self.read.position())
}

fn peek_position(&self) -> Position {
self.adjust_pos(self.read.peek_position())
}

fn byte_offset(&self) -> usize {
self.read.byte_offset()
}

fn parse_str<'s>(&'s mut self, scratch: &'s mut Vec<u8>) -> Result<Reference<'de, 's, str>> {
self.read.parse_str(scratch)
}

fn parse_str_raw<'s>(
&'s mut self,
scratch: &'s mut Vec<u8>,
) -> Result<Reference<'de, 's, [u8]>> {
self.read.parse_str_raw(scratch)
}

fn ignore_str(&mut self) -> Result<()> {
self.read.ignore_str()
}

fn decode_hex_escape(&mut self) -> Result<u16> {
self.read.decode_hex_escape()
}

#[cfg(feature = "raw_value")]
#[doc(hidden)]
fn begin_raw_buffering(&mut self) {
self.read.begin_raw_buffering()
}

#[cfg(feature = "raw_value")]
#[doc(hidden)]
fn end_raw_buffering<V>(&mut self, visitor: V) -> Result<V::Value>
where
V: Visitor<'de>,
{
self.read.end_raw_buffering(visitor)
}

const should_early_return_if_failed: bool = R::should_early_return_if_failed;

fn set_failed(&mut self, failed: &mut bool) {
self.read.set_failed(failed)
}
}