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

Fix newtype variant unwrapping inside enum, seq and map #331

Merged
merged 2 commits into from
Nov 2, 2021
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 @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased
- Add `struct_names` option to `PrettyConfig`
- Fix newtype variant unwrapping around enum, seq and map ([#331](https://github.com/ron-rs/ron/pull/331))

## [0.7.0] - 2021-10-22

Expand Down
10 changes: 10 additions & 0 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
where
V: Visitor<'de>,
{
// Newtype variants can only be unwrapped if we receive information
// about the wrapped type - with `deserialize_any` we don't
self.newtype_variant = false;

if self.bytes.consume_ident("true") {
return visitor.visit_bool(true);
} else if self.bytes.consume_ident("false") {
Expand Down Expand Up @@ -414,6 +418,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
where
V: Visitor<'de>,
{
self.newtype_variant = false;

if self.bytes.consume("[") {
let value = visitor.visit_seq(CommaSeparated::new(b']', &mut self))?;
self.bytes.comma()?;
Expand Down Expand Up @@ -469,6 +475,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
where
V: Visitor<'de>,
{
self.newtype_variant = false;

if self.bytes.consume("{") {
let value = visitor.visit_map(CommaSeparated::new(b'}', &mut self))?;
self.bytes.comma()?;
Expand Down Expand Up @@ -524,6 +532,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
where
V: Visitor<'de>,
{
self.newtype_variant = false;

visitor.visit_enum(Enum::new(self))
}

Expand Down
98 changes: 98 additions & 0 deletions tests/250_variant_newtypes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use ron::{de::from_str, error::ErrorCode};
use serde::Deserialize;

Expand All @@ -13,6 +15,9 @@ enum TestEnum {
TupleNewtypeTupleStruct(TupleStruct),
TupleNewtypeStruct(Struct),
TupleNewtypeEnum(Enum),
TupleNewtypeOption(Option<Struct>),
TupleNewtypeSeq(Vec<Struct>),
TupleNewtypeMap(HashMap<u32, Struct>),
}

#[derive(Debug, Deserialize, Eq, PartialEq)]
Expand All @@ -34,6 +39,8 @@ struct Struct {
enum Enum {
A,
B(Struct),
C(u32, bool),
D { a: u32, b: bool },
}

#[test]
Expand Down Expand Up @@ -181,4 +188,95 @@ fn test_deserialise_tuple_newtypes() {
.unwrap(),
TestEnum::TupleNewtypeEnum(Enum::B(Struct { a: 4, b: false })),
);
assert_eq!(
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(C 4, false)"#)
.unwrap_err()
.code,
ErrorCode::ExpectedArray,
);
assert_eq!(
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(C(4, false))"#
)
.unwrap(),
TestEnum::TupleNewtypeEnum(Enum::C(4, false)),
);
assert_eq!(
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(D a: 4, b: false)"#
)
.unwrap_err()
.code,
ErrorCode::ExpectedStruct,
);
assert_eq!(
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeEnum(D(a: 4, b: false))"#
)
.unwrap(),
TestEnum::TupleNewtypeEnum(Enum::D { a: 4, b: false }),
);

assert_eq!(
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(None)"#)
.unwrap(),
TestEnum::TupleNewtypeOption(None),
);
assert_eq!(
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(Some(a: 4, b: false))"#
)
.unwrap(),
TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })),
);
assert_eq!(
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeOption(a: 4, b: false)"#
)
.unwrap_err()
.code,
ErrorCode::ExpectedOption,
);
assert_eq!(
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes, implicit_some)] TupleNewtypeOption(a: 4, b: false)"#).unwrap(),
TestEnum::TupleNewtypeOption(Some(Struct { a: 4, b: false })),
);

assert_eq!(
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([])"#).unwrap(),
TestEnum::TupleNewtypeSeq(vec![]),
);
assert_eq!(
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([(a: 4, b: false)])"#
)
.unwrap(),
TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]),
);
assert_eq!(
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeSeq([Struct(a: 4, b: false)])"#
)
.unwrap(),
TestEnum::TupleNewtypeSeq(vec![Struct { a: 4, b: false }]),
);

assert_eq!(
from_str::<TestEnum>(r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({})"#).unwrap(),
TestEnum::TupleNewtypeMap(vec![].into_iter().collect()),
);
assert_eq!(
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({2: (a: 4, b: false)})"#
)
.unwrap(),
TestEnum::TupleNewtypeMap(vec![(2, Struct { a: 4, b: false })].into_iter().collect()),
);
assert_eq!(
from_str::<TestEnum>(
r#"#![enable(unwrap_variant_newtypes)] TupleNewtypeMap({8: Struct(a: 4, b: false)})"#
)
.unwrap(),
TestEnum::TupleNewtypeMap(vec![(8, Struct { a: 4, b: false })].into_iter().collect()),
);
}