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

Add explicit_struct_names Extension #522

Merged
merged 27 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
521d91c
prepare adding `explicit_struct_names` extension
sylv256 Jan 3, 2024
35b973e
add error handling, prepare to implement it, prepare to add test
sylv256 Jan 3, 2024
f372298
implement extension, add test
sylv256 Jan 4, 2024
cf7710e
update CHANGELOG.md
sylv256 Jan 4, 2024
8605b97
add documentation, improve comments
sylv256 Jan 4, 2024
5f29b8c
add example using the Options method
sylv256 Jan 4, 2024
c2fdc0a
fix wording in ExpectedStructName error message
sylv256 Jan 4, 2024
c7c4a8a
add tuple struct test, use all codepaths
sylv256 Jan 4, 2024
ef7f6cc
Merge branch 'master' of https://github.com/SylvKT/ron
sylv256 Jan 4, 2024
6dabe55
in test: rename each foo variable to correspond with their phase (for…
sylv256 Jan 4, 2024
d59f672
fix extensions test
sylv256 Jan 4, 2024
b620b3f
add check for Error::ExpectedStructName message, suppress unnecessary…
sylv256 Jan 4, 2024
ec5f527
resolve redundant else block
sylv256 Jan 4, 2024
3f584af
add "see also" blurbs, OR the `explicit_struct_names` value in `Prett…
sylv256 Jan 4, 2024
1057138
fix formatting
sylv256 Jan 4, 2024
00a65cd
fix coverage
sylv256 Jan 4, 2024
966675a
move OR operation to Serializer::struct_names
sylv256 Jan 5, 2024
08ebb5d
fix pretty print in Error::ExpectedStructName message
sylv256 Jan 5, 2024
8add0cc
fix Serializer::struct_names
sylv256 Jan 5, 2024
17cb912
add trunk config files to .gitignore (for those who use it)
sylv256 Jan 6, 2024
2d99d9e
add .DS_Store to the .gitignore (you'll thank me later)
sylv256 Jan 6, 2024
90de04f
add test for enum variants, clean up `Extensions::from_ident` code
sylv256 Jan 6, 2024
7283d7b
Fix grammar error in extensions.rs
sylv256 Jan 6, 2024
313b30f
add `unwrap_variant_newtypes` to test
sylv256 Jan 6, 2024
45204f4
Merge branch 'master' of https://github.com/SylvKT/ron
sylv256 Jan 6, 2024
94fabf3
add to phase 5 a deserialize test
sylv256 Jan 6, 2024
4bf6ef7
add GRCOV_EXCL
sylv256 Jan 6, 2024
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 @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Allow `ron::value::RawValue` to capture any whitespace to the left and right of a ron value ([#487](https://github.com/ron-rs/ron/pull/487))
- Breaking: Enforce that ron always writes valid UTF-8 ([#488](https://github.com/ron-rs/ron/pull/488))
- Add convenient `Value::from` impls ([#498](https://github.com/ron-rs/ron/pull/498))
- Add new extension `explicit_struct_names` which requires that struct names are included during deserialization ([#522](https://github.com/ron-rs/ron/pull/522))

### Format Changes

Expand Down
2 changes: 2 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ pub enum Error {
SuggestRawIdentifier(String),
ExpectedRawValue,
ExceededRecursionLimit,
ExpectedStructName(String),
}

impl fmt::Display for SpannedError {
Expand Down Expand Up @@ -281,6 +282,7 @@ impl fmt::Display for Error {
"Exceeded recursion limit, try increasing `ron::Options::recursion_limit` \
and using `serde_stacker` to protect against a stack overflow",
),
Error::ExpectedStructName(ref name) => write!(f, "Expected a struct name `{}`, but none was found", name),
sylv256 marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ bitflags::bitflags! {
const UNWRAP_NEWTYPES = 0x1;
const IMPLICIT_SOME = 0x2;
const UNWRAP_VARIANT_NEWTYPES = 0x4;
/// During deserialization, this extension requires that structs' names are stated explicitly.
const EXPLICIT_STRUCT_NAMES = 0x8;
}
}
// GRCOV_EXCL_STOP
Expand All @@ -19,6 +21,7 @@ impl Extensions {
"unwrap_newtypes" => Some(Extensions::UNWRAP_NEWTYPES),
"implicit_some" => Some(Extensions::IMPLICIT_SOME),
"unwrap_variant_newtypes" => Some(Extensions::UNWRAP_VARIANT_NEWTYPES),
"explicit_struct_names" => Some(Extensions::EXPLICIT_STRUCT_NAMES),
_ => None,
}
}
Expand All @@ -40,19 +43,25 @@ mod tests {
assert_eq!(ext, ext2);
}

// todo: maybe make a macro for this?
#[test]
fn test_extension_serde() {
roundtrip_extensions(Extensions::default());
roundtrip_extensions(Extensions::UNWRAP_NEWTYPES);
roundtrip_extensions(Extensions::IMPLICIT_SOME);
roundtrip_extensions(Extensions::UNWRAP_VARIANT_NEWTYPES);
roundtrip_extensions(Extensions::EXPLICIT_STRUCT_NAMES);
roundtrip_extensions(Extensions::UNWRAP_NEWTYPES | Extensions::IMPLICIT_SOME);
roundtrip_extensions(Extensions::UNWRAP_NEWTYPES | Extensions::UNWRAP_VARIANT_NEWTYPES);
roundtrip_extensions(Extensions::UNWRAP_NEWTYPES | Extensions::EXPLICIT_STRUCT_NAMES);
roundtrip_extensions(Extensions::IMPLICIT_SOME | Extensions::UNWRAP_VARIANT_NEWTYPES);
roundtrip_extensions(Extensions::IMPLICIT_SOME | Extensions::EXPLICIT_STRUCT_NAMES);
roundtrip_extensions(Extensions::UNWRAP_VARIANT_NEWTYPES | Extensions::EXPLICIT_STRUCT_NAMES);
roundtrip_extensions(
Extensions::UNWRAP_NEWTYPES
| Extensions::IMPLICIT_SOME
| Extensions::UNWRAP_VARIANT_NEWTYPES,
| Extensions::UNWRAP_VARIANT_NEWTYPES
| Extensions::EXPLICIT_STRUCT_NAMES,
sylv256 marked this conversation as resolved.
Show resolved Hide resolved
);
}
}
6 changes: 5 additions & 1 deletion src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,11 @@ impl<'a> Parser<'a> {

pub fn consume_struct_name(&mut self, ident: &'static str) -> Result<bool> {
if self.check_ident("") {
return Ok(false);
if self.exts.contains(Extensions::EXPLICIT_STRUCT_NAMES) {
return Err(Error::ExpectedStructName(ident.to_string()))
juntyr marked this conversation as resolved.
Show resolved Hide resolved
} else {
return Ok(false);
}
}

let found_ident = match self.identifier() {
Expand Down
43 changes: 43 additions & 0 deletions tests/522_explicit_struct_names.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use ron::{Options, extensions::Extensions, Error, from_str};
use serde_derive::Deserialize;

#[derive(Debug, Deserialize)]
struct Id(u32);

#[derive(Debug, Deserialize)]
struct Foo {
#[allow(unused)]
pub id: Id,
}

const EXPECT_ERROR_MESSAGE: &'static str = "expected `Err(Error::ExpectedStructName)`, deserializer returned `Ok`";
const INCORRECT_ERROR_MESSAGE: &'static str = "expected error ExpectedStructName, found";

#[test]
fn explicit_struct_names() {
let options = Options::default()
.with_default_extension(Extensions::EXPLICIT_STRUCT_NAMES);

// phase 1
let content = r#"(
id: Id(3),
)"#;
let foo = options.from_str::<Foo>(content);
match foo.expect_err(EXPECT_ERROR_MESSAGE).code {
Error::ExpectedStructName(_) => {},
err => panic!("{INCORRECT_ERROR_MESSAGE} \"{err}\""),
}

// phase 2
let content = r#"Foo(
id: (3),
)"#;
let foo = options.from_str::<Foo>(content);
match foo.expect_err(EXPECT_ERROR_MESSAGE).code {
Error::ExpectedStructName(_) => {},
err => panic!("{INCORRECT_ERROR_MESSAGE} \"{err}\""),
}

// phase 3 (use content from phase 2)
let _foo = from_str::<Foo>(content).unwrap();
sylv256 marked this conversation as resolved.
Show resolved Hide resolved
}