Merge pull request #66 from media-io/fix_default_namespace_prefix

Fix default namespace prefix
This commit is contained in:
Marc-Antoine ARNAUD 2020-04-06 15:48:19 +02:00 committed by GitHub
commit 62da5f17f2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 77 additions and 75 deletions

View File

@ -2,10 +2,12 @@ language: rust
rust: rust:
- 1.36.0 - 1.36.0
- 1.37.0 - 1.37.0
- 1.38.0 # error on cfg(doctest)
- 1.39.0 # - 1.38.0
# - 1.39.0
- 1.40.0 - 1.40.0
- 1.41.0 - 1.41.0
- 1.42.0
- stable - stable
- beta - beta
- nightly - nightly
@ -22,6 +24,8 @@ matrix:
- cargo fmt --version - cargo fmt --version
script: script:
- cargo fmt -- --check - cargo fmt -- --check
env:
- CHECK_FORMAT=true
# Clippy # Clippy
- rust: stable - rust: stable
@ -30,6 +34,8 @@ matrix:
script: script:
# Fail if clippy output contains "error:" or "warning:" # Fail if clippy output contains "error:" or "warning:"
- cargo clippy 2>&1 | tee ./clippy.out && ! grep -qe "error:\|warning:" ./clippy.out - cargo clippy 2>&1 | tee ./clippy.out && ! grep -qe "error:\|warning:" ./clippy.out
env:
- CHECK_SYNTAX=true
# Test coverage (with Tarpaulin) # Test coverage (with Tarpaulin)
- rust: stable - rust: stable
@ -39,6 +45,8 @@ matrix:
- RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install cargo-tarpaulin --force - RUSTFLAGS="--cfg procmacro2_semver_exempt" cargo install cargo-tarpaulin --force
script: script:
- cargo tarpaulin -v --ciserver travis-ci --coveralls $TRAVIS_JOB_ID - cargo tarpaulin -v --ciserver travis-ci --coveralls $TRAVIS_JOB_ID
env:
- COVERAGE_MEASUREMENT=true
script: script:
- cargo test - cargo test

View File

@ -9,6 +9,7 @@ repository = "https://github.com/media-io/yaserde"
homepage = "https://github.com/media-io/yaserde" homepage = "https://github.com/media-io/yaserde"
documentation = "https://docs.rs/yaserde" documentation = "https://docs.rs/yaserde"
readme = "../README.md" readme = "../README.md"
edition = "2018"
[dependencies] [dependencies]
yaserde_derive = { version = "0.3.11", path = "../yaserde_derive", optional = true } yaserde_derive = { version = "0.3.11", path = "../yaserde_derive", optional = true }

View File

