From 63cf36dcdc4a0960c623fb59b8192adcdf3d38fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dj8yf0=CE=BCl?= <26653921+dj8yfo@users.noreply.github.com> Date: Tue, 26 Sep 2023 22:15:39 +0300 Subject: [PATCH] chore!: rename `"Tuple"` -> `"(T0, T1, T2...)"` (`schema::Declaration`) (#234) --- borsh/src/schema.rs | 26 +++++++++++++++----------- borsh/tests/test_schema_enums.rs | 4 ++-- borsh/tests/test_schema_nested.rs | 4 ++-- borsh/tests/test_schema_recursive.rs | 4 ++-- borsh/tests/test_schema_structs.rs | 8 ++++---- borsh/tests/test_schema_tuple.rs | 4 ++-- borsh/tests/test_schema_with.rs | 8 ++++---- 7 files changed, 31 insertions(+), 27 deletions(-) diff --git a/borsh/src/schema.rs b/borsh/src/schema.rs index 701be6151..8d6b45108 100644 --- a/borsh/src/schema.rs +++ b/borsh/src/schema.rs @@ -667,7 +667,11 @@ macro_rules! impl_tuple { fn declaration() -> Declaration { let params = vec![$($name::declaration()),+]; - format!(r#"Tuple<{}>"#, params.join(", ")) + if params.len() == 1 { + format!(r#"({},)"#, params[0]) + } else { + format!(r#"({})"#, params.join(", ")) + } } } }; @@ -817,10 +821,10 @@ mod tests { let actual_name = <(u64, core::num::NonZeroU16, String)>::declaration(); let mut actual_defs = map!(); <(u64, core::num::NonZeroU16, String)>::add_definitions_recursively(&mut actual_defs); - assert_eq!("Tuple", actual_name); + assert_eq!("(u64, NonZeroU16, String)", actual_name); assert_eq!( map! { - "Tuple" => Definition::Tuple { + "(u64, NonZeroU16, String)" => Definition::Tuple { elements: vec![ "u64".to_string(), "NonZeroU16".to_string(), @@ -845,15 +849,15 @@ mod tests { let actual_name = <(u64, (u8, bool), String)>::declaration(); let mut actual_defs = map!(); <(u64, (u8, bool), String)>::add_definitions_recursively(&mut actual_defs); - assert_eq!("Tuple, String>", actual_name); + assert_eq!("(u64, (u8, bool), String)", actual_name); assert_eq!( map! { - "Tuple, String>" => Definition::Tuple { elements: vec![ + "(u64, (u8, bool), String)" => Definition::Tuple { elements: vec![ "u64".to_string(), - "Tuple".to_string(), + "(u8, bool)".to_string(), "String".to_string(), ]}, - "Tuple" => Definition::Tuple { elements: vec![ "u8".to_string(), "bool".to_string()]}, + "(u8, bool)" => Definition::Tuple { elements: vec![ "u8".to_string(), "bool".to_string()]}, "u64" => Definition::Primitive(8), "u8" => Definition::Primitive(1), "bool" => Definition::Primitive(1), @@ -879,9 +883,9 @@ mod tests { "HashMap" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, - elements: "Tuple".to_string(), + elements: "(u64, String)".to_string(), } , - "Tuple" => Definition::Tuple { + "(u64, String)" => Definition::Tuple { elements: vec![ "u64".to_string(), "String".to_string()], }, "u64" => Definition::Primitive(8), @@ -933,9 +937,9 @@ mod tests { "BTreeMap" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, - elements: "Tuple".to_string(), + elements: "(u64, String)".to_string(), } , - "Tuple" => Definition::Tuple { elements: vec![ "u64".to_string(), "String".to_string()]}, + "(u64, String)" => Definition::Tuple { elements: vec![ "u64".to_string(), "String".to_string()]}, "u64" => Definition::Primitive(8), "String" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, diff --git a/borsh/tests/test_schema_enums.rs b/borsh/tests/test_schema_enums.rs index ad6974879..eab12703f 100644 --- a/borsh/tests/test_schema_enums.rs +++ b/borsh/tests/test_schema_enums.rs @@ -286,9 +286,9 @@ fn common_map() -> BTreeMap { "BTreeMap" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, - elements: "Tuple".to_string(), + elements: "(u32, u16)".to_string(), }, - "Tuple" => Definition::Tuple { elements: vec!["u32".to_string(), "u16".to_string()]}, + "(u32, u16)" => Definition::Tuple { elements: vec!["u32".to_string(), "u16".to_string()]}, "u32" => Definition::Primitive(4), "i8" => Definition::Primitive(1), "u16" => Definition::Primitive(2), diff --git a/borsh/tests/test_schema_nested.rs b/borsh/tests/test_schema_nested.rs index 7b91c1e95..735a74230 100644 --- a/borsh/tests/test_schema_nested.rs +++ b/borsh/tests/test_schema_nested.rs @@ -97,7 +97,7 @@ pub fn duplicated_instantiations() { "HashMap" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, - elements: "Tuple".to_string(), + elements: "(u64, String)".to_string(), }, "Oil" => Definition::Struct { fields: Fields::NamedFields(vec![("seeds".to_string(), "HashMap".to_string()), ("liquid".to_string(), "Option".to_string())])}, "Option" => Definition::Enum { @@ -115,7 +115,7 @@ pub fn duplicated_instantiations() { ] }, "Tomatoes" => Definition::Struct {fields: Fields::Empty}, - "Tuple" => Definition::Tuple {elements: vec!["u64".to_string(), "String".to_string()]}, + "(u64, String)" => Definition::Tuple {elements: vec!["u64".to_string(), "String".to_string()]}, "Wrapper" => Definition::Struct{ fields: Fields::NamedFields(vec![("foo".to_string(), "Option".to_string()), ("bar".to_string(), "A".to_string())])}, "u64" => Definition::Primitive(8), "()" => Definition::Primitive(0), diff --git a/borsh/tests/test_schema_recursive.rs b/borsh/tests/test_schema_recursive.rs index 83e6a880b..63cd2171c 100644 --- a/borsh/tests/test_schema_recursive.rs +++ b/borsh/tests/test_schema_recursive.rs @@ -62,9 +62,9 @@ pub fn recursive_struct_schema() { "BTreeMap" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, - elements: "Tuple".to_string(), + elements: "(String, CRecC)".to_string(), }, - "Tuple" => Definition::Tuple {elements: vec!["String".to_string(), "CRecC".to_string()]}, + "(String, CRecC)" => Definition::Tuple {elements: vec!["String".to_string(), "CRecC".to_string()]}, "String" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, diff --git a/borsh/tests/test_schema_structs.rs b/borsh/tests/test_schema_structs.rs index c4bcc4919..0311cfe31 100644 --- a/borsh/tests/test_schema_structs.rs +++ b/borsh/tests/test_schema_structs.rs @@ -204,9 +204,9 @@ pub fn simple_generics() { "HashMap" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, - elements: "Tuple".to_string(), + elements: "(u64, String)".to_string(), }, - "Tuple" => Definition::Tuple{elements: vec!["u64".to_string(), "String".to_string()]}, + "(u64, String)" => Definition::Tuple{elements: vec!["u64".to_string(), "String".to_string()]}, "u64" => Definition::Primitive(8), "String" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, @@ -337,11 +337,11 @@ pub fn generic_associated_item3() { map! { "Parametrized" => Definition::Struct { fields: Fields::NamedFields(vec![ - ("field".to_string(), "Tuple".to_string()), + ("field".to_string(), "(i8, u32)".to_string()), ("another".to_string(), "String".to_string()) ]) }, - "Tuple" => Definition::Tuple { + "(i8, u32)" => Definition::Tuple { elements: vec!["i8".to_string(), "u32".to_string()] }, "i8" => Definition::Primitive(1), diff --git a/borsh/tests/test_schema_tuple.rs b/borsh/tests/test_schema_tuple.rs index 761781d9b..c3892d048 100644 --- a/borsh/tests/test_schema_tuple.rs +++ b/borsh/tests/test_schema_tuple.rs @@ -26,12 +26,12 @@ macro_rules! map( #[test] fn test_unary_tuple_schema() { - assert_eq!("Tuple", <(bool,)>::declaration()); + assert_eq!("(bool,)", <(bool,)>::declaration()); let mut defs = Default::default(); <(bool,)>::add_definitions_recursively(&mut defs); assert_eq!( map! { - "Tuple" => Definition::Tuple { elements: vec!["bool".to_string()] }, + "(bool,)" => Definition::Tuple { elements: vec!["bool".to_string()] }, "bool" => Definition::Primitive(1) }, defs diff --git a/borsh/tests/test_schema_with.rs b/borsh/tests/test_schema_with.rs index a003d2eba..b337ab51e 100644 --- a/borsh/tests/test_schema_with.rs +++ b/borsh/tests/test_schema_with.rs @@ -116,9 +116,9 @@ pub fn struct_overriden() { "BTreeMap"=> Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, - elements: "Tuple".to_string(), + elements: "(u64, String)".to_string(), }, - "Tuple" => Definition::Tuple { elements: vec!["u64".to_string(), "String".to_string()]}, + "(u64, String)" => Definition::Tuple { elements: vec!["u64".to_string(), "String".to_string()]}, "u64" => Definition::Primitive(8), "String" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, @@ -158,9 +158,9 @@ pub fn enum_overriden() { "BTreeMap"=> Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, - elements: "Tuple".to_string(), + elements: "(u64, String)".to_string(), }, - "Tuple" => Definition::Tuple { elements: vec!["u64".to_string(), "String".to_string()]}, + "(u64, String)" => Definition::Tuple { elements: vec!["u64".to_string(), "String".to_string()]}, "u64" => Definition::Primitive(8), "String" => Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH,