Skip to content

Commit

Permalink
add test for enum variants, clean up Extensions::from_ident code
Browse files Browse the repository at this point in the history
  • Loading branch information
sylv256 committed Jan 6, 2024
1 parent 2d99d9e commit 90de04f
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 26 deletions.
14 changes: 7 additions & 7 deletions src/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@ bitflags::bitflags! {
// GRCOV_EXCL_STOP

impl Extensions {
/// Creates an extension flag from an ident.
/// Creates an extension flag from an idents.
#[must_use]
pub fn from_ident(ident: &str) -> Option<Extensions> {
match ident {
"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,
for (name, extension) in Extensions::all().iter_names() {
if ident == name.to_lowercase() {
return Some(extension);
}
}

None
}
}

Expand Down
16 changes: 3 additions & 13 deletions src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,20 +415,10 @@ impl<W: fmt::Write> Serializer<W> {

let non_default_extensions = !options.default_extensions;

if (non_default_extensions & conf.extensions).contains(Extensions::IMPLICIT_SOME) {
writer.write_str("#![enable(implicit_some)]")?;
for (extension_name, _) in (non_default_extensions & conf.extensions).iter_names() {
write!(writer, "#![enable({})]", extension_name.to_lowercase())?;
writer.write_str(&conf.new_line)?;
};
if (non_default_extensions & conf.extensions).contains(Extensions::UNWRAP_NEWTYPES) {
writer.write_str("#![enable(unwrap_newtypes)]")?;
writer.write_str(&conf.new_line)?;
};
if (non_default_extensions & conf.extensions)
.contains(Extensions::UNWRAP_VARIANT_NEWTYPES)
{
writer.write_str("#![enable(unwrap_variant_newtypes)]")?;
writer.write_str(&conf.new_line)?;
};
}
};
Ok(Serializer {
output: writer,
Expand Down
45 changes: 40 additions & 5 deletions tests/522_explicit_struct_names.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,32 @@
use ron::{extensions::Extensions, from_str, Error, Options};
use serde_derive::Deserialize;
use ron::{
extensions::Extensions,
from_str,
ser::{to_string_pretty, PrettyConfig},
Error, Options,
};
use serde_derive::{Deserialize, Serialize};

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

#[derive(Debug, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
struct Position(f32, f32);

#[derive(Debug, Deserialize)]
#[derive(Debug, Serialize, Deserialize)]
enum Query {
None,
Creature(Id),
Location(Position),
}

#[derive(Debug, 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 =
Expand All @@ -21,11 +35,17 @@ const EXPECT_ERROR_MESSAGE: &'static str =
#[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!(
Expand All @@ -37,6 +57,7 @@ fn explicit_struct_names() {
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!(
Expand All @@ -48,6 +69,7 @@ fn explicit_struct_names() {
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!(
Expand All @@ -59,4 +81,17 @@ fn explicit_struct_names() {
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);
let content = to_string_pretty(&foo_ser, pretty_config).unwrap();
assert_eq!(
content,
r#"#![enable(explicit_struct_names)]
Foo(
id: Id(3),
position: Position(0.0, 8.72),
query: Creature(Id(4)),
)"#
);
}
2 changes: 1 addition & 1 deletion tests/non_string_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ macro_rules! test_tag {
}
}

#[derive(Debug)]
#[derive(Debug)] // GRCOV_EXCL_LINE
struct InternallyTagged;

impl<'de> Deserialize<'de> for InternallyTagged {
Expand Down

0 comments on commit 90de04f

Please sign in to comment.