@ -1,10 +1,10 @@
//! Generic data structure deserialization framework. //! Generic data structure deserialization framework.
//! //!
use crate::YaDeserialize;
use std::io::Read; use std::io::Read;
use xml::name::OwnedName; use xml::name::OwnedName;
use xml::reader::{EventReader, ParserConfig, XmlEvent}; use xml::reader::{EventReader, ParserConfig, XmlEvent};
use YaDeserialize;
pub fn from_str<T: YaDeserialize>(s: &str) -> Result<T, String> { pub fn from_str<T: YaDeserialize>(s: &str) -> Result<T, String> {
from_reader(s.as_bytes()) from_reader(s.as_bytes())

View File

@ -5,7 +5,7 @@
#[macro_use] #[macro_use]
extern crate log; extern crate log;
extern crate xml;
#[cfg(feature = "yaserde_derive")] #[cfg(feature = "yaserde_derive")]
#[allow(unused_imports)] #[allow(unused_imports)]
#[macro_use] #[macro_use]

View File

@ -1,12 +1,12 @@
//! Generic data structure serialization framework. //! Generic data structure serialization framework.
//! //!
use crate::YaSerialize;
use std::io::{Cursor, Write}; use std::io::{Cursor, Write};
use std::str; use std::str;
use xml; use xml;
use xml::writer::XmlEvent; use xml::writer::XmlEvent;
use xml::{EmitterConfig, EventWriter}; use xml::{EmitterConfig, EventWriter};
use YaSerialize;
pub fn to_string<T: YaSerialize>(model: &T) -> Result<String, String> { pub fn to_string<T: YaSerialize>(model: &T) -> Result<String, String> {
let buf = Cursor::new(Vec::new()); let buf = Cursor::new(Vec::new());

View File

@ -1,8 +1,4 @@
#[macro_use] #[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;
use std::io::Read; use std::io::Read;

View File

@ -1,8 +1,4 @@
#[macro_use] #[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;
use std::io::Read; use std::io::Read;

View File

@ -1,8 +1,4 @@
#[macro_use] #[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;
use std::io::Read; use std::io::Read;

View File

@ -1,8 +1,4 @@
#[macro_use] #[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;
use std::io::Read; use std::io::Read;

View File

@ -1,8 +1,4 @@
#[macro_use] #[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;
use std::io::Read; use std::io::Read;

View File

@ -1,8 +1,4 @@
#[macro_use] #[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;
use std::io::Read; use std::io::Read;

View File

@ -1,6 +1,3 @@
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use] #[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;

View File

@ -1,6 +1,3 @@
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use] #[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;
@ -182,6 +179,29 @@ fn ser_struct_default_namespace_via_attribute() {
convert_and_validate!(model, content); convert_and_validate!(model, content);
} }
#[test]
fn ser_struct_default_namespace_via_attribute_with_prefix() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(
root = "tt",
prefix = "TTML",
default_namespace = "TTML",
namespace = "TTML: http://www.w3.org/ns/ttml",
namespace = "ttm: http://www.w3.org/ns/ttml#metadata"
)]
pub struct XmlStruct {
#[yaserde(prefix = "TTML")]
item: String,
}
let model = XmlStruct {
item: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><tt xmlns=\"http://www.w3.org/ns/ttml\" xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\"><item>something</item></tt>";
convert_and_validate!(model, content);
}
#[test] #[test]
fn ser_struct_namespace_nested() { fn ser_struct_namespace_nested() {
#[derive(YaSerialize, Default, PartialEq, Debug)] #[derive(YaSerialize, Default, PartialEq, Debug)]

View File

@ -1,6 +1,3 @@
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use] #[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;

View File

@ -1,6 +1,3 @@
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use] #[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;

View File

@ -1,6 +1,3 @@
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use] #[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;

View File

@ -1,6 +1,3 @@
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use] #[macro_use]
extern crate yaserde_derive; extern crate yaserde_derive;

View File

@ -9,6 +9,7 @@ repository = "https://github.com/media-io/yaserde"
homepage = "https://github.com/media-io/yaserde" homepage = "https://github.com/media-io/yaserde"
documentation = "https://docs.rs/yaserde" documentation = "https://docs.rs/yaserde"
readme = "../README.md" readme = "../README.md"
edition = "2018"
[dependencies] [dependencies]
syn = { version = "~1.0", features = ["visit", "extra-traits"] } syn = { version = "~1.0", features = ["visit", "extra-traits"] }

View File

@ -1,5 +1,5 @@
use attribute::*; use crate::attribute::*;
use field_type::*; use crate::field_type::*;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use syn::spanned::Spanned; use syn::spanned::Spanned;

View File

@ -1,6 +1,6 @@
use attribute::*; use crate::attribute::*;
use de::build_default_value::build_default_value; use crate::de::build_default_value::build_default_value;
use field_type::*; use crate::field_type::*;
use proc_macro2::{Span, TokenStream}; use proc_macro2::{Span, TokenStream};
use std::collections::BTreeMap; use std::collections::BTreeMap;
use syn::spanned::Spanned; use syn::spanned::Spanned;

View File

@ -2,7 +2,7 @@ pub mod build_default_value;
pub mod expand_enum; pub mod expand_enum;
pub mod expand_struct; pub mod expand_struct;
use attribute; use crate::attribute;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use syn; use syn;
use syn::Ident; use syn::Ident;

View File

