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

Support Enum numbered variant parsing #102

Merged
merged 1 commit into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#[derive(Debug, Clone)]
pub struct Attribute {
pub name: String,

Check warning on line 19 in derive/src/parse.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `name` is never read

Check warning on line 19 in derive/src/parse.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `name` is never read

Check warning on line 19 in derive/src/parse.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `name` is never read

Check warning on line 19 in derive/src/parse.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `name` is never read
pub tokens: Vec<String>,
}

Expand All @@ -37,7 +37,7 @@
#[derive(Debug, Clone)]
pub struct Field {
pub attributes: Vec<Attribute>,
pub vis: Visibility,

Check warning on line 40 in derive/src/parse.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `vis` is never read

Check warning on line 40 in derive/src/parse.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `vis` is never read

Check warning on line 40 in derive/src/parse.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `vis` is never read

Check warning on line 40 in derive/src/parse.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `vis` is never read
pub field_name: Option<String>,
pub ty: Type,
}
Expand Down Expand Up @@ -1232,6 +1232,12 @@
break;
}

if next_exact_punct(&mut body, "=").is_some() {
body.next();
let _maybe_coma = next_exact_punct(&mut body, ",");
continue;
}

let attributes = next_attributes_list(&mut body);

let variant_name = next_ident(&mut body).expect("Unnamed variants are not supported");
Expand Down Expand Up @@ -1260,6 +1266,7 @@
vis: Visibility::Public,
});
}

let _maybe_semicolon = next_exact_punct(&mut body, ";");
let _maybe_coma = next_exact_punct(&mut body, ",");
}
Expand Down
31 changes: 31 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
fn de_illegal_inline_comment() {
#[derive(DeJson)]
pub struct Test {
pub a: f32,

Check warning on line 74 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 74 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 74 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 74 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read
}

let jsons = vec![
Expand All @@ -94,7 +94,7 @@
fn de_illegal_multiline_comment() {
#[derive(DeJson)]
pub struct Test {
pub a: f32,

Check warning on line 97 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 97 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 97 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 97 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read
}

let jsons = vec![
Expand Down Expand Up @@ -631,6 +631,37 @@

#[test]
fn de_ser_enum() {
#[derive(DeJson, SerJson, PartialEq, Debug)]
pub enum Fud {
A = 0,
B = 1,
C = 2,
}

#[derive(DeJson, SerJson, PartialEq, Debug)]
pub struct Bar {
foo1: Fud,
foo2: Fud,
foo3: Fud,
}

let json = "{\"foo1\":\"A\",\"foo2\":\"B\",\"foo3\":\"C\"}";

let data = Bar {
foo1: Fud::A,
foo2: Fud::B,
foo3: Fud::C,
};

let serialized = SerJson::serialize_json(&data);
assert_eq!(serialized, json);

let deserialized: Bar = DeJson::deserialize_json(&serialized).unwrap();
assert_eq!(deserialized, data);
}

#[test]
fn de_ser_enum_complex() {
#[derive(DeJson, SerJson, PartialEq, Debug)]
pub enum Foo {
A,
Expand Down
64 changes: 64 additions & 0 deletions tests/ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,70 @@ fn de_enum() {
);
}

#[test]
fn de_ser_enum() {
#[derive(SerRon, DeRon, PartialEq, Debug)]
pub enum Fud {
A = 0,
B = 1,
C = 2,
}

#[derive(SerRon, DeRon, PartialEq, Debug)]
pub struct Bar {
foo1: Fud,
foo2: Fud,
foo3: Fud,
}

let ron = "(\n foo1:A,\n foo2:B,\n foo3:C,\n)";

let data = Bar {
foo1: Fud::A,
foo2: Fud::B,
foo3: Fud::C,
};
let serialized = SerRon::serialize_ron(&data);

assert_eq!(serialized, ron);

let deserialized: Bar = DeRon::deserialize_ron(&serialized).unwrap();
assert_eq!(deserialized, data);
}

#[test]
fn ser_enum_complex() {
#[derive(SerRon, DeRon, PartialEq, Debug)]
pub enum Foo {
A,
B(i32, String),
C { a: i32, b: String },
}

#[derive(SerRon, DeRon, PartialEq, Debug)]
pub struct Bar {
foo1: Foo,
foo2: Foo,
foo3: Foo,
}

let ron = "(\n foo1:A,\n foo2:B(1, \"asd\"),\n foo3:C(\n a:2,\n b:\"qwe\",\n ),\n)";

let data = Bar {
foo1: Foo::A,
foo2: Foo::B(1, String::from("asd")),
foo3: Foo::C {
a: 2,
b: String::from("qwe"),
},
};
let serialized = SerRon::serialize_ron(&data);
assert_eq!(serialized, ron);

let deserialized: Bar = DeRon::deserialize_ron(&serialized).unwrap();
assert_eq!(deserialized, data);
}

#[test]
fn test_various_escapes() {
let ron = r#""\n\t\u0020\f\b\\\"\/\ud83d\uDE0B\r""#;
Expand Down
Loading