use crate::common_macro::schema_imports::*; // use alloc::collections::BTreeMap; #[allow(unused)] struct ThirdParty(BTreeMap); #[allow(unused)] mod third_party_impl { use crate::common_macro::schema_imports::*; pub(super) fn declaration( ) -> borsh::schema::Declaration { let params = vec![::declaration(), ::declaration()]; format!(r#"{}<{}>"#, "ThirdParty", params.join(", ")) } pub(super) fn add_definitions_recursively( definitions: &mut BTreeMap, ) { let fields = borsh::schema::Fields::UnnamedFields(vec![ as borsh::BorshSchema>::declaration(), ]); let definition = borsh::schema::Definition::Struct { fields }; let no_recursion_flag = definitions.get(&declaration::()).is_none(); borsh::schema::add_definition(declaration::(), definition, definitions); if no_recursion_flag { as borsh::BorshSchema>::add_definitions_recursively(definitions); } } } #[allow(unused)] #[derive(BorshSchema)] struct A { #[borsh(schema(with_funcs( declaration = "third_party_impl::declaration::", definitions = "third_party_impl::add_definitions_recursively::" )))] x: ThirdParty, y: u64, } #[allow(unused)] #[derive(BorshSchema)] enum C { C3(u64, u64), C4( u64, #[borsh(schema(with_funcs( declaration = "third_party_impl::declaration::", definitions = "third_party_impl::add_definitions_recursively::" )))] ThirdParty, ), } #[test] pub fn struct_overriden() { assert_eq!( "A".to_string(), >::declaration() ); let mut defs = Default::default(); >::add_definitions_recursively(&mut defs); assert_eq!( schema_map! { "A" => Definition::Struct { fields: Fields::NamedFields(vec![ ("x".to_string(), "ThirdParty".to_string()), ("y".to_string(), "u64".to_string())] )}, "ThirdParty" => Definition::Struct { fields: Fields::UnnamedFields(vec![ "BTreeMap".to_string(), ]) }, "BTreeMap"=> Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, elements: "(u64, 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, length_range: Definition::DEFAULT_LENGTH_RANGE, elements: "u8".to_string() }, "u8" => Definition::Primitive(1) }, defs ); } #[test] pub fn enum_overriden() { assert_eq!( "C".to_string(), >::declaration() ); let mut defs = Default::default(); >::add_definitions_recursively(&mut defs); assert_eq!( schema_map! { "C" => Definition::Enum { tag_width: 1, variants: vec![ (0, "C3".to_string(), "CC3".to_string()), (1, "C4".to_string(), "CC4".to_string()) ] }, "CC3" => Definition::Struct { fields: Fields::UnnamedFields(vec!["u64".to_string(), "u64".to_string()]) }, "CC4" => Definition::Struct { fields: Fields::UnnamedFields(vec![ "u64".to_string(), "ThirdParty".to_string() ]) }, "ThirdParty" => Definition::Struct { fields: Fields::UnnamedFields(vec![ "BTreeMap".to_string(), ]) }, "BTreeMap"=> Definition::Sequence { length_width: Definition::DEFAULT_LENGTH_WIDTH, length_range: Definition::DEFAULT_LENGTH_RANGE, elements: "(u64, 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, length_range: Definition::DEFAULT_LENGTH_RANGE, elements: "u8".to_string() }, "u8" => Definition::Primitive(1) }, defs ); }