From 354592125c45f76f279531b281a735385e563930 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Thu, 16 May 2024 00:40:16 -0700 Subject: [PATCH 1/2] Update apis. --- book/src/schema/array.md | 3 +- book/src/schema/boolean.md | 3 +- book/src/schema/enum.md | 3 +- book/src/schema/float.md | 3 +- book/src/schema/index.md | 3 +- book/src/schema/integer.md | 3 +- book/src/schema/literal.md | 5 +- book/src/schema/null.md | 3 +- book/src/schema/object.md | 3 +- book/src/schema/string.md | 3 +- book/src/schema/struct.md | 3 +- book/src/schema/tuple.md | 3 +- book/src/schema/union.md | 3 +- crates/macros/src/config/mod.rs | 1 - crates/macros/src/config_enum/mod.rs | 3 +- crates/macros/src/schematic/mod.rs | 1 - crates/schematic/src/config/configs.rs | 3 +- crates/types/src/arrays.rs | 15 ++--- crates/types/src/bools.rs | 3 +- crates/types/src/externals.rs | 30 +++------ crates/types/src/lib.rs | 6 +- crates/types/src/numbers.rs | 6 +- crates/types/src/objects.rs | 6 +- crates/types/src/schema_builder.rs | 84 ++++++++++++++------------ crates/types/src/strings.rs | 9 +-- crates/types/src/tuples.rs | 3 +- crates/types/tests/builder_test.rs | 6 +- 27 files changed, 88 insertions(+), 129 deletions(-) diff --git a/book/src/schema/array.md b/book/src/schema/array.md index 5eccf2a9..0552ee09 100644 --- a/book/src/schema/array.md +++ b/book/src/schema/array.md @@ -11,8 +11,7 @@ impl Schematic for T { schema.array(ArrayType { items_type: Box::new(schema.infer::()), ..ArrayType::default() - }); - schema.build() + }) } } ``` diff --git a/book/src/schema/boolean.md b/book/src/schema/boolean.md index 329a981e..55062098 100644 --- a/book/src/schema/boolean.md +++ b/book/src/schema/boolean.md @@ -8,8 +8,7 @@ use schematic::{Schematic, Schema, SchemaBuilder, SchemaType, schema::BooleanTyp impl Schematic for T { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.boolean_default(); - schema.build() + schema.boolean_default() } } ``` diff --git a/book/src/schema/enum.md b/book/src/schema/enum.md index 67e6223d..254d2f11 100644 --- a/book/src/schema/enum.md +++ b/book/src/schema/enum.md @@ -14,8 +14,7 @@ impl Schematic for T { LiteralValue::String("warning".into()), ], ..EnumType::default() - }); - schema.build() + }) } } ``` diff --git a/book/src/schema/float.md b/book/src/schema/float.md index ade5432e..d9c72f31 100644 --- a/book/src/schema/float.md +++ b/book/src/schema/float.md @@ -10,8 +10,7 @@ impl Schematic for T { schema.float(FloatType { kind: FloatKind::F32, ..FloatType::default() - }); - schema.build() + }) } } ``` diff --git a/book/src/schema/index.md b/book/src/schema/index.md index 78ddceec..fe75a113 100644 --- a/book/src/schema/index.md +++ b/book/src/schema/index.md @@ -76,8 +76,7 @@ impl Schematic for User { })), ("age".into(), schema.nest().integer(IntegerType::new_kind(IntegerKind::Usize))), ("status".into(), schema.infer::()), - ])); - schema.build() + ])) } } ``` diff --git a/book/src/schema/integer.md b/book/src/schema/integer.md index 6a6deee9..8a830e77 100644 --- a/book/src/schema/integer.md +++ b/book/src/schema/integer.md @@ -10,8 +10,7 @@ impl Schematic for T { schema.integer(IntegerType { kind: IntegerKind::U32, ..IntegerType::default() - }); - schema.build() + }) } } ``` diff --git a/book/src/schema/literal.md b/book/src/schema/literal.md index d6a1cc3e..b1d6017f 100644 --- a/book/src/schema/literal.md +++ b/book/src/schema/literal.md @@ -8,10 +8,9 @@ use schematic::{Schematic, Schema, SchemaBuilder, SchemaType, schema::{LiteralTy impl Schematic for T { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.literal(LiteralType::new(LiteralValue::String("enabled".into()))); + schema.literal(LiteralType::new(LiteralValue::String("enabled".into()))) // Or - schema.literal_value(LiteralValue::String("enabled".into())); - schema.build() + schema.literal_value(LiteralValue::String("enabled".into())) } } ``` diff --git a/book/src/schema/null.md b/book/src/schema/null.md index 01ac645e..c3a7d367 100644 --- a/book/src/schema/null.md +++ b/book/src/schema/null.md @@ -8,8 +8,7 @@ use schematic::{Schematic, Schema, SchemaBuilder, SchemaType}; impl Schematic for T { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.custom(SchemaType::Null); - schema.build() + schema.set_type_and_build(SchemaType::Null) } } ``` diff --git a/book/src/schema/object.md b/book/src/schema/object.md index 6175ae0d..9209c231 100644 --- a/book/src/schema/object.md +++ b/book/src/schema/object.md @@ -12,8 +12,7 @@ impl Schematic for T { key_type: Box::new(schema.infer::()), value_type: Box::new(schema.infer::()), ..ObjectType::default() - }); - schema.build() + }) } } ``` diff --git a/book/src/schema/string.md b/book/src/schema/string.md index 3e394d3c..1f08e5da 100644 --- a/book/src/schema/string.md +++ b/book/src/schema/string.md @@ -7,8 +7,7 @@ use schematic::{Schematic, Schema, SchemaBuilder, SchemaType, schema::{StringTyp impl Schematic for T { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.string_default(); - schema.build() + schema.string_default() } } ``` diff --git a/book/src/schema/struct.md b/book/src/schema/struct.md index 97d64e51..012df0d4 100644 --- a/book/src/schema/struct.md +++ b/book/src/schema/struct.md @@ -39,8 +39,7 @@ impl Schematic for T { ), ]), ..StructType::default() - }); - schema.build() + }) } } ``` diff --git a/book/src/schema/tuple.md b/book/src/schema/tuple.md index 5feac65d..e040eac0 100644 --- a/book/src/schema/tuple.md +++ b/book/src/schema/tuple.md @@ -15,8 +15,7 @@ impl Schematic for T { Box::new(schema.nest().integer(IntegerType::new_kind(IntegerKind::U32))), ], ..TupleType::default() - }); - schema.build() + }) } } ``` diff --git a/book/src/schema/union.md b/book/src/schema/union.md index dcf027a6..47cf2cd2 100644 --- a/book/src/schema/union.md +++ b/book/src/schema/union.md @@ -18,8 +18,7 @@ impl Schematic for T { Box::new(schema.nest().integer(IntegerType::new_kind(IntegerKind::U32))), ], ..UnionType::default() - }); - schema.build() + }) } } ``` diff --git a/crates/macros/src/config/mod.rs b/crates/macros/src/config/mod.rs index 402031c2..82a88ae8 100644 --- a/crates/macros/src/config/mod.rs +++ b/crates/macros/src/config/mod.rs @@ -143,7 +143,6 @@ impl<'l> ToTokens for ConfigMacro<'l> { use schematic::schema::*; #schema_impl - schema.build() } } diff --git a/crates/macros/src/config_enum/mod.rs b/crates/macros/src/config_enum/mod.rs index eb03f88b..360d9e6b 100644 --- a/crates/macros/src/config_enum/mod.rs +++ b/crates/macros/src/config_enum/mod.rs @@ -192,8 +192,7 @@ pub fn macro_impl(item: TokenStream) -> TokenStream { #(#schema_types),* ], #default_index, - )); - schema.build() + )) } } }); diff --git a/crates/macros/src/schematic/mod.rs b/crates/macros/src/schematic/mod.rs index 01213077..a2bd3950 100644 --- a/crates/macros/src/schematic/mod.rs +++ b/crates/macros/src/schematic/mod.rs @@ -26,7 +26,6 @@ impl<'l> ToTokens for SchematicMacro<'l> { use schematic::schema::*; #schema_impl - schema.build() } } }); diff --git a/crates/schematic/src/config/configs.rs b/crates/schematic/src/config/configs.rs index dcbb3d6c..0f63e763 100644 --- a/crates/schematic/src/config/configs.rs +++ b/crates/schematic/src/config/configs.rs @@ -112,7 +112,6 @@ impl Schematic for ExtendsFrom { schema.union(UnionType::new_any([ schema.infer::(), schema.infer::>(), - ])); - schema.build() + ])) } } diff --git a/crates/types/src/arrays.rs b/crates/types/src/arrays.rs index 01b39fb7..6b6fbbb6 100644 --- a/crates/types/src/arrays.rs +++ b/crates/types/src/arrays.rs @@ -24,15 +24,13 @@ impl ArrayType { impl Schematic for Vec { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.array(ArrayType::new(schema.infer::())); - schema.build() + schema.array(ArrayType::new(schema.infer::())) } } impl Schematic for &[T] { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.array(ArrayType::new(schema.infer::())); - schema.build() + schema.array(ArrayType::new(schema.infer::())) } } @@ -43,8 +41,7 @@ impl Schematic for [T; N] { max_length: Some(N), min_length: Some(N), ..ArrayType::default() - }); - schema.build() + }) } } @@ -54,8 +51,7 @@ impl Schematic for HashSet { items_type: Box::new(schema.infer::()), unique: Some(true), ..ArrayType::default() - }); - schema.build() + }) } } @@ -65,7 +61,6 @@ impl Schematic for BTreeSet { items_type: Box::new(schema.infer::()), unique: Some(true), ..ArrayType::default() - }); - schema.build() + }) } } diff --git a/crates/types/src/bools.rs b/crates/types/src/bools.rs index fb06259f..0f8d425b 100644 --- a/crates/types/src/bools.rs +++ b/crates/types/src/bools.rs @@ -16,7 +16,6 @@ impl BooleanType { impl Schematic for bool { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.boolean(BooleanType::default()); - schema.build() + schema.boolean_default() } } diff --git a/crates/types/src/externals.rs b/crates/types/src/externals.rs index d18099fd..89f48fc8 100644 --- a/crates/types/src/externals.rs +++ b/crates/types/src/externals.rs @@ -12,8 +12,7 @@ macro_rules! impl_set { ($type:ty) => { impl Schematic for $type { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.array(ArrayType::new(schema.infer::())); - schema.build() + schema.array(ArrayType::new(schema.infer::())) } } }; @@ -23,8 +22,7 @@ macro_rules! impl_map { ($type:ty) => { impl Schematic for $type { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.object(ObjectType::new(schema.infer::(), schema.infer::())); - schema.build() + schema.object(ObjectType::new(schema.infer::(), schema.infer::())) } } }; @@ -34,8 +32,7 @@ macro_rules! impl_string { ($type:ty) => { impl Schematic for $type { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.string_default(); - schema.build() + schema.string_default() } } }; @@ -48,8 +45,7 @@ macro_rules! impl_string_format { schema.string(StringType { format: Some($format.into()), ..StringType::default() - }); - schema.build() + }) } } }; @@ -66,8 +62,7 @@ mod chrono_feature { schema.string(StringType { format: Some($format.into()), ..StringType::default() - }); - schema.build() + }) } } }; @@ -133,15 +128,13 @@ mod serde_json_feature { // This isn't accurate since we can't access the `N` enum impl Schematic for serde_json::Number { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.integer(IntegerType::new_kind(IntegerKind::I64)); - schema.build() + schema.integer(IntegerType::new_kind(IntegerKind::I64)) } } impl Schematic for serde_json::Map { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.object(ObjectType::new(schema.infer::(), schema.infer::())); - schema.build() + schema.object(ObjectType::new(schema.infer::(), schema.infer::())) } } } @@ -154,8 +147,7 @@ mod serde_toml_feature { impl Schematic for toml::map::Map { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.object(ObjectType::new(schema.infer::(), schema.infer::())); - schema.build() + schema.object(ObjectType::new(schema.infer::(), schema.infer::())) } } } @@ -169,8 +161,7 @@ mod serde_yaml_feature { // This isn't accurate since we can't access the `N` enum impl Schematic for serde_yaml::Number { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.integer(IntegerType::new_kind(IntegerKind::I64)); - schema.build() + schema.integer(IntegerType::new_kind(IntegerKind::I64)) } } @@ -179,8 +170,7 @@ mod serde_yaml_feature { schema.object(ObjectType::new( schema.infer::(), schema.infer::(), - )); - schema.build() + )) } } } diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index 842a196e..bd933a69 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -49,8 +49,7 @@ pub trait Schematic { impl Schematic for () { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.custom(SchemaType::Null); - schema.build() + schema.set_type_and_build(SchemaType::Null) } } @@ -86,7 +85,6 @@ impl Schematic for Arc { impl Schematic for Option { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.union(UnionType::new_any([schema.infer::(), Schema::null()])); - schema.build() + schema.union(UnionType::new_any([schema.infer::(), Schema::null()])) } } diff --git a/crates/types/src/numbers.rs b/crates/types/src/numbers.rs index e5df5821..29e31461 100644 --- a/crates/types/src/numbers.rs +++ b/crates/types/src/numbers.rs @@ -75,8 +75,7 @@ macro_rules! impl_int { ($type:ty, $kind:expr) => { impl Schematic for $type { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.integer(IntegerType::new_kind($kind)); - schema.build() + schema.integer(IntegerType::new_kind($kind)) } } }; @@ -149,8 +148,7 @@ macro_rules! impl_float { ($type:ty, $kind:expr) => { impl Schematic for $type { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.float(FloatType::new_kind($kind)); - schema.build() + schema.float(FloatType::new_kind($kind)) } } }; diff --git a/crates/types/src/objects.rs b/crates/types/src/objects.rs index e72e20ac..fde8b34f 100644 --- a/crates/types/src/objects.rs +++ b/crates/types/src/objects.rs @@ -23,14 +23,12 @@ impl ObjectType { impl Schematic for BTreeMap { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.object(ObjectType::new(schema.infer::(), schema.infer::())); - schema.build() + schema.object(ObjectType::new(schema.infer::(), schema.infer::())) } } impl Schematic for HashMap { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.object(ObjectType::new(schema.infer::(), schema.infer::())); - schema.build() + schema.object(ObjectType::new(schema.infer::(), schema.infer::())) } } diff --git a/crates/types/src/schema_builder.rs b/crates/types/src/schema_builder.rs index 7662bfc2..59e5c524 100644 --- a/crates/types/src/schema_builder.rs +++ b/crates/types/src/schema_builder.rs @@ -25,12 +25,12 @@ impl SchemaBuilder { } /// Build the schema from the configured values. - pub fn build(self) -> Schema { + pub fn build(&self) -> Schema { Schema { - description: self.description, - name: self.name, + description: self.description.clone(), + name: self.name.clone(), nullable: self.nullable, - ty: self.ty, + ty: self.ty.clone(), ..Default::default() } } @@ -48,58 +48,64 @@ impl SchemaBuilder { self.name_stack.borrow_mut().push(name.to_owned()); } + /// Set the type of schema. + pub fn set_type(&mut self, value: SchemaType) { + self.ty = value; + } + + /// Set the type of schema and then build it. + pub fn set_type_and_build(&mut self, value: SchemaType) -> Schema { + self.set_type(value); + self.build() + } + /// Build an array type. - pub fn array(&mut self, value: ArrayType) { - self.custom(SchemaType::Array(Box::new(value))); + pub fn array(&mut self, value: ArrayType) -> Schema { + self.set_type_and_build(SchemaType::Array(Box::new(value))) } /// Build a boolean type. - pub fn boolean(&mut self, value: BooleanType) { - self.custom(SchemaType::Boolean(Box::new(value))); + pub fn boolean(&mut self, value: BooleanType) -> Schema { + self.set_type_and_build(SchemaType::Boolean(Box::new(value))) } /// Build a boolean type with default settings. - pub fn boolean_default(&mut self) { + pub fn boolean_default(&mut self) -> Schema { self.boolean(BooleanType::default()) } - /// Build with a custom type. - pub fn custom(&mut self, value: SchemaType) { - self.ty = value; - } - /// Build an enum type. - pub fn enumerable(&mut self, value: EnumType) { - self.custom(SchemaType::Enum(Box::new(value))); + pub fn enumerable(&mut self, value: EnumType) -> Schema { + self.set_type_and_build(SchemaType::Enum(Box::new(value))) } /// Build a float type. - pub fn float(&mut self, value: FloatType) { - self.custom(SchemaType::Float(Box::new(value))); + pub fn float(&mut self, value: FloatType) -> Schema { + self.set_type_and_build(SchemaType::Float(Box::new(value))) } /// Build a 32bit float type with default settings. - pub fn float32_default(&mut self) { + pub fn float32_default(&mut self) -> Schema { self.float(FloatType::new_kind(FloatKind::F32)) } /// Build a 64bit float type with default settings. - pub fn float64_default(&mut self) { + pub fn float64_default(&mut self) -> Schema { self.float(FloatType::new_kind(FloatKind::F64)) } /// Build an integer type. - pub fn integer(&mut self, value: IntegerType) { - self.custom(SchemaType::Integer(Box::new(value))); + pub fn integer(&mut self, value: IntegerType) -> Schema { + self.set_type_and_build(SchemaType::Integer(Box::new(value))) } /// Build a literal type. - pub fn literal(&mut self, value: LiteralType) { - self.custom(SchemaType::Literal(Box::new(value))); + pub fn literal(&mut self, value: LiteralType) -> Schema { + self.set_type_and_build(SchemaType::Literal(Box::new(value))) } /// Build a literal type with a value. - pub fn literal_value(&mut self, value: LiteralValue) { + pub fn literal_value(&mut self, value: LiteralValue) -> Schema { self.literal(LiteralType::new(value)) } @@ -115,38 +121,38 @@ impl SchemaBuilder { } /// Build a schema that is also nullable (uses a union). - pub fn nullable(&mut self, value: impl Into) { + pub fn nullable(&mut self, value: impl Into) -> Schema { self.union(UnionType::new_any([value.into(), Schema::null()])) } /// Build an object type. - pub fn object(&mut self, value: ObjectType) { - self.custom(SchemaType::Object(Box::new(value))); + pub fn object(&mut self, value: ObjectType) -> Schema { + self.set_type_and_build(SchemaType::Object(Box::new(value))) } /// Build a string type. - pub fn string(&mut self, value: StringType) { - self.custom(SchemaType::String(Box::new(value))); + pub fn string(&mut self, value: StringType) -> Schema { + self.set_type_and_build(SchemaType::String(Box::new(value))) } /// Build a string type with default settings. - pub fn string_default(&mut self) { + pub fn string_default(&mut self) -> Schema { self.string(StringType::default()) } /// Build a struct type. - pub fn structure(&mut self, value: StructType) { - self.custom(SchemaType::Struct(Box::new(value))); + pub fn structure(&mut self, value: StructType) -> Schema { + self.set_type_and_build(SchemaType::Struct(Box::new(value))) } /// Build a tuple type. - pub fn tuple(&mut self, value: TupleType) { - self.custom(SchemaType::Tuple(Box::new(value))); + pub fn tuple(&mut self, value: TupleType) -> Schema { + self.set_type_and_build(SchemaType::Tuple(Box::new(value))) } /// Build a union type. - pub fn union(&mut self, value: UnionType) { - self.custom(SchemaType::Union(Box::new(value))); + pub fn union(&mut self, value: UnionType) -> Schema { + self.set_type_and_build(SchemaType::Union(Box::new(value))) } /// Infer a [`Schema`] from a type that implements [`Schematic`]. @@ -161,9 +167,7 @@ impl SchemaBuilder { // If this name has already been used, create a reference // so that we avoid recursion! if self.name_stack.borrow().contains(&name) { - builder.custom(SchemaType::Reference(name)); - - return builder.build(); + return builder.set_type_and_build(SchemaType::Reference(name)); } // Otherwise generate a new schema and persist our name cache diff --git a/crates/types/src/strings.rs b/crates/types/src/strings.rs index 18215291..590e4d4c 100644 --- a/crates/types/src/strings.rs +++ b/crates/types/src/strings.rs @@ -27,8 +27,7 @@ macro_rules! impl_string { ($type:ty) => { impl Schematic for $type { fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.string_default(); - schema.build() + schema.string_default() } } }; @@ -41,8 +40,7 @@ macro_rules! impl_string_format { schema.string(StringType { format: Some($format.into()), ..StringType::default() - }); - schema.build() + }) } } }; @@ -54,8 +52,7 @@ impl Schematic for char { max_length: Some(1), min_length: Some(1), ..StringType::default() - }); - schema.build() + }) } } diff --git a/crates/types/src/tuples.rs b/crates/types/src/tuples.rs index 6b2252ac..2b77ec1e 100644 --- a/crates/types/src/tuples.rs +++ b/crates/types/src/tuples.rs @@ -27,8 +27,7 @@ macro_rules! impl_tuple { fn build_schema(mut schema: SchemaBuilder) -> Schema { schema.tuple(TupleType::new([ $(schema.infer::<$arg>(),)* - ])); - schema.build() + ])) } } }; diff --git a/crates/types/tests/builder_test.rs b/crates/types/tests/builder_test.rs index 9611d156..45ec8ce4 100644 --- a/crates/types/tests/builder_test.rs +++ b/crates/types/tests/builder_test.rs @@ -18,8 +18,7 @@ impl Schematic for Named { } fn build_schema(mut schema: SchemaBuilder) -> Schema { - schema.structure(StructType::new([("field".into(), schema.infer::())])); - schema.build() + schema.structure(StructType::new([("field".into(), schema.infer::())])) } } @@ -222,8 +221,7 @@ impl Schematic for Cycle { schema.structure(StructType::new([( "values".into(), schema.infer::>(), - )])); - schema.build() + )])) } } From 4a979cca3867effe4bbc8b3585b1a4d2903f2d65 Mon Sep 17 00:00:00 2001 From: Miles Johnson Date: Thu, 16 May 2024 00:43:22 -0700 Subject: [PATCH 2/2] Fixes. --- crates/macros/src/common/container.rs | 6 +++--- crates/types/src/lib.rs | 2 +- crates/types/src/schema_builder.rs | 11 ++++++----- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/crates/macros/src/common/container.rs b/crates/macros/src/common/container.rs index 4288647e..c204cadc 100644 --- a/crates/macros/src/common/container.rs +++ b/crates/macros/src/common/container.rs @@ -43,7 +43,7 @@ impl<'l> Container<'l> { #description schema.structure(StructType::new([ #(#schema_types),* - ])); + ])) } } Self::Enum { variants } => { @@ -78,7 +78,7 @@ impl<'l> Container<'l> { #(#variants_types),* ], #default_index, - )); + )) } } else { quote! { @@ -88,7 +88,7 @@ impl<'l> Container<'l> { #(#variants_types),* ], #default_index, - )); + )) } } } diff --git a/crates/types/src/lib.rs b/crates/types/src/lib.rs index bd933a69..bc307019 100644 --- a/crates/types/src/lib.rs +++ b/crates/types/src/lib.rs @@ -40,7 +40,7 @@ pub trait Schematic { /// Create and return a schema that models the structure of the implementing type. /// The schema can be used to generate code, documentation, or other artifacts. - fn build_schema(schema: SchemaBuilder) -> Schema { + fn build_schema(mut schema: SchemaBuilder) -> Schema { schema.build() } } diff --git a/crates/types/src/schema_builder.rs b/crates/types/src/schema_builder.rs index 59e5c524..60c5d368 100644 --- a/crates/types/src/schema_builder.rs +++ b/crates/types/src/schema_builder.rs @@ -1,5 +1,6 @@ use crate::*; use std::cell::RefCell; +use std::mem; use std::ops::{Deref, DerefMut}; use std::rc::Rc; @@ -25,12 +26,12 @@ impl SchemaBuilder { } /// Build the schema from the configured values. - pub fn build(&self) -> Schema { + pub fn build(&mut self) -> Schema { Schema { - description: self.description.clone(), - name: self.name.clone(), + description: self.description.take(), + name: self.name.take(), nullable: self.nullable, - ty: self.ty.clone(), + ty: mem::take(&mut self.ty), ..Default::default() } } @@ -212,7 +213,7 @@ impl DerefMut for SchemaBuilder { } impl From for Schema { - fn from(builder: SchemaBuilder) -> Self { + fn from(mut builder: SchemaBuilder) -> Self { builder.build() } }