Skip to content

Commit

Permalink
Merge 358341c into 15576ef
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Dec 17, 2020
2 parents 15576ef + 358341c commit c32dd5d
Show file tree
Hide file tree
Showing 27 changed files with 365 additions and 354 deletions.
2 changes: 1 addition & 1 deletion yaserde/src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl<'de, R: Read> Deserializer<R> {
if let Some(ref next) = self.peeked {
Ok(next)
} else {
Err(String::from("unable to peek next item"))
Err("unable to peek next item".into())
}
}

Expand Down
56 changes: 23 additions & 33 deletions yaserde/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ pub mod ser;

/// A **data structure** that can be deserialized from any data format supported by YaSerDe.
pub trait YaDeserialize: Sized {
fn deserialize<R: Read>(reader: &mut de::Deserializer<R>) -> Result<Self, std::string::String>;
fn deserialize<R: Read>(reader: &mut de::Deserializer<R>) -> Result<Self, String>;
}

/// A **data structure** that can be serialized into any data format supported by YaSerDe.
pub trait YaSerialize: Sized {
fn serialize<W: Write>(&self, writer: &mut ser::Serializer<W>)
-> Result<(), std::string::String>;
fn serialize<W: Write>(&self, writer: &mut ser::Serializer<W>) -> Result<(), String>;

fn serialize_attributes(
&self,
Expand All @@ -36,7 +35,7 @@ pub trait YaSerialize: Sized {
Vec<xml::attribute::OwnedAttribute>,
xml::namespace::Namespace,
),
std::string::String,
String,
>;
}

Expand All @@ -45,62 +44,59 @@ pub trait Visitor<'de>: Sized {
/// The value produced by this visitor.
type Value;

fn visit_bool(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_bool(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected bool {:?}", v))
}

fn visit_i8(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_i8(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected i8 {:?}", v))
}

fn visit_u8(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_u8(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected u8 {:?}", v))
}

fn visit_i16(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_i16(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected i16 {:?}", v))
}

fn visit_u16(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_u16(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected u16 {:?}", v))
}

fn visit_i32(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_i32(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected i32 {:?}", v))
}

fn visit_u32(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_u32(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected u32 {:?}", v))
}

fn visit_i64(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_i64(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected i64 {:?}", v))
}

fn visit_u64(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_u64(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected u64 {:?}", v))
}

fn visit_f32(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_f32(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected f32 {:?}", v))
}

fn visit_f64(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_f64(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected f64 {:?}", v))
}

fn visit_str(self, v: &str) -> Result<Self::Value, std::string::String> {
fn visit_str(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected str {:?}", v))
}
}

macro_rules! serialize_type {
($type:ty) => {
impl YaSerialize for $type {
fn serialize<W: Write>(
&self,
writer: &mut ser::Serializer<W>,
) -> Result<(), std::string::String> {
fn serialize<W: Write>(&self, writer: &mut ser::Serializer<W>) -> Result<(), String> {
let content = format!("{}", self);
let event = XmlEvent::characters(&content);
let _ret = writer.write(event);
Expand All @@ -116,7 +112,7 @@ macro_rules! serialize_type {
Vec<xml::attribute::OwnedAttribute>,
xml::namespace::Namespace,
),
std::string::String,
String,
> {
Ok((attributes, namespace))
}
Expand Down Expand Up @@ -182,9 +178,9 @@ mod testing {
let model = Data { item: $value };

let content = if let Some(str_value) = $content {
std::string::String::from("<data><item>") + str_value + "</item></data>"
format!("<data><item>{}</item></data>", str_value)
} else {
std::string::String::from("<data />")
"<data />".to_owned()
};

serialize_and_validate!(model, content);
Expand Down Expand Up @@ -218,7 +214,7 @@ mod testing {
macro_rules! deserialize_and_validate {
($content: expr, $model: expr, $struct: tt) => {
log::debug!("deserialize_and_validate @ {}:{}", file!(), line!());
let loaded: Result<$struct, std::string::String> = yaserde::de::from_str($content);
let loaded: Result<$struct, String> = yaserde::de::from_str($content);
assert_eq!(loaded, Ok($model));
};
}
Expand All @@ -227,18 +223,12 @@ mod testing {
macro_rules! serialize_and_validate {
($model: expr, $content: expr) => {
log::debug!("serialize_and_validate @ {}:{}", file!(), line!());
let data: Result<std::string::String, std::string::String> = yaserde::ser::to_string(&$model);
let data: Result<String, String> = yaserde::ser::to_string(&$model);

let content =
std::string::String::from(r#"<?xml version="1.0" encoding="utf-8"?>"#) + &$content;
let content = format!(r#"<?xml version="1.0" encoding="utf-8"?>{}"#, $content);
assert_eq!(
data,
Ok(
std::string::String::from(content)
.split("\n")
.map(|s| s.trim())
.collect::<std::string::String>()
)
Ok(content.split("\n").map(|s| s.trim()).collect::<String>())
);
};
}
Expand Down
6 changes: 3 additions & 3 deletions yaserde/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ pub fn to_string<T: YaSerialize>(model: &T) -> Result<String, String> {
let buf = Cursor::new(Vec::new());
let cursor = serialize_with_writer(model, buf, &Config::default())?;
let data = str::from_utf8(cursor.get_ref()).expect("Found invalid UTF-8");
Ok(std::string::String::from(data))
Ok(data.into())
}

pub fn to_string_with_config<T: YaSerialize>(model: &T, config: &Config) -> Result<String, String> {
let buf = Cursor::new(Vec::new());
let cursor = serialize_with_writer(model, buf, config)?;
let data = str::from_utf8(cursor.get_ref()).expect("Found invalid UTF-8");
Ok(std::string::String::from(data))
Ok(data.into())
}

pub fn serialize_with_writer<W: Write, T: YaSerialize>(
Expand All @@ -37,7 +37,7 @@ pub fn to_string_content<T: YaSerialize>(model: &T) -> Result<String, String> {
let buf = Cursor::new(Vec::new());
let cursor = serialize_with_writer_content(model, buf)?;
let data = str::from_utf8(cursor.get_ref()).expect("Found invalid UTF-8");
Ok(std::string::String::from(data))
Ok(data.into())
}

pub fn serialize_with_writer_content<W: Write, T: YaSerialize>(
Expand Down
5 changes: 0 additions & 5 deletions yaserde/tests/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;

use std::io::{Read, Write};
use yaserde::{YaDeserialize, YaSerialize};

fn init() {
let _ = env_logger::builder().is_test(true).try_init();
}
Expand Down Expand Up @@ -128,8 +125,6 @@ fn module_inclusion() {
init();

mod module {
use super::*;

#[derive(Debug, Default, PartialEq, YaDeserialize, YaSerialize)]
#[yaserde(rename = "module")]
pub struct Module {
Expand Down

0 comments on commit c32dd5d

Please sign in to comment.