Skip to content

Commit

Permalink
fix: Serde optimizations. (#137)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Aug 19, 2024
1 parent 31e7248 commit 5a63776
Show file tree
Hide file tree
Showing 16 changed files with 270 additions and 57 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

#### 🐞 Fixes

- More serde optimizations and fixes.

## 0.17.2

#### 🚀 Updates
Expand Down
30 changes: 24 additions & 6 deletions crates/types/src/arrays.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,42 @@ use std::collections::{BTreeSet, HashSet};
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct ArrayType {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub contains: Option<bool>,

pub items_type: Box<Schema>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub max_contains: Option<usize>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub max_length: Option<usize>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub min_contains: Option<usize>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub min_length: Option<usize>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub unique: Option<bool>,
}

Expand Down
5 changes: 4 additions & 1 deletion crates/types/src/bools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use crate::*;
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct BooleanType {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub default: Option<LiteralValue>,
}

Expand Down
10 changes: 8 additions & 2 deletions crates/types/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@ pub use indexmap::IndexMap;
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct EnumType {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub default_index: Option<usize>,

pub values: Vec<LiteralValue>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub variants: Option<IndexMap<String, Box<SchemaField>>>,
}

Expand Down
5 changes: 4 additions & 1 deletion crates/types/src/literals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ pub enum LiteralValue {
#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct LiteralType {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub format: Option<String>,

pub value: LiteralValue,
Expand Down
85 changes: 68 additions & 17 deletions crates/types/src/numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,30 +30,54 @@ impl IntegerKind {
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct IntegerType {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub default: Option<LiteralValue>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub enum_values: Option<Vec<isize>>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub format: Option<String>,

pub kind: IntegerKind,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub max: Option<isize>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub max_exclusive: Option<isize>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub min: Option<isize>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub min_exclusive: Option<isize>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub multiple_of: Option<isize>,
}

Expand Down Expand Up @@ -124,33 +148,60 @@ pub enum FloatKind {
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct FloatType {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub default: Option<LiteralValue>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub enum_values: Option<Vec<f64>>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub format: Option<String>,

pub kind: FloatKind,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub max: Option<f64>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub max_exclusive: Option<f64>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub min: Option<f64>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub min_exclusive: Option<f64>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub multiple_of: Option<f64>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub name: Option<String>,
}

Expand Down
15 changes: 12 additions & 3 deletions crates/types/src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@ use std::collections::{BTreeMap, HashMap};
pub struct ObjectType {
pub key_type: Box<Schema>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub max_length: Option<usize>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub min_length: Option<usize>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub required: Option<Vec<String>>,

pub value_type: Box<Schema>,
Expand Down
45 changes: 38 additions & 7 deletions crates/types/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,25 @@ use std::ops::{Deref, DerefMut};
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Schema {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub deprecated: Option<String>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub description: Option<String>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub name: Option<String>,

#[cfg_attr(feature = "serde", serde(skip_serializing, default))]
#[cfg_attr(feature = "serde", serde(default, skip_serializing))]
pub nullable: bool,

pub ty: SchemaType,
Expand Down Expand Up @@ -181,25 +190,47 @@ impl From<Schema> for SchemaType {

impl Schematic for Schema {}

fn is_false(value: &bool) -> bool {
!value
}

/// Describes the metadata and shape of a field within a struct or enum.
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct SchemaField {
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub comment: Option<String>,

pub schema: Schema,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub deprecated: Option<String>,

#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
#[cfg_attr(
feature = "serde",
serde(default, skip_serializing_if = "Option::is_none")
)]
pub env_var: Option<String>,

#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "is_false"))]
pub hidden: bool,

#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "is_false"))]
pub nullable: bool,

#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "is_false"))]
pub optional: bool,

#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "is_false"))]
pub read_only: bool,

#[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "is_false"))]
pub write_only: bool,
}

Expand Down
5 changes: 5 additions & 0 deletions crates/types/src/schema_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ pub struct SchemaBuilder {
}

impl SchemaBuilder {
/// Generate a schema from the provided type.
pub fn generate<T: Schematic>() -> Schema {
Self::build_root::<T>()
}

/// Generate a schema from the provided type.
pub fn build_root<T: Schematic>() -> Schema {
let mut builder = SchemaBuilder::default();
Expand Down
Loading

0 comments on commit 5a63776

Please sign in to comment.