lumeohq / xsd-parser-rs

A xsd/wsdl => rust code generator written in rust
Apache License 2.0
96 stars 34 forks source link

Support attributes in a schema root #33

Closed victor-soloviev closed 4 years ago

victor-soloviev commented 4 years ago

xs:schema may have attributes within itself.

xmlmime.xsd:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
          xmlns:xmime="http://www.w3.org/2005/05/xmlmime"
          targetNamespace="http://www.w3.org/2005/05/xmlmime" >

 <xs:attribute name="contentType">
   <xs:simpleType>
     <xs:restriction base="xs:string" >
     <xs:minLength value="3" />
     </xs:restriction>
   </xs:simpleType>
 </xs:attribute>

 ...

</xs:schema>

Current state of generated code for this case:

//generated file
  #[yaserde(attribute, rename = "contentType")]
  pub content_type: Option<String>,
  #[yaserde(attribute, rename = "expectedContentTypes")]
  pub expected_content_types: Option<String>,
#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
#[yaserde(prefix = "xmime", namespace = "xmime: http://www.w3.org/2005/05/xmlmime")]
pub struct Base64Binary {
  #[yaserde(attribute, prefix = "xmime" rename = "contentType")]
  pub xmime_content_type: Option<ContentType>,
}

#[derive(Default, PartialEq, Debug, YaSerialize, YaDeserialize)]
#[yaserde(prefix = "xmime", namespace = "xmime: http://www.w3.org/2005/05/xmlmime")]
pub struct HexBinary {
  #[yaserde(attribute, prefix = "xmime" rename = "contentType")]
  pub xmime_content_type: Option<ContentType>,
}
LeonidKrutovsky commented 4 years ago
#[derive(Default, PartialEq, Debug, UtilsTupleSerDe)]
pub struct ContentType (pub String);

impl Validate for ContentType {
    fn validate(&self) -> Result<(), String> {        
        if self.0.len() < "3".parse().unwrap() {
            return Err(format!("MinLength validation error. \nExpected: 0 length >= 3 \nActual: 0 length == {}", self.0.len()));
        }
        Ok(())
    }
}

pub type ExpectedContentTypes = String;