Skip to content

Commit

Permalink
Add #526 as a test (#527)
Browse files Browse the repository at this point in the history
* Add #526 as a test

Co-authored-by: porkbrain <[email protected]>

* Fix indemap deprecated API usage

---------

Co-authored-by: porkbrain <[email protected]>
  • Loading branch information
juntyr and porkbrain committed Mar 5, 2024
1 parent d3e9ebe commit d02baeb
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/value/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,14 @@ impl Map {

/// Removes an element by its `key`.
pub fn remove(&mut self, key: &Value) -> Option<Value> {
self.0.remove(key)
#[cfg(feature = "indexmap")]
{
self.0.shift_remove(key)
}
#[cfg(not(feature = "indexmap"))]
{
self.0.remove(key)
}
}

/// Iterate all key-value pairs.
Expand Down
54 changes: 54 additions & 0 deletions tests/526_flatten.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct SomeCollection {
inner: Vec<SomeItem>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct SomeItem {
#[serde(flatten)]
foo: Foo,
#[serde(flatten)]
bar: Bar,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Bar {
name: String,
some_enum: Option<SomeEnum>,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
struct Foo {
something: String,
}

#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
enum SomeEnum {
A,
B,
}

#[test]
fn roundtrip() {
let scene = SomeCollection {
inner: vec![SomeItem {
foo: Foo {
something: "something".to_string(),
},
bar: Bar {
name: "name".to_string(),
some_enum: Some(SomeEnum::A),
},
}],
};

let ron = ron::ser::to_string(&scene).unwrap();
let de: SomeCollection = ron::de::from_str(&ron).unwrap();
assert_eq!(de, scene);

let ron = ron::ser::to_string_pretty(&scene, Default::default()).unwrap();
let _deser_scene: SomeCollection = ron::de::from_str(&ron).unwrap();
assert_eq!(de, scene);
}

0 comments on commit d02baeb

Please sign in to comment.