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

Implemented ron::value::RawValue #407

Merged
merged 4 commits into from
Sep 12, 2022
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- 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))
- Fix issue [#401](https://github.com/ron-rs/ron/issues/401) with correct raw struct name identifier parsing ([#402](https://github.com/ron-rs/ron/pull/402))
- Add `ron::value::RawValue` helper type which can (de)serialize any valid RON ([#407](https://github.com/ron-rs/ron/pull/407))

## [0.8.0] - 2022-08-17

Expand Down
15 changes: 15 additions & 0 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,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 @@ -90,6 +90,7 @@ pub enum Error {
},
InvalidIdentifier(String),
SuggestRawIdentifier(String),
ExpectedRawValue,
}

impl fmt::Display for SpannedError {
Expand Down Expand Up @@ -248,6 +249,7 @@ impl fmt::Display for Error {
"Found invalid std identifier `{}`, try the raw identifier `r#{}` instead",
identifier, identifier
),
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 @@ -355,7 +355,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 @@ -9,6 +9,7 @@ use crate::{
parse::{is_ident_first_char, is_ident_other_char, is_ident_raw_char, LargeSInt, LargeUInt},
};

mod raw;
#[cfg(test)]
mod tests;
mod value;
Expand Down Expand Up @@ -533,6 +534,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 @@ -14,6 +14,10 @@ use serde::{

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