#![allow(dead_code)] // Local structures do not have their fields used. use borsh::maybestd::collections::HashMap; use borsh::schema::*; macro_rules! map( () => { HashMap::new() }; { $($key:expr => $value:expr),+ } => { { let mut m = HashMap::new(); $( m.insert($key.to_string(), $value); )+ m } }; ); // Checks that recursive definitions work. Also checks that re-instantiations of templated types work. #[test] pub fn duplicated_instantiations() { #[derive(borsh::BorshSchema)] struct Tomatoes; #[derive(borsh::BorshSchema)] struct Cucumber; #[derive(borsh::BorshSchema)] struct Oil { seeds: HashMap, liquid: Option, }; #[derive(borsh::BorshSchema)] struct Wrapper { foo: Option, bar: Box>, }; #[derive(borsh::BorshSchema)] struct Filling; #[derive(borsh::BorshSchema)] enum A { Bacon, Eggs, Salad(Tomatoes, C, Oil), Sausage { wrapper: W, filling: Filling }, } assert_eq!( "A>".to_string(), >>::declaration() ); let mut defs = Default::default(); >>::add_definitions_recursively(&mut defs); assert_eq!( map! { "A>" => Definition::Enum {variants: vec![ ("Bacon".to_string(), "ABacon>".to_string()), ("Eggs".to_string(), "AEggs>".to_string()), ("Salad".to_string(), "ASalad>".to_string()), ("Sausage".to_string(), "ASausage>".to_string()) ]}, "A" => Definition::Enum {variants: vec![ ("Bacon".to_string(), "ABacon".to_string()), ("Eggs".to_string(), "AEggs".to_string()), ("Salad".to_string(), "ASalad".to_string()), ("Sausage".to_string(), "ASausage".to_string())]}, "ABacon>" => Definition::Struct {fields: Fields::Empty}, "ABacon" => Definition::Struct {fields: Fields::Empty}, "AEggs>" => Definition::Struct {fields: Fields::Empty}, "AEggs" => Definition::Struct {fields: Fields::Empty}, "ASalad>" => Definition::Struct {fields: Fields::UnnamedFields(vec!["Tomatoes".to_string(), "Cucumber".to_string(), "Oil".to_string()])}, "ASalad" => Definition::Struct { fields: Fields::UnnamedFields( vec!["Tomatoes".to_string(), "string".to_string(), "Oil".to_string() ])}, "ASausage>" => Definition::Struct {fields: Fields::NamedFields(vec![("wrapper".to_string(), "Wrapper".to_string()), ("filling".to_string(), "Filling".to_string())])}, "ASausage" => Definition::Struct{ fields: Fields::NamedFields(vec![("wrapper".to_string(), "string".to_string()), ("filling".to_string(), "Filling".to_string())])}, "Cucumber" => Definition::Struct {fields: Fields::Empty}, "Filling" => Definition::Struct {fields: Fields::Empty}, "HashMap" => Definition::Sequence { elements: "Tuple".to_string()}, "Oil" => Definition::Struct { fields: Fields::NamedFields(vec![("seeds".to_string(), "HashMap".to_string()), ("liquid".to_string(), "Option".to_string())])}, "Option" => Definition::Enum {variants: vec![("None".to_string(), "nil".to_string()), ("Some".to_string(), "string".to_string())]}, "Option" => Definition::Enum { variants: vec![("None".to_string(), "nil".to_string()), ("Some".to_string(), "u64".to_string())]}, "Tomatoes" => Definition::Struct {fields: Fields::Empty}, "Tuple" => 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())])} }, defs ); }