Skip to content

Commit

Permalink
Implemented ron::value::RawValue (ron-rs#407)
Browse files Browse the repository at this point in the history
* Implemented ron::value::RawValue

* Added more test cases for full coverage
  • Loading branch information
juntyr committed Aug 16, 2023
1 parent b4f1cdf commit 4116e77
Show file tree
Hide file tree
Showing 9 changed files with 609 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## Unreleased
- Add `ron::value::RawValue` helper type which can (de)serialize any valid RON ([#407](https://github.com/ron-rs/ron/pull/407))

## [0.8.1] - 2023-08-??
- Fix issues [#277](https://github.com/ron-rs/ron/issues/277) and [#405](https://github.com/ron-rs/ron/issues/405) with `Value::Map` `IntoIter` and extraneous item check for `Value::Seq` ([#406](https://github.com/ron-rs/ron/pull/406))
Expand Down
15 changes: 15 additions & 0 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,21 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
where
V: Visitor<'de>,
{
if name == crate::value::raw::RAW_VALUE_TOKEN {
let bytes_before = self.bytes.bytes();
self.bytes.skip_ws()?;
let _ignored = self.deserialize_ignored_any(serde::de::IgnoredAny)?;
self.bytes.skip_ws()?;
let bytes_after = self.bytes.bytes();

let ron_bytes = &bytes_before[..bytes_before.len() - bytes_after.len()];
let ron_str = str::from_utf8(ron_bytes).map_err(Error::from)?;

return visitor
.visit_borrowed_str::<Error>(ron_str)
.map_err(|_| Error::ExpectedRawValue);
}

if self.bytes.exts.contains(Extensions::UNWRAP_NEWTYPES) || self.newtype_variant {
self.newtype_variant = false;

Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ pub enum Error {
InvalidIdentifier(String),
SuggestRawIdentifier(String),
ExceededRecursionLimit,
ExpectedRawValue,
}

impl fmt::Display for SpannedError {
Expand Down Expand Up @@ -250,6 +251,7 @@ impl fmt::Display for Error {
identifier, identifier
),
Error::ExceededRecursionLimit => f.write_str("Exceeded recursion limit, try increasing the limit and using `serde_stacker` to protect against a stack overflow"),
Error::ExpectedRawValue => f.write_str("Expected a `ron::value::RawValue`"),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ impl<'a> Bytes<'a> {
}
}

pub fn bytes(&self) -> &[u8] {
pub fn bytes(&self) -> &'a [u8] {
self.bytes
}

Expand Down
5 changes: 5 additions & 0 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{
},
};

mod raw;
#[cfg(test)]
mod tests;
mod value;
Expand Down Expand Up @@ -572,6 +573,10 @@ impl<'a, W: io::Write> ser::Serializer for &'a mut Serializer<W> {
where
T: ?Sized + Serialize,
{
if name == crate::value::raw::RAW_VALUE_TOKEN {
return value.serialize(raw::RawValueSerializer::new(self));
}

if self.extensions().contains(Extensions::UNWRAP_NEWTYPES) || self.newtype_variant {
self.newtype_variant = false;

Expand Down
172 changes: 172 additions & 0 deletions src/ser/raw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
use std::io;

use serde::{ser, Serialize};

use super::{Error, Result, Serializer};

pub struct RawValueSerializer<'a, W: io::Write> {
ser: &'a mut Serializer<W>,
}

impl<'a, W: io::Write> RawValueSerializer<'a, W> {
pub fn new(ser: &'a mut Serializer<W>) -> Self {
Self { ser }
}
}

impl<'a, W: io::Write> ser::Serializer for RawValueSerializer<'a, W> {
type Error = Error;
type Ok = ();
type SerializeMap = ser::Impossible<(), Error>;
type SerializeSeq = ser::Impossible<(), Error>;
type SerializeStruct = ser::Impossible<(), Error>;
type SerializeStructVariant = ser::Impossible<(), Error>;
type SerializeTuple = ser::Impossible<(), Error>;
type SerializeTupleStruct = ser::Impossible<(), Error>;
type SerializeTupleVariant = ser::Impossible<(), Error>;

fn serialize_bool(self, _: bool) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_i8(self, _: i8) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_i16(self, _: i16) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_i32(self, _: i32) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_i64(self, _: i64) -> Result<()> {
Err(Error::ExpectedRawValue)
}

#[cfg(feature = "integer128")]
fn serialize_i128(self, _: i128) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_u8(self, _: u8) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_u16(self, _: u16) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_u32(self, _: u32) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_u64(self, _: u64) -> Result<()> {
Err(Error::ExpectedRawValue)
}

#[cfg(feature = "integer128")]
fn serialize_u128(self, _: u128) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_f32(self, _: f32) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_f64(self, _: f64) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_char(self, _: char) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_str(self, ron: &str) -> Result<()> {
self.ser.output.write_all(ron.as_bytes())?;
Ok(())
}

fn serialize_bytes(self, _: &[u8]) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_none(self) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_some<T: ?Sized + Serialize>(self, _: &T) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_unit(self) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_unit_struct(self, _: &'static str) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_unit_variant(self, _: &'static str, _: u32, _: &'static str) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_newtype_struct<T: ?Sized + Serialize>(self, _: &'static str, _: &T) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_newtype_variant<T: ?Sized + Serialize>(
self,
_: &'static str,
_: u32,
_: &'static str,
_: &T,
) -> Result<()> {
Err(Error::ExpectedRawValue)
}

fn serialize_seq(self, _: Option<usize>) -> Result<Self::SerializeSeq> {
Err(Error::ExpectedRawValue)
}

fn serialize_tuple(self, _: usize) -> Result<Self::SerializeTuple> {
Err(Error::ExpectedRawValue)
}

fn serialize_tuple_struct(
self,
_: &'static str,
_: usize,
) -> Result<Self::SerializeTupleStruct> {
Err(Error::ExpectedRawValue)
}

fn serialize_tuple_variant(
self,
_: &'static str,
_: u32,
_: &'static str,
_: usize,
) -> Result<Self::SerializeTupleVariant> {
Err(Error::ExpectedRawValue)
}

fn serialize_map(self, _: Option<usize>) -> Result<Self::SerializeMap> {
Err(Error::ExpectedRawValue)
}

fn serialize_struct(self, _: &'static str, _: usize) -> Result<Self::SerializeStruct> {
Err(Error::ExpectedRawValue)
}

fn serialize_struct_variant(
self,
_: &'static str,
_: u32,
_: &'static str,
_: usize,
) -> Result<Self::SerializeStructVariant> {
Err(Error::ExpectedRawValue)
}
}
4 changes: 4 additions & 0 deletions src/value.rs → src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ use serde_derive::{Deserialize, Serialize};

use crate::{de::Error, error::Result};

pub(crate) mod raw;

pub use raw::RawValue;

/// A [`Value`] to [`Value`] map.
///
/// This structure either uses a [BTreeMap](std::collections::BTreeMap) or the
Expand Down
Loading

0 comments on commit 4116e77

Please sign in to comment.