Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal: Simplify builder API. #117

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions book/src/schema/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ impl Schematic for T {
schema.array(ArrayType {
items_type: Box::new(schema.infer::<String>()),
..ArrayType::default()
});
schema.build()
})
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/boolean.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ impl Schematic for T {
LiteralValue::String("warning".into()),
],
..EnumType::default()
});
schema.build()
})
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/float.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ impl Schematic for T {
schema.float(FloatType {
kind: FloatKind::F32,
..FloatType::default()
});
schema.build()
})
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,7 @@ impl Schematic for User {
})),
("age".into(), schema.nest().integer(IntegerType::new_kind(IntegerKind::Usize))),
("status".into(), schema.infer::<UserStatus>()),
]));
schema.build()
]))
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/integer.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ impl Schematic for T {
schema.integer(IntegerType {
kind: IntegerKind::U32,
..IntegerType::default()
});
schema.build()
})
}
}
```
Expand Down
5 changes: 2 additions & 3 deletions book/src/schema/literal.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()))
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/null.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/object.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ impl Schematic for T {
key_type: Box::new(schema.infer::<String>()),
value_type: Box::new(schema.infer::<String>()),
..ObjectType::default()
});
schema.build()
})
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/struct.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl Schematic for T {
),
]),
..StructType::default()
});
schema.build()
})
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/tuple.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ impl Schematic for T {
Box::new(schema.nest().integer(IntegerType::new_kind(IntegerKind::U32))),
],
..TupleType::default()
});
schema.build()
})
}
}
```
Expand Down
3 changes: 1 addition & 2 deletions book/src/schema/union.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ impl Schematic for T {
Box::new(schema.nest().integer(IntegerType::new_kind(IntegerKind::U32))),
],
..UnionType::default()
});
schema.build()
})
}
}
```
Expand Down
6 changes: 3 additions & 3 deletions crates/macros/src/common/container.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl<'l> Container<'l> {
#description
schema.structure(StructType::new([
#(#schema_types),*
]));
]))
}
}
Self::Enum { variants } => {
Expand Down Expand Up @@ -78,7 +78,7 @@ impl<'l> Container<'l> {
#(#variants_types),*
],
#default_index,
));
))
}
} else {
quote! {
Expand All @@ -88,7 +88,7 @@ impl<'l> Container<'l> {
#(#variants_types),*
],
#default_index,
));
))
}
}
}
Expand Down
1 change: 0 additions & 1 deletion crates/macros/src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ impl<'l> ToTokens for ConfigMacro<'l> {
use schematic::schema::*;

#schema_impl
schema.build()
}
}

Expand Down
3 changes: 1 addition & 2 deletions crates/macros/src/config_enum/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ pub fn macro_impl(item: TokenStream) -> TokenStream {
#(#schema_types),*
],
#default_index,
));
schema.build()
))
}
}
});
Expand Down
1 change: 0 additions & 1 deletion crates/macros/src/schematic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ impl<'l> ToTokens for SchematicMacro<'l> {
use schematic::schema::*;

#schema_impl
schema.build()
}
}
});
Expand Down
3 changes: 1 addition & 2 deletions crates/schematic/src/config/configs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ impl Schematic for ExtendsFrom {
schema.union(UnionType::new_any([
schema.infer::<String>(),
schema.infer::<Vec<String>>(),
]));
schema.build()
]))
}
}
15 changes: 5 additions & 10 deletions crates/types/src/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@ impl ArrayType {

impl<T: Schematic> Schematic for Vec<T> {
fn build_schema(mut schema: SchemaBuilder) -> Schema {
schema.array(ArrayType::new(schema.infer::<T>()));
schema.build()
schema.array(ArrayType::new(schema.infer::<T>()))
}
}

impl<T: Schematic> Schematic for &[T] {
fn build_schema(mut schema: SchemaBuilder) -> Schema {
schema.array(ArrayType::new(schema.infer::<T>()));
schema.build()
schema.array(ArrayType::new(schema.infer::<T>()))
}
}

Expand All @@ -43,8 +41,7 @@ impl<T: Schematic, const N: usize> Schematic for [T; N] {
max_length: Some(N),
min_length: Some(N),
..ArrayType::default()
});
schema.build()
})
}
}

Expand All @@ -54,8 +51,7 @@ impl<T: Schematic, S> Schematic for HashSet<T, S> {
items_type: Box::new(schema.infer::<T>()),
unique: Some(true),
..ArrayType::default()
});
schema.build()
})
}
}

Expand All @@ -65,7 +61,6 @@ impl<T: Schematic> Schematic for BTreeSet<T> {
items_type: Box::new(schema.infer::<T>()),
unique: Some(true),
..ArrayType::default()
});
schema.build()
})
}
}
3 changes: 1 addition & 2 deletions crates/types/src/bools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
30 changes: 10 additions & 20 deletions crates/types/src/externals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ macro_rules! impl_set {
($type:ty) => {
impl<T: Schematic, S> Schematic for $type {
fn build_schema(mut schema: SchemaBuilder) -> Schema {
schema.array(ArrayType::new(schema.infer::<T>()));
schema.build()
schema.array(ArrayType::new(schema.infer::<T>()))
}
}
};
Expand All @@ -23,8 +22,7 @@ macro_rules! impl_map {
($type:ty) => {
impl<K: Schematic, V: Schematic, S> Schematic for $type {
fn build_schema(mut schema: SchemaBuilder) -> Schema {
schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()));
schema.build()
schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()))
}
}
};
Expand All @@ -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()
}
}
};
Expand All @@ -48,8 +45,7 @@ macro_rules! impl_string_format {
schema.string(StringType {
format: Some($format.into()),
..StringType::default()
});
schema.build()
})
}
}
};
Expand All @@ -66,8 +62,7 @@ mod chrono_feature {
schema.string(StringType {
format: Some($format.into()),
..StringType::default()
});
schema.build()
})
}
}
};
Expand Down Expand Up @@ -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<K: Schematic, V: Schematic> Schematic for serde_json::Map<K, V> {
fn build_schema(mut schema: SchemaBuilder) -> Schema {
schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()));
schema.build()
schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()))
}
}
}
Expand All @@ -154,8 +147,7 @@ mod serde_toml_feature {

impl<K: Schematic, V: Schematic> Schematic for toml::map::Map<K, V> {
fn build_schema(mut schema: SchemaBuilder) -> Schema {
schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()));
schema.build()
schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()))
}
}
}
Expand All @@ -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))
}
}

Expand All @@ -179,8 +170,7 @@ mod serde_yaml_feature {
schema.object(ObjectType::new(
schema.infer::<serde_yaml::Value>(),
schema.infer::<serde_yaml::Value>(),
));
schema.build()
))
}
}
}
Expand Down
8 changes: 3 additions & 5 deletions crates/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand All @@ -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)
}
}

Expand Down Expand Up @@ -86,7 +85,6 @@ impl<T: Schematic> Schematic for Arc<T> {

impl<T: Schematic> Schematic for Option<T> {
fn build_schema(mut schema: SchemaBuilder) -> Schema {
schema.union(UnionType::new_any([schema.infer::<T>(), Schema::null()]));
schema.build()
schema.union(UnionType::new_any([schema.infer::<T>(), Schema::null()]))
}
}
6 changes: 2 additions & 4 deletions crates/types/src/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
};
Expand Down Expand Up @@ -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))
}
}
};
Expand Down
Loading