From b2c6718a606f34677bf5ed968c294cbda0b553a6 Mon Sep 17 00:00:00 2001 From: Ulysse Carion Date: Sun, 31 Jan 2021 09:20:49 -0800 Subject: [PATCH] Add Schema::into_serde_schema (#15) * Add Schema::into_serde_schema * Prepare release 0.3.1 --- Cargo.toml | 2 +- src/schema.rs | 195 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 196 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 61137c7..158a48c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "jtd" -version = "0.3.0" +version = "0.3.1" description = "A Rust implementation of JSON Type Definition" authors = ["JSON Type Definition Contributors"] edition = "2018" diff --git a/src/schema.rs b/src/schema.rs index a24fb41..1798ca7 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -576,6 +576,201 @@ const VALID_FORM_SIGNATURES: [[bool; 10]; 13] = [ ]; impl Schema { + /// Converts a [`Schema`] into a [`SerdeSchema`]. + /// + /// ``` + /// use jtd::{Schema, SerdeSchema, Type}; + /// + /// assert_eq!( + /// SerdeSchema { + /// type_: Some("uint8".to_owned()), + /// ..Default::default() + /// }, + /// Schema::Type { + /// definitions: Default::default(), + /// metadata: Default::default(), + /// nullable: false, + /// type_: Type::Uint8, + /// }.into_serde_schema(), + /// ); + /// ``` + pub fn into_serde_schema(self) -> SerdeSchema { + let mut serde_schema: SerdeSchema = Default::default(); + + match self { + Schema::Empty { + definitions, + metadata, + } => { + serde_schema.definitions = Self::definitions_into_serde_schema(definitions); + serde_schema.metadata = Self::metadata_into_serde_schema(metadata); + } + + Schema::Ref { + definitions, + metadata, + nullable, + ref_, + } => { + serde_schema.definitions = Self::definitions_into_serde_schema(definitions); + serde_schema.metadata = Self::metadata_into_serde_schema(metadata); + serde_schema.nullable = Self::nullable_into_serde_schema(nullable); + serde_schema.ref_ = Some(ref_); + } + + Schema::Type { + definitions, + metadata, + nullable, + type_, + } => { + serde_schema.definitions = Self::definitions_into_serde_schema(definitions); + serde_schema.metadata = Self::metadata_into_serde_schema(metadata); + serde_schema.nullable = Self::nullable_into_serde_schema(nullable); + serde_schema.type_ = Some( + match type_ { + Type::Boolean => "boolean", + Type::Int8 => "int8", + Type::Uint8 => "uint8", + Type::Int16 => "int16", + Type::Uint16 => "uint16", + Type::Int32 => "int32", + Type::Uint32 => "uint32", + Type::Float32 => "float32", + Type::Float64 => "float64", + Type::String => "string", + Type::Timestamp => "timestamp", + } + .to_owned(), + ); + } + + Schema::Enum { + definitions, + metadata, + nullable, + enum_, + } => { + serde_schema.definitions = Self::definitions_into_serde_schema(definitions); + serde_schema.metadata = Self::metadata_into_serde_schema(metadata); + serde_schema.nullable = Self::nullable_into_serde_schema(nullable); + serde_schema.enum_ = Some(enum_.into_iter().collect()); + } + + Schema::Elements { + definitions, + metadata, + nullable, + elements, + } => { + serde_schema.definitions = Self::definitions_into_serde_schema(definitions); + serde_schema.metadata = Self::metadata_into_serde_schema(metadata); + serde_schema.nullable = Self::nullable_into_serde_schema(nullable); + serde_schema.elements = Some(Box::new(elements.into_serde_schema())); + } + + Schema::Properties { + definitions, + metadata, + nullable, + properties, + optional_properties, + properties_is_present, + additional_properties, + } => { + serde_schema.definitions = Self::definitions_into_serde_schema(definitions); + serde_schema.metadata = Self::metadata_into_serde_schema(metadata); + serde_schema.nullable = Self::nullable_into_serde_schema(nullable); + + if properties_is_present { + serde_schema.properties = Some( + properties + .into_iter() + .map(|(k, v)| (k, v.into_serde_schema())) + .collect(), + ); + } + + if !optional_properties.is_empty() { + serde_schema.optional_properties = Some( + optional_properties + .into_iter() + .map(|(k, v)| (k, v.into_serde_schema())) + .collect(), + ); + } + + if additional_properties { + serde_schema.additional_properties = Some(additional_properties); + } + } + + Schema::Values { + definitions, + metadata, + nullable, + values, + } => { + serde_schema.definitions = Self::definitions_into_serde_schema(definitions); + serde_schema.metadata = Self::metadata_into_serde_schema(metadata); + serde_schema.nullable = Self::nullable_into_serde_schema(nullable); + serde_schema.values = Some(Box::new(values.into_serde_schema())); + } + + Schema::Discriminator { + definitions, + metadata, + nullable, + discriminator, + mapping, + } => { + serde_schema.definitions = Self::definitions_into_serde_schema(definitions); + serde_schema.metadata = Self::metadata_into_serde_schema(metadata); + serde_schema.nullable = Self::nullable_into_serde_schema(nullable); + serde_schema.discriminator = Some(discriminator); + serde_schema.mapping = Some( + mapping + .into_iter() + .map(|(k, v)| (k, v.into_serde_schema())) + .collect(), + ); + } + } + + serde_schema + } + + fn definitions_into_serde_schema( + definitions: Definitions, + ) -> Option> { + if definitions.is_empty() { + None + } else { + Some( + definitions + .into_iter() + .map(|(k, v)| (k, v.into_serde_schema())) + .collect(), + ) + } + } + + fn metadata_into_serde_schema(metadata: Metadata) -> Option> { + if metadata.is_empty() { + None + } else { + Some(metadata) + } + } + + fn nullable_into_serde_schema(nullable: bool) -> Option { + if nullable { + Some(true) + } else { + None + } + } + /// Constructs a [`Schema`] from a [`SerdeSchema`]. /// /// ```