Skip to content
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
4 changes: 2 additions & 2 deletions xsd-parser/src/pipeline/generator/data/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl EnumerationMetaVariant {
.ok()
.map(|code| {
let ident = types.naming.format_constant_ident(
&Name::new_named(variant_ident.to_string()),
&Name::new_named(format!("VALUE_{variant_ident}")),
None,
);

Expand All @@ -113,7 +113,7 @@ impl EnumerationMetaVariant {
})
.map(|code| {
let ident = types.naming.format_constant_ident(
&Name::new_named(variant_ident.to_string()),
&Name::new_named(format!("VALUE_{variant_ident}")),
None,
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{borrow::Cow, collections::btree_map::Entry};
use tracing::instrument;
use xsd_parser_types::{misc::Namespace, xml::QName};

use crate::models::meta::{BuildInMeta, MetaType, MetaTypeVariant, MetaTypes};
use crate::models::meta::{BuildInMeta, MetaType, MetaTypeVariant, MetaTypes, ReferenceMeta};
use crate::models::schema::{xs::Schema, NamespaceId, SchemaId, Schemas};
use crate::models::{IdentCache, IdentType, Name, TypeIdent};
use crate::traits::{NameBuilder, NameBuilderExt as _};
Expand Down Expand Up @@ -100,14 +100,16 @@ impl<'state, 'schema> TypeProcessor<'state, 'schema> {
ty if ty.is_mixed(self.types) && ty.is_emptiable(self.types) => {
Some(Cow::Owned(MetaTypeVariant::BuildIn(BuildInMeta::String)))
}
MetaTypeVariant::Custom(_) => Some(Cow::Owned(MetaTypeVariant::Reference(
ReferenceMeta::new(ident.clone()),
))),
MetaTypeVariant::ComplexType(_)
| MetaTypeVariant::All(_)
| MetaTypeVariant::Choice(_)
| MetaTypeVariant::Sequence(_)
| MetaTypeVariant::Dynamic(_) => None,
ty @ (MetaTypeVariant::Enumeration(_)
| MetaTypeVariant::BuildIn(_)
| MetaTypeVariant::Custom(_)
| MetaTypeVariant::Union(_)
| MetaTypeVariant::Reference(_)
| MetaTypeVariant::SimpleType(_)) => Some(Cow::Borrowed(ty)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,9 +434,9 @@ impl<'a, 'state, 'schema> VariantProcessor<'a, 'state, 'schema> {
| MetaTypeVariant::Reference(_)
| MetaTypeVariant::SimpleType(_),
) => (),
Some(MetaTypeVariant::Union(e)) => e.base = Base::Extension(base),
Some(MetaTypeVariant::Enumeration(e)) => e.base = Base::Extension(base),
Some(MetaTypeVariant::ComplexType(e)) => e.base = Base::Extension(base),
Some(MetaTypeVariant::Union(e)) => e.base = Base::Restriction(base),
Some(MetaTypeVariant::Enumeration(e)) => e.base = Base::Restriction(base),
Some(MetaTypeVariant::ComplexType(e)) => e.base = Base::Restriction(base),
e => crate::unreachable!("Unexpected type: {e:#?}"),
}
}
Expand Down
14 changes: 8 additions & 6 deletions xsd-parser/tests/feature/advanced_enums/expected/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ pub enum StringEnumType {
Off,
On,
Auto,
X123,
}
impl StringEnumType {
pub const OFF: &str = "OFF";
pub const ON: &str = "ON";
pub const AUTO: &str = "AUTO";
pub const VALUE_OFF: &str = "OFF";
pub const VALUE_ON: &str = "ON";
pub const VALUE_AUTO: &str = "AUTO";
pub const VALUE_X123: &str = "X123";
}
#[derive(Debug)]
pub enum QNameEnumType {
Expand All @@ -23,17 +25,17 @@ pub enum QNameEnumType {
TnsBaz,
}
impl QNameEnumType {
pub const TNS_FOO: &'static QName = &QName::new_const(
pub const VALUE_TNS_FOO: &'static QName = &QName::new_const(
b"tns:Foo",
Some(3usize),
Some(Namespace::new_const(b"http://example.com")),
);
pub const TNS_BAR: &'static QName = &QName::new_const(
pub const VALUE_TNS_BAR: &'static QName = &QName::new_const(
b"tns:Bar",
Some(3usize),
Some(Namespace::new_const(b"http://example.com")),
);
pub const TNS_BAZ: &'static QName = &QName::new_const(
pub const VALUE_TNS_BAZ: &'static QName = &QName::new_const(
b"tns:Baz",
Some(3usize),
Some(Namespace::new_const(b"http://example.com")),
Expand Down
34 changes: 19 additions & 15 deletions xsd-parser/tests/feature/advanced_enums/expected/quick_xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,21 @@ pub enum StringEnumType {
Off,
On,
Auto,
X123,
}
impl StringEnumType {
pub const OFF: &str = "OFF";
pub const ON: &str = "ON";
pub const AUTO: &str = "AUTO";
pub const VALUE_OFF: &str = "OFF";
pub const VALUE_ON: &str = "ON";
pub const VALUE_AUTO: &str = "AUTO";
pub const VALUE_X123: &str = "X123";
}
impl SerializeBytes for StringEnumType {
fn serialize_bytes(&self, helper: &mut SerializeHelper) -> Result<Option<Cow<'_, str>>, Error> {
match self {
Self::Off => Ok(Some(Cow::Borrowed(Self::OFF))),
Self::On => Ok(Some(Cow::Borrowed(Self::ON))),
Self::Auto => Ok(Some(Cow::Borrowed(Self::AUTO))),
Self::Off => Ok(Some(Cow::Borrowed(Self::VALUE_OFF))),
Self::On => Ok(Some(Cow::Borrowed(Self::VALUE_ON))),
Self::Auto => Ok(Some(Cow::Borrowed(Self::VALUE_AUTO))),
Self::X123 => Ok(Some(Cow::Borrowed(Self::VALUE_X123))),
}
}
}
Expand All @@ -65,6 +68,7 @@ impl DeserializeBytes for StringEnumType {
b"OFF" => Ok(Self::Off),
b"ON" => Ok(Self::On),
b"AUTO" => Ok(Self::Auto),
b"X123" => Ok(Self::X123),
x => Err(Error::from(ErrorKind::UnknownOrInvalidValue(
RawByteStr::from_slice(x),
))),
Expand All @@ -78,17 +82,17 @@ pub enum QNameEnumType {
TnsBaz,
}
impl QNameEnumType {
pub const TNS_FOO: &'static QName = &QName::new_const(
pub const VALUE_TNS_FOO: &'static QName = &QName::new_const(
b"tns:Foo",
Some(3usize),
Some(Namespace::new_const(b"http://example.com")),
);
pub const TNS_BAR: &'static QName = &QName::new_const(
pub const VALUE_TNS_BAR: &'static QName = &QName::new_const(
b"tns:Bar",
Some(3usize),
Some(Namespace::new_const(b"http://example.com")),
);
pub const TNS_BAZ: &'static QName = &QName::new_const(
pub const VALUE_TNS_BAZ: &'static QName = &QName::new_const(
b"tns:Baz",
Some(3usize),
Some(Namespace::new_const(b"http://example.com")),
Expand All @@ -97,23 +101,23 @@ impl QNameEnumType {
impl SerializeBytes for QNameEnumType {
fn serialize_bytes(&self, helper: &mut SerializeHelper) -> Result<Option<Cow<'_, str>>, Error> {
match self {
Self::TnsFoo => Self::TNS_FOO.serialize_bytes(helper),
Self::TnsBar => Self::TNS_BAR.serialize_bytes(helper),
Self::TnsBaz => Self::TNS_BAZ.serialize_bytes(helper),
Self::TnsFoo => Self::VALUE_TNS_FOO.serialize_bytes(helper),
Self::TnsBar => Self::VALUE_TNS_BAR.serialize_bytes(helper),
Self::TnsBaz => Self::VALUE_TNS_BAZ.serialize_bytes(helper),
}
}
}
impl DeserializeBytes for QNameEnumType {
fn deserialize_bytes(helper: &mut DeserializeHelper, bytes: &[u8]) -> Result<Self, Error> {
let x = bytes;
if let Ok(value) = QName::deserialize_bytes(helper, x) {
if value == *Self::TNS_FOO {
if value == *Self::VALUE_TNS_FOO {
return Ok(Self::TnsFoo);
}
if value == *Self::TNS_BAR {
if value == *Self::VALUE_TNS_BAR {
return Ok(Self::TnsBar);
}
if value == *Self::TNS_BAZ {
if value == *Self::VALUE_TNS_BAZ {
return Ok(Self::TnsBaz);
}
}
Expand Down
1 change: 1 addition & 0 deletions xsd-parser/tests/feature/advanced_enums/schema.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<xs:enumeration value="OFF" />
<xs:enumeration value="ON" />
<xs:enumeration value="AUTO" />
<xs:enumeration value="X123" />
</xs:restriction>
</xs:simpleType>

Expand Down
1 change: 1 addition & 0 deletions xsd-parser/tests/feature/custom_type/expected/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub struct CurrencyAmountType {
pub ccy: String,
pub content: Decimal,
}
pub type RestrictedDecimalType = Decimal;
1 change: 1 addition & 0 deletions xsd-parser/tests/feature/custom_type/expected/quick_xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl WithSerializer for CurrencyAmountType {
impl WithDeserializer for CurrencyAmountType {
type Deserializer = quick_xml_deserialize::CurrencyAmountTypeDeserializer;
}
pub type RestrictedDecimalType = Decimal;
pub mod quick_xml_deserialize {
use core::mem::replace;
use xsd_parser_types::quick_xml::{
Expand Down
63 changes: 32 additions & 31 deletions xsd-parser/tests/feature/custom_type/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use xsd_parser::pipeline::generator::ValueGeneratorMode;
use xsd_parser::{
config::{Config, IdentTriple, NamespaceIdent},
models::{
meta::{CustomMeta, MetaType, ReferenceMeta},
IdentType, TypeIdent,
meta::{CustomMeta, MetaType},
IdentType,
},
pipeline::{
generator::{Context as GeneratorContext, Error as GeneratorError},
Expand All @@ -20,39 +20,40 @@ use xsd_parser_types::quick_xml::{DeserializeBytesFromStr, SerializeBytesToStrin
use crate::utils::{generate_test, ConfigEx};

fn config() -> Config {
let mut config = Config::test_default().with_generate([(
IdentType::Element,
Some(NamespaceIdent::namespace(b"urn:example:minimal")),
"Amount",
)]);

config.interpreter.types = vec![
let mut config = Config::test_default().with_generate([
(
IdentTriple::from((IdentType::Type, "Decimal")),
MetaType::from(CustomMeta::new("Decimal").with_default(
|ctx: &GeneratorContext<'_, '_>,
value: &str,
mode: ValueGeneratorMode|
-> Result<ValueRendererBox, GeneratorError> {
if mode != ValueGeneratorMode::Value {
return Err(GeneratorError::InvalidDefaultValue {
ident: ctx.ident.clone(),
value: value.into(),
mode,
});
}

Ok(Box::new(quote! {
<Decimal as core::str::FromStr>::from_str(#value).unwrap()
}))
},
)),
IdentType::Element,
Some(NamespaceIdent::namespace(b"urn:example:minimal")),
"Amount",
),
(
IdentTriple::from((IdentType::Type, "xs:decimal")),
MetaType::from(ReferenceMeta::new(TypeIdent::type_("Decimal"))),
IdentType::Type,
Some(NamespaceIdent::namespace(b"urn:example:minimal")),
"RestrictedDecimal",
),
];
]);

config.interpreter.types = vec![(
IdentTriple::from((IdentType::Type, "xs:decimal")),
MetaType::from(CustomMeta::new("Decimal").with_default(
|ctx: &GeneratorContext<'_, '_>,
value: &str,
mode: ValueGeneratorMode|
-> Result<ValueRendererBox, GeneratorError> {
if mode != ValueGeneratorMode::Value {
return Err(GeneratorError::InvalidDefaultValue {
ident: ctx.ident.clone(),
value: value.into(),
mode,
});
}

Ok(Box::new(quote! {
<Decimal as core::str::FromStr>::from_str(#value).unwrap()
}))
},
)),
)];

config
}
Expand Down
12 changes: 12 additions & 0 deletions xsd-parser/tests/feature/custom_type/schema.xsd
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
</xs:restriction>
</xs:simpleType>

<!-- Type redefinition using restriction -->
<xs:simpleType name="RestrictedDecimal">
<xs:restriction base="xs:decimal">
<xs:annotation>
<xs:documentation>
Restricting the base type without applying any facets.
This should be treated as a custom type, not a simple type.
</xs:documentation>
</xs:annotation>
</xs:restriction>
</xs:simpleType>

<!-- Define a complex type with an attribute -->
<xs:complexType name="CurrencyAmount">
<xs:simpleContent>
Expand Down
21 changes: 6 additions & 15 deletions xsd-parser/tests/schema/musicxml/expected/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ pub struct DegreeValue {
pub struct Direction {
pub placement: ::core::option::Option<AboveBelow>,
pub directive: ::core::option::Option<YesNo>,
pub system: ::core::option::Option<SystemRelation>,
pub system: ::core::option::Option<SystemRelationNumber>,
pub id: ::core::option::Option<::std::string::String>,
pub content: DirectionContent,
}
Expand Down Expand Up @@ -1033,7 +1033,7 @@ pub struct Ending {
pub font_size: ::core::option::Option<FontSize>,
pub font_weight: ::core::option::Option<FontWeight>,
pub color: ::core::option::Option<::std::string::String>,
pub system: ::core::option::Option<SystemRelation>,
pub system: ::core::option::Option<SystemRelationNumber>,
pub end_length: ::core::option::Option<::core::primitive::f64>,
pub text_x: ::core::option::Option<::core::primitive::f64>,
pub text_y: ::core::option::Option<::core::primitive::f64>,
Expand Down Expand Up @@ -1557,7 +1557,7 @@ pub struct Harmony {
pub font_weight: ::core::option::Option<FontWeight>,
pub color: ::core::option::Option<::std::string::String>,
pub placement: ::core::option::Option<AboveBelow>,
pub system: ::core::option::Option<SystemRelation>,
pub system: ::core::option::Option<SystemRelationNumber>,
pub id: ::core::option::Option<::std::string::String>,
pub content: ::std::vec::Vec<HarmonyContent>,
}
Expand Down Expand Up @@ -3486,14 +3486,10 @@ pub enum SwingContent {
Straight(Empty),
First(::core::num::NonZeroUsize),
Second(::core::num::NonZeroUsize),
SwingType(SwingTypeValue),
SwingType(NoteTypeValue),
SwingStyle(::std::string::String),
}
#[derive(Debug)]
pub enum SwingTypeValue {
_16Th,
Eighth,
}
pub type SwingTypeValue = NoteTypeValue;
#[derive(Debug)]
pub enum Syllabic {
Single,
Expand Down Expand Up @@ -3553,12 +3549,7 @@ pub struct SystemMarginsContent {
pub left_margin: ::core::primitive::f64,
pub right_margin: ::core::primitive::f64,
}
#[derive(Debug)]
pub enum SystemRelation {
OnlyTop,
AlsoTop,
None,
}
pub type SystemRelation = SystemRelationNumber;
#[derive(Debug)]
pub enum SystemRelationNumber {
OnlyTop,
Expand Down
Loading