@ -1,4 +1,4 @@
use attribute::*; use crate::attribute::*;
use proc_macro2::{Ident, Span, TokenStream}; use proc_macro2::{Ident, Span, TokenStream};
pub fn enclose_formatted_characters(label: &Ident, label_name: String) -> TokenStream { pub fn enclose_formatted_characters(label: &Ident, label_name: String) -> TokenStream {

View File

@ -1,5 +1,5 @@
use attribute::*; use crate::attribute::*;
use field_type::*; use crate::field_type::*;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use syn::spanned::Spanned; use syn::spanned::Spanned;

View File

@ -1,5 +1,6 @@
use attribute::*; use crate::attribute::*;
use field_type::*; use crate::field_type::*;
use crate::ser::element::*;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use std::string::ToString; use std::string::ToString;
@ -7,8 +8,6 @@ use syn::spanned::Spanned;
use syn::DataStruct; use syn::DataStruct;
use syn::Ident; use syn::Ident;
use ser::element::*;
pub fn serialize( pub fn serialize(
data_struct: &DataStruct, data_struct: &DataStruct,
name: &Ident, name: &Ident,
@ -27,7 +26,7 @@ pub fn serialize(
let label = &field.ident; let label = &field.ident;
let label_name = build_label_name(&field, &field_attrs); let label_name = build_label_name(&field, &field_attrs, default_namespace);
get_field_type(field).and_then(|f| match f { get_field_type(field).and_then(|f| match f {
FieldType::FieldTypeString FieldType::FieldTypeString
@ -240,7 +239,7 @@ pub fn serialize(
)); ));
} }
let label_name = build_label_name(&field, &field_attrs); let label_name = build_label_name(&field, &field_attrs, default_namespace);
let conditions = condition_generator(label, &field_attrs); let conditions = condition_generator(label, &field_attrs);
get_field_type(field).and_then(|f| match f { get_field_type(field).and_then(|f| match f {
@ -411,16 +410,24 @@ pub fn serialize(
} }
} }
fn build_label_name(field: &syn::Field, field_attrs: &YaSerdeAttribute) -> String { fn build_label_name(
format!( field: &syn::Field,
"{}{}", field_attrs: &YaSerdeAttribute,
default_namespace: &Option<String>,
) -> String {
let prefix = if default_namespace == &field_attrs.prefix {
"".to_string()
} else {
field_attrs field_attrs
.prefix .prefix
.clone() .clone()
.map_or("".to_string(), |prefix| prefix + ":"), .map_or("".to_string(), |prefix| prefix + ":")
field_attrs };
.rename
.clone() let label = field_attrs
.unwrap_or_else(|| field.ident.as_ref().unwrap().to_string()) .rename
) .clone()
.unwrap_or_else(|| field.ident.as_ref().unwrap().to_string());
format!("{}{}", prefix, label)
} }

View File

@ -2,7 +2,7 @@ pub mod element;
pub mod expand_enum; pub mod expand_enum;
pub mod expand_struct; pub mod expand_struct;
use attribute; use crate::attribute;
use proc_macro2::TokenStream; use proc_macro2::TokenStream;
use syn; use syn;
use syn::Ident; use syn::Ident;
@ -15,12 +15,16 @@ pub fn expand_derive_serialize(ast: &syn::DeriveInput) -> Result<TokenStream, St
let root_attrs = attribute::YaSerdeAttribute::parse(attrs); let root_attrs = attribute::YaSerdeAttribute::parse(attrs);
let root = root_attrs.clone().root.unwrap_or_else(|| name.to_string()); let root = root_attrs.clone().root.unwrap_or_else(|| name.to_string());
let root = if let Some(prefix) = root_attrs.prefix { let prefix = if root_attrs.default_namespace == root_attrs.prefix {
prefix + ":" + &root "".to_string()
} else { } else {
root root_attrs
.prefix
.map_or("".to_string(), |prefix| prefix + ":")
}; };
let root = format!("{}{}", prefix, root);
let impl_block = match *data { let impl_block = match *data {
syn::Data::Struct(ref data_struct) => expand_struct::serialize( syn::Data::Struct(ref data_struct) => expand_struct::serialize(
data_struct, data_struct,