Lines
100 %
Functions
24.39 %
Branches
use std::collections::HashMap;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, PartialEq, Eq, Debug)]
struct Main {
#[serde(flatten)]
required: Required,
optional: Optional,
some_other_field: u32,
}
struct Required {
first: u32,
second: u32,
struct Optional {
third: Option<u32>,
struct MyType {
everything_else: HashMap<String, ron::Value>,
struct AllOptional {
enum Newtype {
Main(Main),
MyType(MyType),
AllOptional(AllOptional),
#[test]
fn test_flatten_struct_into_struct() {
let val = Main {
required: Required {
first: 1,
second: 2,
},
optional: Optional { third: Some(3) },
some_other_field: 1337,
};
let ron = ron::ser::to_string_pretty(&val, ron::ser::PrettyConfig::default()).unwrap();
assert_eq!(
ron,
"{
\"first\": 1,
\"second\": 2,
\"third\": Some(3),
\"some_other_field\": 1337,
}"
);
let de: Main = ron::from_str(&ron).unwrap();
assert_eq!(de, val);
let val = Newtype::Main(Main {
});
let ron = ron::ser::to_string_pretty(
&val,
ron::ser::PrettyConfig::default()
.extensions(ron::extensions::Extensions::UNWRAP_VARIANT_NEWTYPES),
)
.unwrap();
"#![enable(unwrap_variant_newtypes)]
Main({
})"
let de: Newtype = ron::from_str(&ron).unwrap();
ron::from_str::<Main>(
first\": 1,
),
Err(ron::error::SpannedError {
code: ron::error::Error::ExpectedString,
position: ron::error::Position { line: 2, col: 9 },
})
\"second: 2,
code: ron::error::Error::ExpectedMapColon,
position: ron::error::Position { line: 4, col: 10 },
third\": Some(3),
position: ron::error::Position { line: 4, col: 9 },
\"some_other_field: 1337,
code: ron::error::Error::ExpectedStringEnd,
position: ron::error::Position { line: 5, col: 10 },
fn test_flatten_rest() {
let val = MyType {
everything_else: {
let mut map = HashMap::new();
map.insert(
String::from("third"),
ron::Value::Number(ron::value::Number::U8(3)),
map
\"third\": 3,
let de: MyType = ron::from_str(&ron).unwrap();
let val = Newtype::MyType(MyType {
MyType({
ron::from_str::<MyType>(
third\": 3,
\"third: 3,
fn test_flatten_only_rest() {
let val = AllOptional {
everything_else: HashMap::new(),
let ron = ron::ser::to_string(&val).unwrap();
assert_eq!(ron, "{}");
let de: AllOptional = ron::from_str(&ron).unwrap();
let val = Newtype::AllOptional(AllOptional {
AllOptional({
#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct AvailableCards {
pub left: u8,
pub right: u8,
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
struct MapProperties {
cards: AvailableCards,
fn test_issue_456() {
let map_properties = MapProperties {
cards: AvailableCards {
..Default::default()
let ron = ron::to_string(&map_properties).unwrap();
let de: MapProperties = ron::from_str(&ron).unwrap();
assert_eq!(map_properties, de);