Replace String::from by .into() / .to_owned() / format!()

This commit is contained in:
Jonas Platte
2020-12-02 17:20:13 +01:00
parent c455f12d23
commit d19c57d23d
7 changed files with 43 additions and 51 deletions

View File

@@ -48,7 +48,7 @@ impl<'de, R: Read> Deserializer<R> {
if let Some(ref next) = self.peeked { if let Some(ref next) = self.peeked {
Ok(next) Ok(next)
} else { } else {
Err(String::from("unable to peek next item")) Err("unable to peek next item".into())
} }
} }

View File

@@ -178,9 +178,10 @@ mod testing {
let model = Data { item: $value }; let model = Data { item: $value };
let content = if let Some(str_value) = $content { let content = if let Some(str_value) = $content {
String::from("<data><item>") + str_value + "</item></data>" let str_value: &str = str_value;
format!("<data><item>{}</item></data>", str_value)
} else { } else {
String::from("<data />") "<data />".to_owned()
}; };
serialize_and_validate!(model, content); serialize_and_validate!(model, content);
@@ -225,15 +226,10 @@ mod testing {
log::debug!("serialize_and_validate @ {}:{}", file!(), line!()); log::debug!("serialize_and_validate @ {}:{}", file!(), line!());
let data: Result<String, String> = yaserde::ser::to_string(&$model); let data: Result<String, String> = yaserde::ser::to_string(&$model);
let content = 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!( assert_eq!(
data, data,
Ok( Ok(content.split("\n").map(|s| s.trim()).collect::<String>())
String::from(content)
.split("\n")
.map(|s| s.trim())
.collect::<String>()
)
); );
}; };
} }

View File

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

View File

