From c455f12d236db95d275216980275c70b7c63c0f7 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Wed, 2 Dec 2020 17:15:53 +0100 Subject: [PATCH] Drop unnecessary qualification of String outside of generated code --- yaserde/src/lib.rs | 53 ++++++++++++-------------- yaserde/src/ser/mod.rs | 6 +-- yaserde/tests/deserializer.rs | 18 ++++----- yaserde/tests/enum.rs | 2 +- yaserde/tests/serializer.rs | 2 +- yaserde_derive/src/common/field.rs | 4 +- yaserde_derive/src/de/expand_enum.rs | 2 +- yaserde_derive/src/de/expand_struct.rs | 2 +- 8 files changed, 41 insertions(+), 48 deletions(-) diff --git a/yaserde/src/lib.rs b/yaserde/src/lib.rs index ad27683..d5d737b 100644 --- a/yaserde/src/lib.rs +++ b/yaserde/src/lib.rs @@ -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(reader: &mut de::Deserializer) -> Result; + fn deserialize(reader: &mut de::Deserializer) -> Result; } /// A **data structure** that can be serialized into any data format supported by YaSerDe. pub trait YaSerialize: Sized { - fn serialize(&self, writer: &mut ser::Serializer) - -> Result<(), std::string::String>; + fn serialize(&self, writer: &mut ser::Serializer) -> Result<(), String>; fn serialize_attributes( &self, @@ -36,7 +35,7 @@ pub trait YaSerialize: Sized { Vec, xml::namespace::Namespace, ), - std::string::String, + String, >; } @@ -45,51 +44,51 @@ pub trait Visitor<'de>: Sized { /// The value produced by this visitor. type Value; - fn visit_bool(self, v: &str) -> Result { + fn visit_bool(self, v: &str) -> Result { Err(format!("Unexpected bool {:?}", v)) } - fn visit_i8(self, v: &str) -> Result { + fn visit_i8(self, v: &str) -> Result { Err(format!("Unexpected i8 {:?}", v)) } - fn visit_u8(self, v: &str) -> Result { + fn visit_u8(self, v: &str) -> Result { Err(format!("Unexpected u8 {:?}", v)) } - fn visit_i16(self, v: &str) -> Result { + fn visit_i16(self, v: &str) -> Result { Err(format!("Unexpected i16 {:?}", v)) } - fn visit_u16(self, v: &str) -> Result { + fn visit_u16(self, v: &str) -> Result { Err(format!("Unexpected u16 {:?}", v)) } - fn visit_i32(self, v: &str) -> Result { + fn visit_i32(self, v: &str) -> Result { Err(format!("Unexpected i32 {:?}", v)) } - fn visit_u32(self, v: &str) -> Result { + fn visit_u32(self, v: &str) -> Result { Err(format!("Unexpected u32 {:?}", v)) } - fn visit_i64(self, v: &str) -> Result { + fn visit_i64(self, v: &str) -> Result { Err(format!("Unexpected i64 {:?}", v)) } - fn visit_u64(self, v: &str) -> Result { + fn visit_u64(self, v: &str) -> Result { Err(format!("Unexpected u64 {:?}", v)) } - fn visit_f32(self, v: &str) -> Result { + fn visit_f32(self, v: &str) -> Result { Err(format!("Unexpected f32 {:?}", v)) } - fn visit_f64(self, v: &str) -> Result { + fn visit_f64(self, v: &str) -> Result { Err(format!("Unexpected f64 {:?}", v)) } - fn visit_str(self, v: &str) -> Result { + fn visit_str(self, v: &str) -> Result { Err(format!("Unexpected str {:?}", v)) } } @@ -97,10 +96,7 @@ pub trait Visitor<'de>: Sized { macro_rules! serialize_type { ($type:ty) => { impl YaSerialize for $type { - fn serialize( - &self, - writer: &mut ser::Serializer, - ) -> Result<(), std::string::String> { + fn serialize(&self, writer: &mut ser::Serializer) -> Result<(), String> { let content = format!("{}", self); let event = XmlEvent::characters(&content); let _ret = writer.write(event); @@ -116,7 +112,7 @@ macro_rules! serialize_type { Vec, xml::namespace::Namespace, ), - std::string::String, + String, > { Ok((attributes, namespace)) } @@ -182,9 +178,9 @@ mod testing { let model = Data { item: $value }; let content = if let Some(str_value) = $content { - std::string::String::from("") + str_value + "" + String::from("") + str_value + "" } else { - std::string::String::from("") + String::from("") }; serialize_and_validate!(model, content); @@ -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)); }; } @@ -227,17 +223,16 @@ mod testing { macro_rules! serialize_and_validate { ($model: expr, $content: expr) => { log::debug!("serialize_and_validate @ {}:{}", file!(), line!()); - let data: Result = yaserde::ser::to_string(&$model); + let data: Result = yaserde::ser::to_string(&$model); - let content = - std::string::String::from(r#""#) + &$content; + let content = String::from(r#""#) + &$content; assert_eq!( data, Ok( - std::string::String::from(content) + String::from(content) .split("\n") .map(|s| s.trim()) - .collect::() + .collect::() ) ); }; diff --git a/yaserde/src/ser/mod.rs b/yaserde/src/ser/mod.rs index 26747f3..4d57c87 100644 --- a/yaserde/src/ser/mod.rs +++ b/yaserde/src/ser/mod.rs @@ -11,14 +11,14 @@ pub fn to_string(model: &T) -> Result { 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(String::from(data)) } pub fn to_string_with_config(model: &T, config: &Config) -> Result { 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(String::from(data)) } pub fn serialize_with_writer( @@ -37,7 +37,7 @@ pub fn to_string_content(model: &T) -> Result { 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(String::from(data)) } pub fn serialize_with_writer_content( diff --git a/yaserde/tests/deserializer.rs b/yaserde/tests/deserializer.rs index 3b49633..a69cfab 100644 --- a/yaserde/tests/deserializer.rs +++ b/yaserde/tests/deserializer.rs @@ -15,7 +15,7 @@ fn init() { macro_rules! convert_and_validate { ($content: expr, $struct: tt, $model: expr) => { debug!("convert_and_validate @ {}:{}", file!(), line!()); - let loaded: Result<$struct, std::string::String> = from_str($content); + let loaded: Result<$struct, String> = from_str($content); assert_eq!(loaded, Ok($model)); }; } @@ -118,15 +118,15 @@ fn de_multiple_segments() { #[derive(YaDeserialize, PartialEq, Debug, Default)] pub struct Page { pub number: i32, - pub text: std::string::String, + pub text: String, } } #[derive(YaDeserialize, PartialEq, Debug)] #[yaserde(root = "book")] pub struct Book { - author: std::string::String, - title: std::string::String, + author: String, + title: String, page: other_mod::Page, } @@ -277,7 +277,7 @@ fn de_attributes_custom_deserializer() { #[derive(Default, YaDeserialize, PartialEq, Debug)] pub struct Struct { #[yaserde(attribute)] - attr_option_string: Option, + attr_option_string: Option, #[yaserde(attribute)] attr_option_struct: Option, } @@ -326,7 +326,7 @@ fn de_attributes_complex() { #[derive(Default, YaDeserialize, PartialEq, Debug)] pub struct Struct { #[yaserde(attribute)] - attr_option_string: Option, + attr_option_string: Option, #[yaserde(attribute)] attr_option_enum: Option, } @@ -559,7 +559,7 @@ fn de_complex_enum() { pub enum Color { White, Black(String), - Orange(std::string::String), + Orange(String), Red(i32), Green(OtherStruct), Yellow(Option), @@ -801,9 +801,7 @@ fn de_custom() { } impl YaDeserialize for Day { - fn deserialize( - reader: &mut yaserde::de::Deserializer, - ) -> Result { + fn deserialize(reader: &mut yaserde::de::Deserializer) -> Result { use std::str::FromStr; if let xml::reader::XmlEvent::StartElement { name, .. } = reader.peek()?.to_owned() { diff --git a/yaserde/tests/enum.rs b/yaserde/tests/enum.rs index 0e902ca..8de6f67 100644 --- a/yaserde/tests/enum.rs +++ b/yaserde/tests/enum.rs @@ -165,7 +165,7 @@ fn unnamed_enum() { pub enum Enum { Simple, Field(String), - FullPath(std::string::String), + FullPath(String), Integer(i32), UserStruct(OtherStruct), OptionString(Option), diff --git a/yaserde/tests/serializer.rs b/yaserde/tests/serializer.rs index 8c07600..8abe69b 100644 --- a/yaserde/tests/serializer.rs +++ b/yaserde/tests/serializer.rs @@ -195,7 +195,7 @@ fn ser_attributes_complex() { #[derive(YaSerialize, PartialEq, Debug)] pub struct Struct { #[yaserde(attribute)] - attr_option_string: Option, + attr_option_string: Option, #[yaserde(attribute)] attr_option_enum: Option, } diff --git a/yaserde_derive/src/common/field.rs b/yaserde_derive/src/common/field.rs index 42648fa..dc6ff23 100644 --- a/yaserde_derive/src/common/field.rs +++ b/yaserde_derive/src/common/field.rs @@ -273,7 +273,7 @@ impl Into for Field { } impl Into for &Field { - fn into(self) -> std::string::String { + fn into(self) -> String { match self { Field::FieldString => "str".to_string(), Field::FieldBool => "bool".to_string(), @@ -294,7 +294,7 @@ impl Into for &Field { impl fmt::Display for Field { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let string_representation: std::string::String = self.into(); + let string_representation: String = self.into(); write!(f, "{}", string_representation) } } diff --git a/yaserde_derive/src/de/expand_enum.rs b/yaserde_derive/src/de/expand_enum.rs index 222f241..c53b89d 100644 --- a/yaserde_derive/src/de/expand_enum.rs +++ b/yaserde_derive/src/de/expand_enum.rs @@ -182,7 +182,7 @@ fn build_unnamed_field_visitors(fields: &syn::FieldsUnnamed) -> TokenStream { match field.get_type() { Field::FieldStruct { struct_name } => { - let struct_id: std::string::String = struct_name + let struct_id: String = struct_name .segments .iter() .map(|s| s.ident.to_string()) diff --git a/yaserde_derive/src/de/expand_struct.rs b/yaserde_derive/src/de/expand_struct.rs index 2cffffa..8edab56 100644 --- a/yaserde_derive/src/de/expand_struct.rs +++ b/yaserde_derive/src/de/expand_struct.rs @@ -66,7 +66,7 @@ pub fn parse( .map(|field| YaSerdeField::new(field.clone())) .map(|field| { let struct_visitor = |struct_name: syn::Path| { - let struct_id: std::string::String = struct_name + let struct_id: String = struct_name .segments .iter() .map(|s| s.ident.to_string())