|
| 1 | +//! The children of a choice are mapped to Option fields. |
| 2 | +//! Generating an enum would have been the better way but the choice element |
| 3 | +//! may not have a name, so it's impossible to name the generated Rust enum. |
| 4 | +//! The enum would have been nice to avoid runtime checks that only a single choice element is used. |
| 5 | +
|
| 6 | +use crate::xsd::{ |
| 7 | + annotation::Annotation, attribute::Attribute, element::Element, max_occurences::MaxOccurences, |
| 8 | + Implementation, XsdContext, |
| 9 | +}; |
| 10 | +use log::{debug, info}; |
| 11 | +use proc_macro2::TokenStream; |
| 12 | +use std::io::prelude::*; |
| 13 | +use yaserde::YaDeserialize; |
| 14 | + |
| 15 | +#[derive(Clone, Default, Debug, PartialEq, YaDeserialize)] |
| 16 | +#[yaserde( |
| 17 | + rename = "choice" |
| 18 | + prefix = "xs", |
| 19 | + namespace = "xs: http://www.w3.org/2001/XMLSchema" |
| 20 | +)] |
| 21 | +pub struct Choice { |
| 22 | + #[yaserde(attribute)] |
| 23 | + pub id: Option<String>, |
| 24 | + #[yaserde(rename = "attribute")] |
| 25 | + pub attributes: Vec<Attribute>, |
| 26 | + #[yaserde(rename = "minOccurs", attribute)] |
| 27 | + pub min_occurences: Option<u64>, |
| 28 | + #[yaserde(rename = "maxOccurs", attribute)] |
| 29 | + pub max_occurences: Option<MaxOccurences>, |
| 30 | + #[yaserde(rename = "annotation")] |
| 31 | + pub annotation: Option<Annotation>, |
| 32 | + #[yaserde(rename = "element")] |
| 33 | + pub element: Vec<Element>, |
| 34 | +} |
| 35 | + |
| 36 | +impl Implementation for Choice { |
| 37 | + fn implement( |
| 38 | + &self, |
| 39 | + namespace_definition: &TokenStream, |
| 40 | + prefix: &Option<String>, |
| 41 | + context: &XsdContext, |
| 42 | + ) -> TokenStream { |
| 43 | + let elements: TokenStream = self |
| 44 | + .element |
| 45 | + .iter() |
| 46 | + .map(|element| element.implement(&namespace_definition, prefix, context)) |
| 47 | + .collect(); |
| 48 | + |
| 49 | + quote! { |
| 50 | + #elements |
| 51 | + } |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +impl Choice { |
| 56 | + pub fn get_field_implementation( |
| 57 | + &self, |
| 58 | + context: &XsdContext, |
| 59 | + prefix: &Option<String>, |
| 60 | + ) -> TokenStream { |
| 61 | + info!("Generate choice elements"); |
| 62 | + self |
| 63 | + .element |
| 64 | + .iter() |
| 65 | + .map(|element| element.get_field_implementation(context, prefix, false, true)) |
| 66 | + .collect() |
| 67 | + } |
| 68 | +} |
0 commit comments