diff --git a/examples/to_yaml_string.rs b/examples/to_yaml_string.rs index 5f8c151..66f3e47 100644 --- a/examples/to_yaml_string.rs +++ b/examples/to_yaml_string.rs @@ -27,6 +27,6 @@ fn main() -> Result<(), Box> { bar: BarTest { data: true }, }, ]; - println!("{}", rmsd_yaml::to_string(&data, Default::default())?); + println!("{}", rmsd_yaml::to_string(&data)?); Ok(()) } diff --git a/src/lib.rs b/src/lib.rs index 48112eb..f3be274 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,5 +30,7 @@ pub use self::deserializer::{from_str, to_value, RmsdDeserializer}; pub use self::error::{ErrorKind, RmsdError}; pub use self::map::YamlValueMap; pub use self::position::RmsdPosition; -pub use self::serializer::{to_string, RmsdSerializeOption, RmsdSerializer}; +pub use self::serializer::{ + to_string, to_string_with_opt, RmsdSerializeOption, RmsdSerializer, +}; pub use self::value::{YamlTag, YamlValue, YamlValueData}; diff --git a/src/serializer.rs b/src/serializer.rs index 132a83c..d898f80 100644 --- a/src/serializer.rs +++ b/src/serializer.rs @@ -39,7 +39,7 @@ pub struct RmsdSerializer { current_indent_level: usize, } -pub fn to_string( +pub fn to_string_with_opt( value: &T, option: RmsdSerializeOption, ) -> Result @@ -65,6 +65,13 @@ where Ok(serializer.output) } +pub fn to_string(value: &T) -> Result +where + T: Serialize, +{ + to_string_with_opt(value, RmsdSerializeOption::default()) +} + impl RmsdSerializer { fn get_indent_count(&self) -> usize { if !self.output.ends_with("\n") @@ -516,7 +523,7 @@ mod tests { indent_count: 1, ..Default::default() }; - let result = to_string(&"abc", opt); + let result = to_string_with_opt(&"abc", opt); assert!(result.is_err()); if let Err(e) = result { diff --git a/tests/to_string.rs b/tests/to_string.rs index b9174d7..5bf6f3f 100644 --- a/tests/to_string.rs +++ b/tests/to_string.rs @@ -19,7 +19,7 @@ fn test_struct_to_string() -> Result<(), RmsdError> { bool_c: false, }; - let yaml_str = rmsd_yaml::to_string(&data, Default::default())?; + let yaml_str = rmsd_yaml::to_string_with_opt(&data, Default::default())?; assert_eq!( yaml_str, @@ -35,7 +35,7 @@ bool_c: false"# #[test] fn test_array_to_string() -> Result<(), RmsdError> { let data = vec![1u8, 2, 3, 4]; - let yaml_str = rmsd_yaml::to_string(&data, Default::default())?; + let yaml_str = rmsd_yaml::to_string_with_opt(&data, Default::default())?; assert_eq!(yaml_str, "- 1\n- 2\n- 3\n- 4"); @@ -67,7 +67,7 @@ fn test_array_of_map() -> Result<(), RmsdError> { let mut opt = RmsdSerializeOption::default(); opt.leading_start_indicator = true; - let yaml_str = rmsd_yaml::to_string(&data, opt)?; + let yaml_str = rmsd_yaml::to_string_with_opt(&data, opt)?; assert_eq!( yaml_str, @@ -99,7 +99,7 @@ fn test_array_in_map() -> Result<(), RmsdError> { let mut opt = RmsdSerializeOption::default(); opt.leading_start_indicator = true; - let yaml_str = rmsd_yaml::to_string(&data, opt)?; + let yaml_str = rmsd_yaml::to_string_with_opt(&data, opt)?; assert_eq!( yaml_str, @@ -120,7 +120,7 @@ fn test_nested_array() -> Result<(), RmsdError> { #[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] struct FooTest(Vec>); let data = FooTest(vec![vec![1, 2, 3, 4], vec![5, 6, 7, 8]]); - let yaml_str = rmsd_yaml::to_string(&data, Default::default())?; + let yaml_str = rmsd_yaml::to_string_with_opt(&data, Default::default())?; assert_eq!( yaml_str, @@ -160,7 +160,7 @@ fn test_nested_map() -> Result<(), RmsdError> { }, }; - let yaml_str = rmsd_yaml::to_string(&data, Default::default())?; + let yaml_str = rmsd_yaml::to_string_with_opt(&data, Default::default())?; assert_eq!( yaml_str,