@@ -37,8 +37,8 @@ fn de_basic() {
content, content,
Book, Book,
Book { Book {
author: String::from("Antoine de Saint-Exupéry"), author: "Antoine de Saint-Exupéry".to_owned(),
title: String::from("Little prince"), title: "Little prince".to_owned(),
} }
); );
@@ -48,8 +48,8 @@ fn de_basic() {
content, content,
Book, Book,
Book { Book {
author: String::from("Antoine de Saint-Exupéry"), author: "Antoine de Saint-Exupéry".to_owned(),
title: String::from("Little prince"), title: "Little prince".to_owned(),
} }
); );
} }
@@ -93,8 +93,8 @@ fn de_dash_param() {
content, content,
Book, Book,
Book { Book {
author: String::from("Antoine de Saint-Exupéry"), author: "Antoine de Saint-Exupéry".to_owned(),
title: String::from("Little prince"), title: "Little prince".to_owned(),
} }
); );
@@ -104,8 +104,8 @@ fn de_dash_param() {
content, content,
Book, Book,
Book { Book {
author: String::from("Antoine de Saint-Exupéry"), author: "Antoine de Saint-Exupéry".to_owned(),
title: String::from("Little prince"), title: "Little prince".to_owned(),
} }
); );
} }
@@ -145,11 +145,11 @@ fn de_multiple_segments() {
content, content,
Book, Book,
Book { Book {
author: String::from("Antoine de Saint-Exupéry"), author: "Antoine de Saint-Exupéry".to_owned(),
title: String::from("Little prince"), title: "Little prince".to_owned(),
page: other_mod::Page { page: other_mod::Page {
number: 40, number: 40,
text: String::from("The Earth is not just an ordinary planet!"), text: "The Earth is not just an ordinary planet!".to_owned(),
}, },
} }
); );
@@ -170,7 +170,7 @@ fn de_list_of_items() {
content, content,
Library, Library,
Library { Library {
books: vec![String::from("Little Prince"), String::from("Harry Potter")], books: vec!["Little Prince".to_owned(), "Harry Potter".to_owned()],
} }
); );
@@ -187,10 +187,10 @@ fn de_list_of_items() {
Libraries { Libraries {
library: vec![ library: vec![
Library { Library {
books: vec![String::from("Little Prince")], books: vec!["Little Prince".to_owned()],
}, },
Library { Library {
books: vec![String::from("Harry Potter")], books: vec!["Harry Potter".to_owned()],
}, },
], ],
} }
@@ -590,7 +590,7 @@ fn de_complex_enum() {
content, content,
XmlStruct, XmlStruct,
XmlStruct { XmlStruct {
background: Color::Black(String::from("text")), background: Color::Black("text".to_owned()),
} }
); );
@@ -605,7 +605,7 @@ fn de_complex_enum() {
content, content,
XmlStruct, XmlStruct,
XmlStruct { XmlStruct {
background: Color::Orange(String::from("text")), background: Color::Orange("text".to_owned()),
} }
); );
@@ -653,7 +653,7 @@ fn de_complex_enum() {
content, content,
XmlStruct, XmlStruct,
XmlStruct { XmlStruct {
background: Color::Yellow(Some(String::from("text"))), background: Color::Yellow(Some("text".to_owned())),
} }
); );
@@ -687,7 +687,7 @@ fn de_complex_enum() {
content, content,
XmlStruct, XmlStruct,
XmlStruct { XmlStruct {
background: Color::Blue(vec![String::from("abc"), String::from("def")]), background: Color::Blue(vec!["abc".to_owned(), "def".to_owned()]),
} }
); );
@@ -776,7 +776,7 @@ fn de_name_issue_21() {
content, content,
Book, Book,
Book { Book {
name: String::from("Little prince"), name: "Little prince".to_owned(),
} }
); );
} }
@@ -805,7 +805,7 @@ fn de_custom() {
use std::str::FromStr; use std::str::FromStr;
if let xml::reader::XmlEvent::StartElement { name, .. } = reader.peek()?.to_owned() { if let xml::reader::XmlEvent::StartElement { name, .. } = reader.peek()?.to_owned() {
let expected_name = String::from("Day"); let expected_name = "Day".to_owned();
if name.local_name != expected_name { if name.local_name != expected_name {
return Err(format!( return Err(format!(
"Wrong StartElement name: {}, expected: {}", "Wrong StartElement name: {}, expected: {}",

View File

@@ -186,7 +186,7 @@ fn unnamed_enum() {
} }
let model = XmlStruct { let model = XmlStruct {
color: Enum::Field(String::from("some_text")), color: Enum::Field("some_text".to_owned()),
}; };
let content = "<base><color><Field>some_text</Field></color></base>"; let content = "<base><color><Field>some_text</Field></color></base>";
@@ -194,7 +194,7 @@ fn unnamed_enum() {
deserialize_and_validate!(content, model, XmlStruct); deserialize_and_validate!(content, model, XmlStruct);
let model = XmlStruct { let model = XmlStruct {
color: Enum::FullPath(String::from("some_text")), color: Enum::FullPath("some_text".to_owned()),
}; };
let content = "<base><color><FullPath>some_text</FullPath></color></base>"; let content = "<base><color><FullPath>some_text</FullPath></color></base>";
@@ -218,7 +218,7 @@ fn unnamed_enum() {
deserialize_and_validate!(content, model, XmlStruct); deserialize_and_validate!(content, model, XmlStruct);
let model = XmlStruct { let model = XmlStruct {
color: Enum::OptionString(Some(String::from("some_text"))), color: Enum::OptionString(Some("some_text".to_owned())),
}; };
let content = "<base><color><OptionString>some_text</OptionString></color></base>"; let content = "<base><color><OptionString>some_text</OptionString></color></base>";
@@ -253,7 +253,7 @@ fn unnamed_enum() {
// deserialize_and_validate!(content, model, XmlStruct); // deserialize_and_validate!(content, model, XmlStruct);
let model = XmlStruct { let model = XmlStruct {
color: Enum::Strings(vec![String::from("abc"), String::from("def")]), color: Enum::Strings(vec!["abc".to_owned(), "def".to_owned()]),
}; };
let content = "<base><color><Strings>abc</Strings><Strings>def</Strings></color></base>"; let content = "<base><color><Strings>abc</Strings><Strings>def</Strings></color></base>";

View File

@@ -22,9 +22,7 @@ fn de_no_content() {
let loaded: Result<Book, String> = from_str(content); let loaded: Result<Book, String> = from_str(content);
assert_eq!( assert_eq!(
loaded, loaded,
Err(String::from( Err("Unexpected end of stream: no root element found".to_owned())
"Unexpected end of stream: no root element found"
))
); );
} }
@@ -43,8 +41,6 @@ fn de_wrong_end_balise() {
let loaded: Result<Book, String> = from_str(content); let loaded: Result<Book, String> = from_str(content);
assert_eq!( assert_eq!(
loaded, loaded,
Err(String::from( Err("Unexpected closing tag: book, expected author".to_owned())
"Unexpected closing tag: book, expected author"
))
); );
} }

View File

@@ -32,8 +32,8 @@ fn struct_simple_namespace() {
"#; "#;
let model = Book { let model = Book {
author: String::from("Antoine de Saint-Exupéry"), author: "Antoine de Saint-Exupéry".to_owned(),
title: String::from("Little prince"), title: "Little prince".to_owned(),
}; };
serialize_and_validate!(model, content); serialize_and_validate!(model, content);
@@ -66,8 +66,8 @@ fn struct_multiple_namespaces() {
"#; "#;
let model = Book { let model = Book {
author: String::from("Antoine de Saint-Exupéry"), author: "Antoine de Saint-Exupéry".to_owned(),
title: String::from("Little prince"), title: "Little prince".to_owned(),
}; };
serialize_and_validate!(model, content); serialize_and_validate!(model, content);
@@ -98,8 +98,8 @@ fn struct_partial_namespace() {
"#; "#;
let model = Book { let model = Book {
author: String::from("Antoine de Saint-Exupéry"), author: "Antoine de Saint-Exupéry".to_owned(),
title: String::from("Little prince"), title: "Little prince".to_owned(),
}; };
serialize_and_validate!(model, content); serialize_and_validate!(model, content);
@@ -132,8 +132,8 @@ fn struct_sub_namespace_definition() {
"#; "#;
let model = Book { let model = Book {
author: String::from("Antoine de Saint-Exupéry"), author: "Antoine de Saint-Exupéry".to_owned(),
title: String::from("Little prince"), title: "Little prince".to_owned(),
}; };
// TODO support namespace for attribute to specify local namespace // TODO support namespace for attribute to specify local namespace