Skip to content

Commit

Permalink
Add impls.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Apr 25, 2024
1 parent 2c3c54a commit 5ecba30
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 1,903 deletions.
1,872 changes: 147 additions & 1,725 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
resolver = "2"
members = ["crates/*"]
exclude = ["crates/schematic", "crates/test-app"]

[workspace.dependencies]
chrono = "0.4.37"
Expand Down
68 changes: 36 additions & 32 deletions crates/types/src/arrays.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::schema_type::SchemaType;
use crate::Schematic;
use crate::{Schema, SchemaBuilder, Schematic};
use std::collections::{BTreeSet, HashSet};

#[derive(Clone, Debug, Default)]
Expand All @@ -23,38 +23,42 @@ impl ArrayType {
}
}

// macro_rules! impl_list {
// ($type:ident) => {
// impl<T: Schematic> Schematic for $type<T> {
// fn generate_schema() -> SchemaType {
// SchemaType::array(T::generate_schema())
// }
// }
// };
// }
macro_rules! impl_list {
($type:ident) => {
impl<T: Schematic> Schematic for $type<T> {
fn generate_schema(mut schema: SchemaBuilder) -> Schema {
schema.array(ArrayType::new(schema.infer::<T>()));
schema.build()
}
}
};
}

// impl_list!(Vec);
// impl_list!(BTreeSet);
impl_list!(Vec);
impl_list!(BTreeSet);

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

// impl<T: Schematic, const N: usize> Schematic for [T; N] {
// fn generate_schema() -> SchemaType {
// SchemaType::Array(Box::new(ArrayType {
// items_type: Box::new(T::generate_schema()),
// max_length: Some(N),
// min_length: Some(N),
// ..ArrayType::default()
// }))
// }
// }
impl<T: Schematic, const N: usize> Schematic for [T; N] {
fn generate_schema(mut schema: SchemaBuilder) -> Schema {
schema.array(ArrayType {
items_type: Box::new(schema.infer::<T>()),
max_length: Some(N),
min_length: Some(N),
..ArrayType::default()
});
schema.build()
}
}

// impl<T: Schematic, S> Schematic for HashSet<T, S> {
// fn generate_schema() -> SchemaType {
// SchemaType::array(T::generate_schema())
// }
// }
impl<T: Schematic, S> Schematic for HashSet<T, S> {
fn generate_schema(mut schema: SchemaBuilder) -> Schema {
schema.array(ArrayType::new(schema.infer::<T>()));
schema.build()
}
}
4 changes: 4 additions & 0 deletions crates/types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub use unions::*;

/// Defines a schema that represents the shape of the implementing type.
pub trait Schematic {
fn schema_id() -> Option<String> {
None
}

/// 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 generate_schema(schema: SchemaBuilder) -> Schema {
Expand Down
81 changes: 41 additions & 40 deletions crates/types/src/numbers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::literals::LiteralValue;
use crate::schema_type::SchemaType;
use crate::Schematic;
use crate::{Schema, SchemaBuilder, Schematic};

#[derive(Clone, Debug, Default)]
pub enum IntegerKind {
Expand Down Expand Up @@ -65,37 +64,38 @@ impl IntegerType {
}

/// Create an integer schema with the provided kind.
pub fn new_kind(kind: IntegerKind) -> SchemaType {
SchemaType::Integer(Box::new(IntegerType {
pub fn new_kind(kind: IntegerKind) -> Self {
IntegerType {
kind,
..IntegerType::default()
}))
}
}
}

// macro_rules! impl_int {
// ($type:ty, $kind:expr) => {
// impl Schematic for $type {
// fn generate_schema() -> SchemaType {
// SchemaType::integer($kind)
// }
// }
// };
// }

// impl_int!(usize, IntegerKind::Usize);
// impl_int!(u8, IntegerKind::U8);
// impl_int!(u16, IntegerKind::U16);
// impl_int!(u32, IntegerKind::U32);
// impl_int!(u64, IntegerKind::U64);
// impl_int!(u128, IntegerKind::U128);

// impl_int!(isize, IntegerKind::Isize);
// impl_int!(i8, IntegerKind::I8);
// impl_int!(i16, IntegerKind::I16);
// impl_int!(i32, IntegerKind::I32);
// impl_int!(i64, IntegerKind::I64);
// impl_int!(i128, IntegerKind::I128);
macro_rules! impl_int {
($type:ty, $kind:expr) => {
impl Schematic for $type {
fn generate_schema(mut schema: SchemaBuilder) -> Schema {
schema.integer(IntegerType::new_kind($kind));
schema.build()
}
}
};
}

impl_int!(usize, IntegerKind::Usize);
impl_int!(u8, IntegerKind::U8);
impl_int!(u16, IntegerKind::U16);
impl_int!(u32, IntegerKind::U32);
impl_int!(u64, IntegerKind::U64);
impl_int!(u128, IntegerKind::U128);

impl_int!(isize, IntegerKind::Isize);
impl_int!(i8, IntegerKind::I8);
impl_int!(i16, IntegerKind::I16);
impl_int!(i32, IntegerKind::I32);
impl_int!(i64, IntegerKind::I64);
impl_int!(i128, IntegerKind::I128);

#[derive(Clone, Debug, Default)]
pub enum FloatKind {
Expand Down Expand Up @@ -146,15 +146,16 @@ impl FloatType {
}
}

// macro_rules! impl_float {
// ($type:ty, $kind:expr) => {
// impl Schematic for $type {
// fn generate_schema() -> SchemaType {
// SchemaType::float($kind)
// }
// }
// };
// }

// impl_float!(f32, FloatKind::F32);
// impl_float!(f64, FloatKind::F64);
macro_rules! impl_float {
($type:ty, $kind:expr) => {
impl Schematic for $type {
fn generate_schema(mut schema: SchemaBuilder) -> Schema {
schema.float(FloatType::new_kind($kind));
schema.build()
}
}
};
}

impl_float!(f32, FloatKind::F32);
impl_float!(f64, FloatKind::F64);
24 changes: 13 additions & 11 deletions crates/types/src/objects.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::schema_type::SchemaType;
use crate::Schematic;
use crate::{Schema, SchemaBuilder, Schematic};
use std::collections::{BTreeMap, HashMap};

#[derive(Clone, Debug, Default)]
Expand All @@ -22,14 +22,16 @@ impl ObjectType {
}
}

// impl<K: Schematic, V: Schematic> Schematic for BTreeMap<K, V> {
// fn generate_schema() -> SchemaType {
// SchemaType::object(K::generate_schema(), V::generate_schema())
// }
// }
impl<K: Schematic, V: Schematic> Schematic for BTreeMap<K, V> {
fn generate_schema(mut schema: SchemaBuilder) -> Schema {
schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()));
schema.build()
}
}

// impl<K: Schematic, V: Schematic, S> Schematic for HashMap<K, V, S> {
// fn generate_schema() -> SchemaType {
// SchemaType::object(K::generate_schema(), V::generate_schema())
// }
// }
impl<K: Schematic, V: Schematic, S> Schematic for HashMap<K, V, S> {
fn generate_schema(mut schema: SchemaBuilder) -> Schema {
schema.object(ObjectType::new(schema.infer::<K>(), schema.infer::<V>()));
schema.build()
}
}
Loading

0 comments on commit 5ecba30

Please sign in to comment.