Compare commits
2 Commits
353558737f
...
f076ca0fad
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f076ca0fad | ||
|
|
15fb7978a9 |
@ -78,9 +78,11 @@ impl<R: Read> Deserializer<R> {
|
|||||||
};
|
};
|
||||||
match next_event {
|
match next_event {
|
||||||
XmlEvent::StartElement { .. } => {
|
XmlEvent::StartElement { .. } => {
|
||||||
|
log::info!("increasing depth");
|
||||||
self.depth += 1;
|
self.depth += 1;
|
||||||
}
|
}
|
||||||
XmlEvent::EndElement { .. } => {
|
XmlEvent::EndElement { .. } => {
|
||||||
|
log::info!("decreasing depth");
|
||||||
self.depth -= 1;
|
self.depth -= 1;
|
||||||
}
|
}
|
||||||
_ => {}
|
_ => {}
|
||||||
@ -90,6 +92,8 @@ impl<R: Read> Deserializer<R> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn skip_element(&mut self, mut cb: impl FnMut(&XmlEvent)) -> Result<(), String> {
|
pub fn skip_element(&mut self, mut cb: impl FnMut(&XmlEvent)) -> Result<(), String> {
|
||||||
|
log::warn!("Skipping element skip_element");
|
||||||
|
return Err(format!("Skipping elemeent not allowed {:?}", &self.next_event()));
|
||||||
let depth = self.depth;
|
let depth = self.depth;
|
||||||
|
|
||||||
while self.depth >= depth {
|
while self.depth >= depth {
|
||||||
|
|||||||
@ -71,8 +71,9 @@ impl<W: Write> Serializer<W> {
|
|||||||
|
|
||||||
pub fn new_from_writer(writer: W, config: &Config) -> Self {
|
pub fn new_from_writer(writer: W, config: &Config) -> Self {
|
||||||
let mut emitter_config = EmitterConfig::new()
|
let mut emitter_config = EmitterConfig::new()
|
||||||
.cdata_to_characters(true)
|
.cdata_to_characters(config.cdata_to_characters)
|
||||||
.perform_indent(config.perform_indent)
|
.perform_indent(config.perform_indent)
|
||||||
|
.pad_self_closing(config.pad_self_closing)
|
||||||
.write_document_declaration(config.write_document_declaration);
|
.write_document_declaration(config.write_document_declaration);
|
||||||
|
|
||||||
if let Some(indent_string_value) = &config.indent_string {
|
if let Some(indent_string_value) = &config.indent_string {
|
||||||
@ -120,6 +121,8 @@ pub struct Config {
|
|||||||
pub perform_indent: bool,
|
pub perform_indent: bool,
|
||||||
pub write_document_declaration: bool,
|
pub write_document_declaration: bool,
|
||||||
pub indent_string: Option<String>,
|
pub indent_string: Option<String>,
|
||||||
|
pub cdata_to_characters: bool,
|
||||||
|
pub pad_self_closing: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Config {
|
impl Default for Config {
|
||||||
@ -128,6 +131,8 @@ impl Default for Config {
|
|||||||
perform_indent: false,
|
perform_indent: false,
|
||||||
write_document_declaration: true,
|
write_document_declaration: true,
|
||||||
indent_string: None,
|
indent_string: None,
|
||||||
|
cdata_to_characters: true,
|
||||||
|
pad_self_closing: true,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1125,12 +1125,28 @@ fn de_strict() {
|
|||||||
#[derive(PartialEq, Debug, YaDeserialize)]
|
#[derive(PartialEq, Debug, YaDeserialize)]
|
||||||
pub struct Struct {
|
pub struct Struct {
|
||||||
id: i32,
|
id: i32,
|
||||||
|
hello: String,
|
||||||
|
#[yaserde(rename="substruct")]
|
||||||
|
substruct: Substruct,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(PartialEq, Debug, YaDeserialize)]
|
||||||
|
pub struct Substruct {
|
||||||
|
foo: String,
|
||||||
|
}
|
||||||
|
|
||||||
let xml_content = r#"<?xml version="1.0" encoding="utf-8"?>
|
let xml_content = r#"<?xml version="1.0" encoding="utf-8"?>
|
||||||
<Struct>
|
<Struct>
|
||||||
<id>123</id>
|
<id>123</id>
|
||||||
|
<id>123</id>
|
||||||
|
<id>123</id>
|
||||||
|
<id>123</id>
|
||||||
|
<hello>aa</hello>
|
||||||
|
<substruct><foo>bar</foo></substruct>
|
||||||
<NonExistentAttrShouldCrash></NonExistentAttrShouldCrash>
|
<NonExistentAttrShouldCrash></NonExistentAttrShouldCrash>
|
||||||
</Struct>"#;
|
</Struct>"#;
|
||||||
let load: Result<Struct, String> = from_str(xml_content);
|
let load: Result<Struct, String> = from_str(xml_content);
|
||||||
assert!(load.is_err());
|
// println!("loaded struct : {:?}", &load.unwrap());
|
||||||
|
// assert!(false);
|
||||||
|
assert_eq!(load.err().unwrap(), String::from("Found unauthorized element NonExistentAttrShouldCrash"));
|
||||||
}
|
}
|
||||||
|
|||||||
@ -158,12 +158,18 @@ pub fn parse(
|
|||||||
// Don't count current struct's StartElement as substruct's StartElement
|
// Don't count current struct's StartElement as substruct's StartElement
|
||||||
let _root = reader.next_event();
|
let _root = reader.next_event();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
::yaserde::__derive_debug!("Looking at startElement");
|
||||||
|
log::warn!("matching field type ASPDOJIASDPJIDASPJASDJI");
|
||||||
if let Ok(::yaserde::__xml::reader::XmlEvent::StartElement { .. }) = reader.peek() {
|
if let Ok(::yaserde::__xml::reader::XmlEvent::StartElement { .. }) = reader.peek() {
|
||||||
// If substruct's start element found then deserialize substruct
|
// If substruct's start element found then deserialize substruct
|
||||||
|
log::warn!("Found start element ?? {}", stringify!(#struct_name));
|
||||||
let value = <#struct_name as ::yaserde::YaDeserialize>::deserialize(reader)?;
|
let value = <#struct_name as ::yaserde::YaDeserialize>::deserialize(reader)?;
|
||||||
#value_label #action;
|
#value_label #action;
|
||||||
// read EndElement
|
// read EndElement
|
||||||
let _event = reader.next_event()?;
|
let _event = reader.next_event()?;
|
||||||
|
} else {
|
||||||
|
log::warn!("matching field type did not find substruct start element ? {}", stringify!(#struct_name));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -181,17 +187,24 @@ pub fn parse(
|
|||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
log::warn!("matching field type {:?}", field.get_type());
|
||||||
let visit_sub = |sub_type: Box<Field>, action: TokenStream| match *sub_type {
|
let visit_sub = |sub_type: Box<Field>, action: TokenStream| match *sub_type {
|
||||||
Field::FieldOption { .. } | Field::FieldVec { .. } => unimplemented!(),
|
Field::FieldOption { .. } | Field::FieldVec { .. } => unimplemented!(),
|
||||||
Field::FieldStruct { struct_name } => visit_struct(struct_name, action),
|
Field::FieldStruct { struct_name } => {
|
||||||
|
log::warn!("matching field type {:?}", field.get_type());
|
||||||
|
visit_struct(struct_name, action)
|
||||||
|
}
|
||||||
simple_type => visit_simple(simple_type, action),
|
simple_type => visit_simple(simple_type, action),
|
||||||
};
|
};
|
||||||
|
|
||||||
match field.get_type() {
|
match field.get_type() {
|
||||||
Field::FieldStruct { struct_name } => {
|
Field::FieldStruct { struct_name } => {
|
||||||
|
log::warn!("matching field type {:?}", field.get_type());
|
||||||
visit_struct(struct_name, quote! { = ::std::option::Option::Some(value) })
|
visit_struct(struct_name, quote! { = ::std::option::Option::Some(value) })
|
||||||
}
|
}
|
||||||
Field::FieldOption { data_type } => {
|
Field::FieldOption { data_type } => {
|
||||||
|
|
||||||
|
log::info!("207 field.get_type {}", field.get_type());
|
||||||
visit_sub(data_type, quote! { = ::std::option::Option::Some(value) })
|
visit_sub(data_type, quote! { = ::std::option::Option::Some(value) })
|
||||||
}
|
}
|
||||||
Field::FieldVec { data_type } => visit_sub(data_type, quote! { .push(value) }),
|
Field::FieldVec { data_type } => visit_sub(data_type, quote! { .push(value) }),
|
||||||
@ -200,6 +213,7 @@ pub fn parse(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
log::warn!("data struct {:?}", data_struct);
|
||||||
let call_flatten_visitors: TokenStream = data_struct
|
let call_flatten_visitors: TokenStream = data_struct
|
||||||
.fields
|
.fields
|
||||||
.iter()
|
.iter()
|
||||||
@ -207,6 +221,7 @@ pub fn parse(
|
|||||||
.filter(|field| !field.is_attribute() && field.is_flatten())
|
.filter(|field| !field.is_attribute() && field.is_flatten())
|
||||||
.map(|field| {
|
.map(|field| {
|
||||||
let value_label = field.get_value_label();
|
let value_label = field.get_value_label();
|
||||||
|
println!("value_label {:?}", value_label);
|
||||||
|
|
||||||
match field.get_type() {
|
match field.get_type() {
|
||||||
Field::FieldStruct { .. } => quote! {
|
Field::FieldStruct { .. } => quote! {
|
||||||
@ -269,6 +284,7 @@ pub fn parse(
|
|||||||
})
|
})
|
||||||
};
|
};
|
||||||
|
|
||||||
|
log::info!("284 field.get_type {}", field.get_type());
|
||||||
let visit_struct = |struct_name: syn::Path, action: TokenStream| {
|
let visit_struct = |struct_name: syn::Path, action: TokenStream| {
|
||||||
visit(
|
visit(
|
||||||
&action,
|
&action,
|
||||||
@ -291,6 +307,7 @@ pub fn parse(
|
|||||||
simple_type => visit_simple(simple_type, action),
|
simple_type => visit_simple(simple_type, action),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
log::info!("306 field.get_type {}", field.get_type());
|
||||||
match field.get_type() {
|
match field.get_type() {
|
||||||
Field::FieldString => visit_string(),
|
Field::FieldString => visit_string(),
|
||||||
Field::FieldOption { data_type } => {
|
Field::FieldOption { data_type } => {
|
||||||
@ -352,6 +369,10 @@ pub fn parse(
|
|||||||
.map(|field| {
|
.map(|field| {
|
||||||
let label = &field.label();
|
let label = &field.label();
|
||||||
let value_label = field.get_value_label();
|
let value_label = field.get_value_label();
|
||||||
|
quote! {
|
||||||
|
::yaserde::__derive_debug!( "Label {:?}", label);
|
||||||
|
::yaserde::__derive_debug!("Value Label {:?}", value_label);
|
||||||
|
};
|
||||||
|
|
||||||
match field.get_type() {
|
match field.get_type() {
|
||||||
Field::FieldOption { .. } | Field::FieldVec { .. } => {
|
Field::FieldOption { .. } | Field::FieldVec { .. } => {
|
||||||
@ -377,6 +398,7 @@ pub fn parse(
|
|||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
log::warn!("build_code_for_unused_xml_events");
|
||||||
let (init_unused, write_unused, visit_unused) = if call_flatten_visitors.is_empty() {
|
let (init_unused, write_unused, visit_unused) = if call_flatten_visitors.is_empty() {
|
||||||
(None, None, None)
|
(None, None, None)
|
||||||
} else {
|
} else {
|
||||||
@ -431,13 +453,16 @@ pub fn parse(
|
|||||||
match (namespace.as_str(), name.local_name.as_str()) {
|
match (namespace.as_str(), name.local_name.as_str()) {
|
||||||
#call_visitors
|
#call_visitors
|
||||||
_ => {
|
_ => {
|
||||||
::yaserde::__derive_trace!("SKIPPINGSKIPPING Skipping element {:?}", name.local_name);
|
|
||||||
return Err(format!("Found unauthorized element {}", name.local_name));
|
::yaserde::__derive_trace!("Got StartElement {:?}", name.local_name);
|
||||||
|
// return Err(format!("Found unauthorized element {}", name.local_name));
|
||||||
|
|
||||||
let event = reader.next_event()?;
|
let event = reader.next_event()?;
|
||||||
|
::yaserde::__derive_trace!("Next event {:?}", event);
|
||||||
#write_unused
|
#write_unused
|
||||||
|
|
||||||
if depth > 0 { // Don't skip root element
|
if depth > 0 { // Don't skip root element
|
||||||
|
return Err(format!("Found unauthorized element {}", name.local_name));
|
||||||
reader.skip_element(|event| {
|
reader.skip_element(|event| {
|
||||||
#write_unused
|
#write_unused
|
||||||
})?;
|
})?;
|
||||||
@ -451,6 +476,7 @@ pub fn parse(
|
|||||||
depth += 1;
|
depth += 1;
|
||||||
}
|
}
|
||||||
::yaserde::__xml::reader::XmlEvent::EndElement { ref name } => {
|
::yaserde::__xml::reader::XmlEvent::EndElement { ref name } => {
|
||||||
|
log::warn!("endElement {named_element}");
|
||||||
if name.local_name == named_element && reader.depth() == start_depth + 1 {
|
if name.local_name == named_element && reader.depth() == start_depth + 1 {
|
||||||
#write_unused
|
#write_unused
|
||||||
break;
|
break;
|
||||||
@ -535,10 +561,12 @@ fn build_code_for_unused_xml_events(
|
|||||||
) {
|
) {
|
||||||
(
|
(
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
|
::yaserde::__derive_debug!("UNUSED UNUSED created eventWriter {:?}", unused_xml_elements);
|
||||||
let mut buf = ::std::vec![];
|
let mut buf = ::std::vec![];
|
||||||
let mut writer = ::std::option::Option::Some(::yaserde::__xml::writer::EventWriter::new(&mut buf));
|
let mut writer = ::std::option::Option::Some(::yaserde::__xml::writer::EventWriter::new(&mut buf));
|
||||||
}),
|
}),
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
|
::yaserde::__derive_debug!("UNUSED UNUSED writing to writer or something {:?}", unused_xml_elements);
|
||||||
if let ::std::option::Option::Some(ref mut w) = writer {
|
if let ::std::option::Option::Some(ref mut w) = writer {
|
||||||
if w.write(event.as_writer_event().unwrap()).is_err() {
|
if w.write(event.as_writer_event().unwrap()).is_err() {
|
||||||
writer = ::std::option::Option::None;
|
writer = ::std::option::Option::None;
|
||||||
@ -546,6 +574,7 @@ fn build_code_for_unused_xml_events(
|
|||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
Some(quote! {
|
Some(quote! {
|
||||||
|
::yaserde::__derive_debug!("UNUSED UNUSED Handling unused_xml_elements {:?}", unused_xml_elements);
|
||||||
if writer.is_some() {
|
if writer.is_some() {
|
||||||
let unused_xml_elements = ::std::string::String::from_utf8(buf).unwrap();
|
let unused_xml_elements = ::std::string::String::from_utf8(buf).unwrap();
|
||||||
#call_flatten_visitors
|
#call_flatten_visitors
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user