#![allow(dead_code)] use schematic::internal::partialize_schema; use schematic::{derive_enum, Config, ConfigEnum, SchemaBuilder, Schematic}; use similar::{ChangeTag, TextDiff}; use starbase_sandbox::assert_snapshot; use std::collections::HashMap; use std::fmt::Debug; fn create_diff() -> String { let mut schema = SchemaBuilder::build_root::(); let original = format!("{:#?}", schema); partialize_schema(&mut schema, true); let partial = format!("{:#?}", schema); // println!("ORIGINAL:\n{}\n\n", original); // println!("PARTIAL:\n{}\n\n", partial); let mut diff = String::new(); for change in TextDiff::from_lines(&original, &partial).iter_all_changes() { let sign = match change.tag() { ChangeTag::Delete => "🟥", ChangeTag::Insert => "🟩", ChangeTag::Equal => "⬛️", }; diff.push_str(&format!("{}{}", sign, change)); } diff } #[derive(Config)] struct Empty {} #[derive(Config)] struct Basic { field: String, } #[derive(Config)] struct Primitives { string: String, number: usize, boolean: bool, float: f32, string_opt: Option, number_opt: Option, boolean_opt: Option, float_opt: Option, } #[test] fn primitives() { assert_snapshot!(create_diff::()); } #[derive(Config)] struct Compounds { list: Vec, list_opt: Option>, map: HashMap, map_opt: Option>, } #[test] fn compounds() { assert_snapshot!(create_diff::()); } derive_enum!( #[derive(Default, ConfigEnum)] enum UnitEnum { #[default] A, B, C, } ); derive_enum!( #[derive(Default, ConfigEnum)] enum UnitFallbackEnum { #[default] A, B, C, #[variant(fallback)] Other(String), } ); #[derive(Config)] enum TupleEnum { #[setting(nested)] A(Empty), #[setting(nested)] B(Empty), } #[derive(Config)] struct Enums { unit: UnitEnum, unit_opt: Option, fallback: UnitFallbackEnum, fallback_opt: Option, #[setting(nested)] tuple: TupleEnum, #[setting(nested)] tuple_opt: Option, } #[test] fn enums() { assert_snapshot!(create_diff::()); } #[derive(Config)] struct Nested { #[setting(nested)] field: Basic, #[setting(nested)] field_opt: Option, } #[test] fn nested() { assert_snapshot!(create_diff::()); } #[derive(Config)] struct NestedList { #[setting(nested)] field: Vec, #[setting(nested)] field_opt: Option>, } #[test] fn nested_list() { assert_snapshot!(create_diff::()); } #[derive(Config)] struct NestedMap { #[setting(nested)] field: HashMap, #[setting(nested)] field_opt: Option>, } #[test] fn nested_map() { assert_snapshot!(create_diff::()); } #[derive(Config)] #[config(serde(untagged))] enum Untagged { Foo, Bar(bool), Baz(usize, String), #[setting(nested)] Qux(Basic), } #[test] fn enum_untagged() { assert_snapshot!(create_diff::()); } #[derive(Config)] enum ExternalTagged { Foo, Bar(bool), Baz(usize), #[setting(nested)] Qux(Basic), } #[test] fn enum_external() { assert_snapshot!(create_diff::()); } #[derive(Config)] #[config(serde(tag = "type"))] enum InternalTagged { Foo, Bar(bool), Baz(usize), #[setting(nested)] Qux(Basic), } #[test] fn enum_internal() { assert_snapshot!(create_diff::()); } #[derive(Config)] #[config(serde(tag = "type", content = "content"))] enum AdjacentTagged { Foo, Bar(bool), Baz(usize), #[setting(nested)] Qux(Basic), } #[test] fn enum_adjacent() { assert_snapshot!(create_diff::()); }