Skip to content
Open
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
2 changes: 1 addition & 1 deletion xsd-parser/src/parser/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ pub fn enum_to_field(en: Enum) -> StructField {
name: en.name.clone(),
type_name: en.name.clone(),
subtypes: vec![RsEntity::Enum(en)],
source: StructFieldSource::Element,
source: StructFieldSource::Choice,
..Default::default()
}
}
1 change: 1 addition & 0 deletions xsd-parser/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod extension_base_two_files;
mod ref_to_attribute;
mod rename_only_where_needed;
mod restriction_any_type;
mod sequence_choice;
mod simple_type;
mod tuple_with_integer;
mod tuple_with_string;
Expand Down
36 changes: 36 additions & 0 deletions xsd-parser/tests/sequence_choice/example.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<Section
xmlns:xs="http://www.w3.org/2001/XMLSchema-instance"
xs:schemaLocation="http://pubchem.ncbi.nlm.nih.gov/pug_view https://pubchem.ncbi.nlm.nih.gov/pug_view/pug_view.xsd"
>
<TOCHeading>Structures</TOCHeading>
<Description>Structure depictions of this compound, including computationally generated two-dimensional (2D) and three-dimensional (3D) structures, as well as experimentally determined 3D single-crystal structures.</Description>
<Section>
<TOCHeading>2D Structure</TOCHeading>
<Description>A two-dimensional (2D) structure representation of the compound. Because this structure is processed through chemical structure standardization (Hähnke et al., J. Cheminform. 2018, 10, 36), it is not necessarily the same as the structures provided by individual data contributors. </Description>
<URL>https://doi.org/10.1186/s13321-018-0293-8</URL>
<DisplayControls>
<MoveToTop>true</MoveToTop>
</DisplayControls>
<Information>
<ReferenceNumber>66</ReferenceNumber>
<Value>
<Boolean>true</Boolean>
</Value>
</Information>
</Section>
<Section>
<TOCHeading>3D Conformer</TOCHeading>
<Description>A three-dimensional (3D) structure representation of the compound. This 3D structure is not experimentally determined, but computed by PubChem. This structure may or may not be the same as the inherent structure of the compound you would expect to see in vacuum or in the gas phase, because the underlying computational algorithm aims to generate a protein-bound structure, which would be observed in a protein-ligand complex. More detailed information on this conformer model can be found in Kim et al., J. Cheminform. 2013, 5, 1.</Description>
<URL>https://doi.org/10.1186/1758-2946-5-1</URL>
<DisplayControls>
<MoveToTop>true</MoveToTop>
</DisplayControls>
<Information>
<ReferenceNumber>66</ReferenceNumber>
<Description>2-Hydroxyethyl Methacrylate</Description>
<Value>
<Number>13360</Number>
</Value>
</Information>
</Section>
</Section>
37 changes: 37 additions & 0 deletions xsd-parser/tests/sequence_choice/expected.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "http://pubchem.ncbi.nlm.nih.gov/pug_view")]
pub struct Section {
#[yaserde(flatten)]
pub section_choice: section::SectionChoice,

#[yaserde(rename = "Description")]
pub description: Option<String>,

#[yaserde(rename = "URL")]
pub url: Option<String>,
}

impl Validate for Section {}

pub mod section {
use super::*;

#[derive(PartialEq, Debug, YaSerialize, YaDeserialize)]
#[yaserde(namespace = "http://pubchem.ncbi.nlm.nih.gov/pug_view")]

pub enum SectionChoice {
#[yaserde(rename = "TOCHeading")]
Tocheading(String),
#[yaserde(rename = "TOCID")]
Tocid(i32),
__Unknown__(String),
}

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

impl Validate for SectionChoice {}
}
20 changes: 20 additions & 0 deletions xsd-parser/tests/sequence_choice/input.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://pubchem.ncbi.nlm.nih.gov/pug_view"
targetNamespace="http://pubchem.ncbi.nlm.nih.gov/pug_view"
elementFormDefault="qualified"
attributeFormDefault="unqualified">
<xs:element name="Section">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="TOCHeading" type="xs:string"/>
<xs:element name="TOCID" type="xs:int"/>
</xs:choice>
<xs:element name="Description" type="xs:string" minOccurs="0"/>
<xs:element name="URL" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
31 changes: 31 additions & 0 deletions xsd-parser/tests/sequence_choice/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use super::utils;

#[test]
fn deserialization_works() {
mod expected {
use xsd_parser::generator::validator::Validate;
use yaserde_derive::{YaDeserialize, YaSerialize};

include!("expected.rs");
}

let ser = include_str!("example.xml");

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

assert_eq!(de, expected::Section {
section_choice: expected::section::SectionChoice::Tocheading("2D Structure".into()),
description: Some("Structure depictions of this compound, including computationally generated two-dimensional (2D) and three-dimensional (3D) structures, as well as experimentally determined 3D single-crystal structures.".into()),
url: None,
});
}

#[test]
fn generator_does_not_panic() {
println!("{}", utils::generate(include_str!("input.xsd")))
}

#[test]
fn generator_output_has_correct_ast() {
utils::ast_test(include_str!("input.xsd"), include_str!("expected.rs"));
}