-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
explicit_struct_names
Extension (#522)
* prepare adding `explicit_struct_names` extension * add error handling, prepare to implement it, prepare to add test * implement extension, add test * update CHANGELOG.md * add documentation, improve comments * add example using the Options method * fix wording in ExpectedStructName error message Co-authored-by: Juniper Tyree <[email protected]> * add tuple struct test, use all codepaths * in test: rename each foo variable to correspond with their phase (formatting) * fix extensions test * add check for Error::ExpectedStructName message, suppress unnecessary compiler warning * resolve redundant else block * add "see also" blurbs, OR the `explicit_struct_names` value in `PrettyConfig::struct_names` * fix formatting * fix coverage * move OR operation to Serializer::struct_names * fix pretty print in Error::ExpectedStructName message * fix Serializer::struct_names * add trunk config files to .gitignore (for those who use it) * add .DS_Store to the .gitignore (you'll thank me later) * add test for enum variants, clean up `Extensions::from_ident` code * Fix grammar error in extensions.rs Co-authored-by: Juniper Tyree <[email protected]> * add `unwrap_variant_newtypes` to test * add to phase 5 a deserialize test * add GRCOV_EXCL --------- Co-authored-by: Juniper Tyree <[email protected]>
- Loading branch information
Showing
9 changed files
with
208 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,9 @@ | |
# Generated by Cargo | ||
/target/ | ||
/Cargo.lock | ||
|
||
# VSCode Extensions | ||
/.trunk | ||
|
||
# MacOS Shenanigans | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
use ron::{ | ||
extensions::Extensions, | ||
from_str, | ||
ser::{to_string_pretty, PrettyConfig}, | ||
Error, Options, | ||
}; | ||
use serde_derive::{Deserialize, Serialize}; | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize)] | ||
struct Id(u32); | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize)] | ||
struct Position(f32, f32); | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize)] | ||
enum Query { | ||
None, | ||
Creature(Id), | ||
Location(Position), | ||
} | ||
|
||
#[derive(Debug, PartialEq, Serialize, Deserialize)] | ||
struct Foo { | ||
#[allow(unused)] | ||
pub id: Id, | ||
#[allow(unused)] | ||
pub position: Position, | ||
#[allow(unused)] | ||
pub query: Query, | ||
} | ||
|
||
const EXPECT_ERROR_MESSAGE: &'static str = | ||
"expected `Err(Error::ExpectedStructName)`, deserializer returned `Ok`"; | ||
|
||
#[test] | ||
fn explicit_struct_names() { | ||
let options = Options::default().with_default_extension(Extensions::EXPLICIT_STRUCT_NAMES); | ||
let foo_ser = Foo { | ||
id: Id(3), | ||
position: Position(0.0, 8.72), | ||
query: Query::Creature(Id(4)), | ||
}; | ||
|
||
// phase 1 (regular structs) | ||
let content_regular = r#"( | ||
id: Id(3), | ||
position: Position(0.0, 8.72), | ||
query: None, | ||
)"#; | ||
let foo = options.from_str::<Foo>(content_regular); | ||
assert_eq!( | ||
foo.expect_err(EXPECT_ERROR_MESSAGE).code, | ||
Error::ExpectedStructName("Foo".to_string()) | ||
); | ||
|
||
// phase 2 (newtype structs) | ||
let content_newtype = r#"Foo( | ||
id: (3), | ||
position: Position(0.0, 8.72), | ||
query: None, | ||
)"#; | ||
let foo = options.from_str::<Foo>(content_newtype); | ||
assert_eq!( | ||
foo.expect_err(EXPECT_ERROR_MESSAGE).code, | ||
Error::ExpectedStructName("Id".to_string()) | ||
); | ||
|
||
// phase 3 (tuple structs) | ||
let content_tuple = r#"Foo( | ||
id: Id(3), | ||
position: (0.0, 8.72), | ||
query: None, | ||
)"#; | ||
let foo = options.from_str::<Foo>(content_tuple); | ||
assert_eq!( | ||
foo.expect_err(EXPECT_ERROR_MESSAGE).code, | ||
Error::ExpectedStructName("Position".to_string()) | ||
); | ||
|
||
// phase 4 (test without this extension) | ||
let _foo1 = from_str::<Foo>(content_regular).unwrap(); | ||
let _foo2 = from_str::<Foo>(content_newtype).unwrap(); | ||
let _foo3 = from_str::<Foo>(content_tuple).unwrap(); | ||
|
||
// phase 5 (test serialization) | ||
let pretty_config = PrettyConfig::new() | ||
.extensions(Extensions::EXPLICIT_STRUCT_NAMES | Extensions::UNWRAP_VARIANT_NEWTYPES); | ||
let content = to_string_pretty(&foo_ser, pretty_config).unwrap(); | ||
assert_eq!( | ||
content, | ||
r#"#![enable(unwrap_variant_newtypes)] | ||
#![enable(explicit_struct_names)] | ||
Foo( | ||
id: Id(3), | ||
position: Position(0.0, 8.72), | ||
query: Creature(4), | ||
)"# | ||
); | ||
let foo_de = from_str::<Foo>(&content); | ||
match foo_de { | ||
// GRCOV_EXCL_START | ||
Err(err) => panic!( | ||
"failed to deserialize with `explicit_struct_names` and `unwrap_variant_newtypes`: {}", | ||
err | ||
), | ||
// GRCOV_EXCL_STOP | ||
Ok(foo_de) => assert_eq!(foo_de, foo_ser), | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters