Skip to content

Commit

Permalink
Fixed ron-rs#367 with eager implicit some
Browse files Browse the repository at this point in the history
  • Loading branch information
juntyr committed Feb 24, 2022
1 parent e542eff commit dbb1c04
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Bump MSRV to 1.46.0 ([#361](https://github.com/ron-rs/ron/pull/361))
- Fix issue [#337](https://github.com/ron-rs/ron/issues/337) by removing `decimal_floats` PrettyConfig option and unconditional decimals in floats ([#363](https://github.com/ron-rs/ron/pull/363))
- Fix issue [#203](https://github.com/ron-rs/ron/issues/203) with full de error positioning ([#356](https://github.com/ron-rs/ron/pull/356))
- Fix issue [#367](https://github.com/ron-rs/ron/issues/367) with eager implicit some ([#368](https://github.com/ron-rs/ron/pull/368))

## [0.7.0] - 2021-10-22

Expand Down
11 changes: 10 additions & 1 deletion docs/extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ You can add this extension by adding the following attribute at the top of your

`#![enable(implicit_some)]`

This feature enables RON to automatically convert any value to `Some(value)` if the deserialized struct requires it.
This feature enables RON to automatically convert any value to `Some(value)` if the deserialized type requires it.

```rust
struct Object {
Expand All @@ -66,6 +66,15 @@ Enabling the feature would automatically infer `Some(x)` if `x` is given. In thi
)
```

With this extension enabled, explicitly given `None` and `Some(..)` will be matched eagerly on `Option<Option<Option<u32>>>`, i.e.
* `5` -> `Some(Some(Some(5)))`
* `None` -> `None`
* `Some(5)` -> `Some(Some(Some(5)))`
* `Some(None)` -> `Some(None)`
* `Some(Some(5))` -> `Some(Some(Some(5)))`
* `Some(Some(None))` -> `Some(Some(None))`
* `Some(Some(Some(5)))` -> `Some(Some(Some(5)))`

# unwrap_variant_newtypes

You can add this extension by adding the following attribute at the top of your RON document:
Expand Down
4 changes: 2 additions & 2 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,6 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
{
if self.bytes.consume("None") {
visitor.visit_none()
} else if self.bytes.exts.contains(Extensions::IMPLICIT_SOME) {
visitor.visit_some(&mut *self)
} else if self.bytes.consume("Some") && {
self.bytes.skip_ws()?;
self.bytes.consume("(")
Expand All @@ -366,6 +364,8 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
} else {
Err(Error::ExpectedOptionEnd)
}
} else if self.bytes.exts.contains(Extensions::IMPLICIT_SOME) {
visitor.visit_some(&mut *self)
} else {
Err(Error::ExpectedOption)
}
Expand Down
108 changes: 108 additions & 0 deletions tests/367_implicit_some.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
#[derive(Debug, PartialEq, serde::Deserialize, serde::Serialize)]
struct MaybeFields {
f1: i64,
f2: Option<i64>,
f3: Option<Option<i64>>,
}

#[test]
fn test_recursive_implicit_some() {
// Test case provided by d86leader in
// https://github.com/ron-rs/ron/issues/367#issue-1147920589

let x1: std::result::Result<MaybeFields, _> =
ron::from_str("#![enable(implicit_some)]\n(f1: 1)");
let x2: std::result::Result<MaybeFields, _> =
ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: None, f3: None)");
let x3: std::result::Result<MaybeFields, _> =
ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: 3)");
let x4: std::result::Result<MaybeFields, _> =
ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: Some(3))");
let x5: std::result::Result<MaybeFields, _> =
ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: Some(Some(3)))");
let x6: std::result::Result<MaybeFields, _> =
ron::from_str("#![enable(implicit_some)]\n(f1: 1, f2: 2, f3: Some(None))");

assert_eq!(
x1,
Ok(MaybeFields {
f1: 1,
f2: None,
f3: None
})
);
assert_eq!(
x2,
Ok(MaybeFields {
f1: 1,
f2: None,
f3: None
})
);
assert_eq!(
x3,
Ok(MaybeFields {
f1: 1,
f2: Some(2),
f3: Some(Some(3))
})
);
assert_eq!(
x4,
Ok(MaybeFields {
f1: 1,
f2: Some(2),
f3: Some(Some(3))
})
);
assert_eq!(
x5,
Ok(MaybeFields {
f1: 1,
f2: Some(2),
f3: Some(Some(3))
})
);
assert_eq!(
x6,
Ok(MaybeFields {
f1: 1,
f2: Some(2),
f3: Some(None)
})
);
}

#[test]
fn test_nested_implicit_some() {
assert_eq!(
ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\n5"),
Ok(Some(Some(Some(5))))
);
assert_eq!(
ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nNone"),
Ok(None)
);
assert_eq!(
ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(5)"),
Ok(Some(Some(Some(5))))
);
assert_eq!(
ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(None)"),
Ok(Some(None))
);
assert_eq!(
ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(Some(5))"),
Ok(Some(Some(Some(5))))
);
assert_eq!(
ron::from_str::<Option<Option<Option<u32>>>>("#![enable(implicit_some)]\nSome(Some(None))"),
Ok(Some(Some(None)))
);
assert_eq!(
ron::from_str::<Option<Option<Option<u32>>>>(
"#![enable(implicit_some)]\nSome(Some(Some(5)))"
),
Ok(Some(Some(Some(5))))
);
}

0 comments on commit dbb1c04

Please sign in to comment.