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

Stop Pascalizing Enum Cases #162

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
12 changes: 7 additions & 5 deletions xsd-parser/src/generator/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub trait EnumGenerator {
);

format!(
"{comment}{macros}\n\
"{comment}{macros}\
pub enum {name} {{\n\
{cases}\n\
{indent}__Unknown__({typename}),\n\
Expand Down Expand Up @@ -61,22 +61,24 @@ pub trait EnumGenerator {
}

fn macros(&self, entity: &Enum, gen: &Generator) -> Cow<'static, str> {
let allows = "#[allow(non_camel_case_types, clippy::upper_case_acronyms)]\n";

if entity.source == EnumSource::Union {
return "#[derive(PartialEq, Debug, UtilsUnionSerDe)]".into();
return format!("{allows}#[derive(PartialEq, Debug, UtilsUnionSerDe)]").into();
}

let derives = "#[derive(PartialEq, Debug, YaSerialize, YaDeserialize)]";
let derives = "#[derive(PartialEq, Debug, YaSerialize, YaDeserialize)]\n";
let tns = gen.target_ns.borrow();
match tns.as_ref() {
Some(tn) => match tn.name() {
Some(name) => format!(
"{derives}#[yaserde(prefix = \"{prefix}\", namespace = \"{prefix}: {uri}\")]\n",
"{allows}{derives}#[yaserde(prefix = \"{prefix}\", namespace = \"{prefix}: {uri}\")]\n",
derives = derives,
prefix = name,
uri = tn.uri()
),
None => format!(
"{derives}#[yaserde(namespace = \"{uri}\")]\n",
"{allows}{derives}#[yaserde(namespace = \"{uri}\")]\n",
derives = derives,
uri = tn.uri()
),
Expand Down
12 changes: 5 additions & 7 deletions xsd-parser/src/generator/enum_case.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::{
generator::{default::default_format_type, utils::split_name, Generator},
generator::{utils::split_name, Generator},
parser::types::{EnumCase, EnumSource},
};

use super::utils::{filter_type_name, sanitize};

pub trait EnumCaseGenerator {
fn generate(&self, entity: &EnumCase, gen: &Generator) -> String {
let typename = if entity.type_name.is_some() {
Expand All @@ -20,12 +22,8 @@ pub trait EnumCaseGenerator {
)
}

fn get_name(&self, entity: &EnumCase, gen: &Generator) -> String {
default_format_type(entity.name.as_str(), &gen.target_ns.borrow())
.split("::")
.last()
.unwrap()
.to_string()
fn get_name(&self, entity: &EnumCase, _: &Generator) -> String {
sanitize(filter_type_name(entity.name.split(':').last().unwrap_or(&entity.name.to_owned())))
}

fn get_type_name(&self, entity: &EnumCase, gen: &Generator) -> String {
Expand Down
27 changes: 20 additions & 7 deletions xsd-parser/tests/enumeration/expected.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
#[derive(PartialEq, Debug, YaSerialize, YaDeserialize)]
#[yaserde(prefix = "tns", namespace = "tns: http://example.com")]
pub enum FooType {
#[yaserde(rename = "OFF")]
Off,
#[yaserde(rename = "ON")]
On,
#[yaserde(rename = "AUTO")]
Auto,
OFF,
ON,
AUTO,
__Unknown__(String),
}

Expand All @@ -17,9 +15,24 @@ impl Default for FooType {
}
impl Validate for FooType {}

#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
#[derive(PartialEq, Debug, YaSerialize, YaDeserialize)]
#[yaserde(prefix = "tns", namespace = "tns: http://example.com")]
pub enum FooType1 {
OFF,
ON,
On,
__Unknown__(String),
}

impl Default for FooType1 {
fn default() -> FooType1 {
Self::__Unknown__("No valid variants".into())
}
}
impl Validate for FooType1 {}

#[derive(Default, PartialEq, Debug, UtilsTupleIo, UtilsDefaultSerde)]
pub struct FooType2(pub String);

impl Validate for FooType2 {}

8 changes: 8 additions & 0 deletions xsd-parser/tests/enumeration/input.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="FooType1">
<xs:restriction base="xs:string">
<xs:enumeration value="OFF"/>
<xs:enumeration value="ON"/>
<xs:enumeration value="On"/>
</xs:restriction>
</xs:simpleType>

<xs:simpleType name="FooType2">
<xs:restriction base="xs:string">
<xs:enumeration value="xs:OFF"/>
Expand Down
2 changes: 1 addition & 1 deletion xsd-parser/tests/enumeration/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn deserialization_works() {

let de: expected::FooType = yaserde::de::from_str(ser).unwrap();

assert_eq!(de, expected::FooType::Auto);
assert_eq!(de, expected::FooType::AUTO);
}

#[test]
Expand Down
5 changes: 3 additions & 2 deletions xsd-parser/tests/union/expected.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#[allow(non_camel_case_types, clippy::upper_case_acronyms)]
#[derive(PartialEq, Debug, UtilsUnionSerDe)]
pub enum FooType {
Int(i32),
String(String),
int(i32),
string(String),
__Unknown__(String),
}

Expand Down
2 changes: 1 addition & 1 deletion xsd-parser/tests/union/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn deserialization_works() {

let de: expected::FooType = yaserde::de::from_str(ser).unwrap();

assert_eq!(de, expected::FooType::String("string".to_string()));
assert_eq!(de, expected::FooType::string("string".to_string()));
}

#[test]
Expand Down