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
14 changes: 10 additions & 4 deletions instant-xml-macros/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,13 @@ impl StructOutput {
.collect::<Result<Vec<_>, _>>()
.map_err(|err| err.to_compile_error())?;

let mut attrs_only = true;
let mut direct = None;
for (field, field_meta) in &fields {
if !field_meta.attribute {
attrs_only = false;
}

if direct.is_some() {
return Err(
syn::Error::new(field.span(), "direct field must be the last")
Expand All @@ -326,14 +331,15 @@ impl StructOutput {
}

if !inline {
self.body.extend(match direct {
Some(field) => quote!(
self.body.extend(match (attrs_only, direct) {
(true, _) => quote!(serializer.end_empty()?;),
(false, Some(field)) => quote!(
match self.#field.present() {
true => serializer.end_start()?,
false => serializer.end_empty()?,
}
),
None => quote!(serializer.end_start()?;),
(false, None) => quote!(serializer.end_start()?;),
})
}

Expand All @@ -351,7 +357,7 @@ impl StructOutput {
}
}

if !inline {
if !inline && !attrs_only {
let tag = meta.tag();
self.body.extend(match direct {
Some(field) => quote!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ struct Basic {
#[test]
fn basic() {
assert_eq!(
from_str::<Basic>("<Basic flag=\"true\"></Basic>"),
from_str::<Basic>("<Basic flag=\"true\" />"),
Ok(Basic { flag: true })
);

assert_eq!(
to_string(&Basic { flag: true }).unwrap(),
"<Basic flag=\"true\"></Basic>"
"<Basic flag=\"true\" />"
);
}

Expand All @@ -31,3 +31,18 @@ fn empty() {
Ok(Empty)
);
}

#[derive(ToXml)]
#[xml(ns(bar = "BAR"))]
struct NoPrefixAttrNs {
#[xml(attribute, ns(bar))]
flag: bool,
}

#[test]
fn no_prefix_attr_ns() {
assert_eq!(
to_string(&NoPrefixAttrNs { flag: true }).unwrap(),
"<NoPrefixAttrNs xmlns:bar=\"BAR\" bar:flag=\"true\" />"
);
}
104 changes: 0 additions & 104 deletions instant-xml/tests/de-direct.rs

This file was deleted.

21 changes: 0 additions & 21 deletions instant-xml/tests/direct-no-value.rs

This file was deleted.

123 changes: 122 additions & 1 deletion instant-xml/tests/direct.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::borrow::Cow;

use similar_asserts::assert_eq;

use instant_xml::{from_str, to_string, FromXml, ToXml};
use instant_xml::{from_str, to_string, Error, FromXml, ToXml};

#[derive(Clone, Debug, Eq, FromXml, PartialEq, ToXml)]
struct Foo {
Expand Down Expand Up @@ -30,3 +32,122 @@ fn direct() {
let xml = "<!--comment--><Foo flag=\"true\"><!--comment-->cbdté</Foo><!--comment-->";
assert_eq!(from_str::<Foo>(xml), Ok(v));
}

#[derive(Debug, Eq, PartialEq, FromXml)]
#[xml(ns("URI"))]
struct StructDirectNamespace {
#[xml(ns("BAZ"))]
flag: bool,
}

#[test]
fn direct_namespaces() {
// Correct direct namespace
assert_eq!(
from_str(
"<StructDirectNamespace xmlns=\"URI\"><flag xmlns=\"BAZ\">true</flag></StructDirectNamespace>"
),
Ok(StructDirectNamespace { flag: true })
);

// Wrong direct namespace
assert_eq!(
from_str(
"<StructDirectNamespace xmlns=\"URI\"><flag xmlns=\"WRONG\">true</flag></StructDirectNamespace>"
),
Err::<StructDirectNamespace, _>(Error::MissingValue("StructDirectNamespace::flag"))
);

// Wrong direct namespace - missing namespace
assert_eq!(
from_str("<StructDirectNamespace xmlns=\"URI\"><flag>true</flag></StructDirectNamespace>"),
Err::<StructDirectNamespace, _>(Error::MissingValue("StructDirectNamespace::flag"))
);
}

#[derive(Debug, Eq, PartialEq, FromXml)]
struct DirectString {
s: String,
}

#[test]
fn direct_string() {
assert_eq!(
from_str("<DirectString><s>hello</s></DirectString>"),
Ok(DirectString {
s: "hello".to_string()
})
);
}

#[derive(Debug, Eq, PartialEq, FromXml)]
struct DirectStr<'a> {
s: Cow<'a, str>,
}

#[test]
fn direct_empty_str() {
assert_eq!(
from_str("<DirectStr><s></s></DirectStr>"),
Ok(DirectStr { s: "".into() })
);
}

#[test]
fn direct_missing_string() {
assert_eq!(
from_str("<DirectString></DirectString>"),
Err::<DirectString, _>(Error::MissingValue("DirectString::s"))
);
}

#[derive(Debug, PartialEq, FromXml)]
struct ArtUri {
#[xml(direct)]
uri: String,
}

#[derive(Debug, PartialEq, FromXml)]
struct Container {
art: Option<ArtUri>,
}

#[test]
fn container_empty_string() {
assert_eq!(
from_str("<Container><ArtUri></ArtUri></Container>"),
Ok(Container {
art: Some(ArtUri {
uri: "".to_string()
})
})
);
assert_eq!(
from_str("<Container><ArtUri/></Container>"),
Ok(Container {
art: Some(ArtUri {
uri: "".to_string()
})
})
);
}

#[derive(ToXml, FromXml, Debug, PartialEq, Eq)]
struct Options {
#[xml(attribute)]
attribute: Option<String>,
#[xml(direct)]
direct: Option<String>,
}

#[test]
fn direct_options() {
let v = Options {
attribute: Some("Attribute text".to_string()),
direct: None,
};
let xml = r#"<Options attribute="Attribute text" />"#;

assert_eq!(xml, to_string(&v).unwrap());
assert_eq!(from_str::<Options>(xml).unwrap(), v);
}
4 changes: 2 additions & 2 deletions instant-xml/tests/option.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ fn option_borrow() {
let v = Bar {
maybe: Some("a".into()),
};
let xml = r#"<Bar maybe="a"></Bar>"#;
let xml = r#"<Bar maybe="a" />"#;

assert_eq!(xml, to_string(&v).unwrap());
assert_eq!(v, from_str(xml).unwrap());

let v = Bar { maybe: None };
let xml = r#"<Bar></Bar>"#;
let xml = r#"<Bar />"#;

assert_eq!(xml, to_string(&v).unwrap());
assert_eq!(v, from_str(xml).unwrap());
Expand Down
2 changes: 1 addition & 1 deletion instant-xml/tests/rename.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn renamed() {

assert_eq!(
to_string(&Renamed { flag: true }).unwrap(),
"<renamed renamed=\"true\"></renamed>"
"<renamed renamed=\"true\" />"
);
}

Expand Down
18 changes: 0 additions & 18 deletions instant-xml/tests/ser-attr-ns.rs

This file was deleted.