Skip to content

Commit

Permalink
feat: Add impls for the time crate.
Browse files Browse the repository at this point in the history
  • Loading branch information
jetuk committed Sep 2, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 83f8fa9 commit b1bbd63
Showing 6 changed files with 128 additions and 0 deletions.
40 changes: 40 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions schemars/Cargo.toml
Original file line number Diff line number Diff line change
@@ -33,6 +33,7 @@ smallvec1 = { version = "1.0", default-features = false, optional = true, packag
smol_str02 = { version = "0.2.1", default-features = false, optional = true, package = "smol_str" }
url2 = { version = "2.0", default-features = false, optional = true, package = "url" }
uuid1 = { version = "1.0", default-features = false, optional = true, package = "uuid" }
time = { version = "0.3", default-features = false, optional = true }

[dev-dependencies]
pretty_assertions = "1.2.1"
@@ -120,6 +121,10 @@ required-features = ["semver1"]
name = "decimal"
required-features = ["rust_decimal1", "bigdecimal04"]

[[test]]
name = "time"
required-features = ["time"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--extend-css", "docs-rs-custom.css"]
1 change: 1 addition & 0 deletions schemars/src/json_schema_impls/mod.rs
Original file line number Diff line number Diff line change
@@ -48,6 +48,7 @@ mod primitives;
mod sequences;
mod serdejson;
mod std_time;
#[cfg(feature = "time")]
mod time;
mod tuple;
mod wrapper;
35 changes: 35 additions & 0 deletions schemars/src/json_schema_impls/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use crate::SchemaGenerator;
use crate::{json_schema, JsonSchema, Schema};
use alloc::borrow::Cow;
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};

macro_rules! formatted_string_impl {
($ty:ident, $format:literal) => {
formatted_string_impl!($ty, $format, JsonSchema for $ty);
};
($ty:ident, $format:literal, $($desc:tt)+) => {
impl $($desc)+ {
always_inline!();

fn schema_name() -> Cow<'static, str> {
stringify!($ty).into()
}

fn schema_id() -> Cow<'static, str> {
stringify!(time::$ty).into()
}

fn json_schema(_: &mut SchemaGenerator) -> Schema {
json_schema!({
"type": "string",
"format": $format
})
}
}
};
}

formatted_string_impl!(Date, "date");
formatted_string_impl!(PrimitiveDateTime, "partial-date-time");
formatted_string_impl!(Time, "time");
formatted_string_impl!(OffsetDateTime, "date-time");
29 changes: 29 additions & 0 deletions schemars/tests/expected/time-types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"title": "TimeTypes",
"type": "object",
"properties": {
"date": {
"type": "string",
"format": "date"
},
"offset_date_time": {
"type": "string",
"format": "date-time"
},
"primitive_date_time": {
"type": "string",
"format": "partial-date-time"
},
"time": {
"type": "string",
"format": "time"
}
},
"required": [
"date",
"offset_date_time",
"primitive_date_time",
"time"
]
}
18 changes: 18 additions & 0 deletions schemars/tests/time.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
mod util;
use schemars::JsonSchema;
use time::{Date, OffsetDateTime, PrimitiveDateTime, Time};
use util::*;

#[allow(dead_code)]
#[derive(JsonSchema)]
struct TimeTypes {
date: Date,
offset_date_time: OffsetDateTime,
primitive_date_time: PrimitiveDateTime,
time: Time,
}

#[test]
fn time_types() -> TestResult {
test_default_generated_schema::<TimeTypes>("time-types")
}

0 comments on commit b1bbd63

Please sign in to comment.