Skip to content

Commit 95c2aa2

Browse files
authored
Add (optional) support for time 0.3 (#21)
1 parent ba2a0a9 commit 95c2aa2

File tree

6 files changed

+49
-33
lines changed

6 files changed

+49
-33
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ serde_json = "1.0"
2323

2424
# optional dependencies / features
2525
chrono = { version = "0.4.19", default-features = false, optional = true }
26-
uuid = { version = "0.8.2" , optional = true }
26+
time = { version = "0.3.4", features = ["serde-human-readable"], optional = true }
27+
uuid = { version = "0.8.2", optional = true }
2728

2829
[dev-dependencies]
2930
paste = "1.0"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ This repository contains the following crates:
3232

3333
# openapi_type
3434

35-
This crate gives static type information for primitives and commonly used types from the standard library and a few other commonly used libraries like `chrono` and `uuid`. Also, it provides a derive macro for structs and enums to gain access to their static type information at runtime.
35+
This crate gives static type information for primitives and commonly used types from the standard library and a few other commonly used libraries like `chrono`, `time` and `uuid`. Also, it provides a derive macro for structs and enums to gain access to their static type information at runtime.
3636

3737
The core of this crate is the [`OpenapiType`][__link0] trait. It has one static function, [`schema`][__link1], which returns an [`OpenapiSchema`][__link2]. This assembles the static type information in a way that is convenient to use for a generated OpenAPI specification, but can also be utilized in other use cases as well.
3838

crates-io.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# openapi_type [![Rust 1.49+](https://img.shields.io/badge/rustc-1.49+-orange.svg)](https://blog.rust-lang.org/2020/12/31/Rust-1.49.0.html) [![License Apache-2.0](https://img.shields.io/badge/license-Apache--2.0-blue.svg)](https://www.apache.org/licenses/LICENSE-2.0) [![GitHub](https://img.shields.io/badge/Code-On%20Github-blue?logo=GitHub)](https://github.com/msrd0/openapi_type)
22

3-
This crate gives static type information for primitives and commonly used types from the standard library and a few other commonly used libraries like `chrono` and `uuid`. Also, it provides a derive macro for structs and enums to gain access to their static type information at runtime.
3+
This crate gives static type information for primitives and commonly used types from the standard library and a few other commonly used libraries like `chrono`, `time` and `uuid`. Also, it provides a derive macro for structs and enums to gain access to their static type information at runtime.
44

55
The core of this crate is the [`OpenapiType`][__link0] trait. It has one static function, [`schema`][__link1], which returns an [`OpenapiSchema`][__link2]. This assembles the static type information in a way that is convenient to use for a generated OpenAPI specification, but can also be utilized in other use cases as well.
66

src/impls.rs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use crate::{OpenapiSchema, OpenapiType};
2-
#[cfg(feature = "chrono")]
3-
use chrono::{offset::TimeZone, Date, DateTime, NaiveDate, NaiveDateTime};
42
use indexmap::{IndexMap, IndexSet};
53
use openapiv3::{
64
AdditionalProperties, ArrayType, IntegerType, NumberFormat, NumberType, ObjectType, ReferenceOr, SchemaKind,
@@ -12,13 +10,11 @@ use std::{
1210
hash::BuildHasher,
1311
num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize}
1412
};
15-
#[cfg(feature = "uuid")]
16-
use uuid::Uuid;
1713

1814
macro_rules! impl_openapi_type {
19-
($($ty:ident $(<$($generic:ident : $bound:path),+>)*),* => $schema:expr) => {
15+
($($($ty:ident)::+ $(<$($generic:ident : $bound:path),+>)?),* => $schema:expr) => {
2016
$(
21-
impl $(<$($generic : $bound),+>)* OpenapiType for $ty $(<$($generic),+>)* {
17+
impl $(<$($generic : $bound),+>)? OpenapiType for $($ty)::+ $(<$($generic),+>)? {
2218
fn schema() -> OpenapiSchema {
2319
$schema
2420
}
@@ -101,17 +97,27 @@ fn str_schema(format: VariantOrUnknownOrEmpty<StringFormat>) -> OpenapiSchema {
10197
impl_openapi_type!(String, str => str_schema(VariantOrUnknownOrEmpty::Empty));
10298

10399
#[cfg(feature = "chrono")]
104-
impl_openapi_type!(Date<T: TimeZone>, NaiveDate => {
100+
impl_openapi_type!(chrono::Date<T: chrono::TimeZone>, chrono::NaiveDate => {
101+
str_schema(VariantOrUnknownOrEmpty::Item(StringFormat::Date))
102+
});
103+
104+
#[cfg(feature = "time")]
105+
impl_openapi_type!(time::Date => {
105106
str_schema(VariantOrUnknownOrEmpty::Item(StringFormat::Date))
106107
});
107108

108109
#[cfg(feature = "chrono")]
109-
impl_openapi_type!(DateTime<T: TimeZone>, NaiveDateTime => {
110+
impl_openapi_type!(chrono::DateTime<T: chrono::TimeZone>, chrono::NaiveDateTime => {
111+
str_schema(VariantOrUnknownOrEmpty::Item(StringFormat::DateTime))
112+
});
113+
114+
#[cfg(feature = "time")]
115+
impl_openapi_type!(time::OffsetDateTime, time::PrimitiveDateTime => {
110116
str_schema(VariantOrUnknownOrEmpty::Item(StringFormat::DateTime))
111117
});
112118

113119
#[cfg(feature = "uuid")]
114-
impl_openapi_type!(Uuid => {
120+
impl_openapi_type!(uuid::Uuid => {
115121
str_schema(VariantOrUnknownOrEmpty::Unknown("uuid".to_owned()))
116122
});
117123

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
#![cfg_attr(feature = "cargo-clippy", allow(clippy::tabs_in_doc_comments))]
44
#![doc = r##"
55
This crate gives static type information for primitives and commonly used types from the standard
6-
library and a few other commonly used libraries like `chrono` and `uuid`. Also, it provides a
7-
derive macro for structs and enums to gain access to their static type information at runtime.
6+
library and a few other commonly used libraries like `chrono`, `time` and `uuid`. Also, it provides
7+
a derive macro for structs and enums to gain access to their static type information at runtime.
88
99
The core of this crate is the [`OpenapiType`] trait. It has one static function,
1010
[`schema`](OpenapiType::schema), which returns an [`OpenapiSchema`]. This assembles the static

tests/std_types.rs

Lines changed: 28 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
#[cfg(feature = "chrono")]
2-
use chrono::{Date, DateTime, FixedOffset, NaiveDate, NaiveDateTime, Utc};
31
use indexmap::{IndexMap, IndexSet};
42
use openapi_type::OpenapiType;
53
use serde_json::Value;
64
use std::{
75
collections::{BTreeMap, BTreeSet, HashMap, HashSet},
86
num::{NonZeroU128, NonZeroU16, NonZeroU32, NonZeroU64, NonZeroU8, NonZeroUsize}
97
};
10-
#[cfg(feature = "uuid")]
11-
use uuid::Uuid;
12-
138
macro_rules! test_type {
14-
($($ty:ident $(<$($generic:ident),+>)*),* = $json:tt) => {
15-
paste::paste! { $(
16-
#[test]
17-
fn [< $ty:lower $($(_ $generic:lower)+)* >]() {
18-
let schema = <$ty $(<$($generic),+>)* as OpenapiType>::schema();
19-
let schema = openapi_type::OpenapiSchema::into_schema(schema);
20-
let schema_json = serde_json::to_value(&schema).unwrap();
21-
let expected = serde_json::json!($json);
22-
pretty_assertions::assert_eq!(schema_json, expected);
23-
}
24-
)* }
9+
($($($ty:ident)::+ $(<$($($generic:ident)::+),+>)?),* = $json:tt) => {
10+
paste::paste! {
11+
$(
12+
#[test]
13+
fn [<$($ty:lower)_+ $($($(_$generic:lower)+)+)? >]() {
14+
let schema = <$($ty)::+ $(<$($($generic)::+),+>)? as OpenapiType>::schema();
15+
let schema = openapi_type::OpenapiSchema::into_schema(schema);
16+
let schema_json = serde_json::to_value(&schema).unwrap();
17+
let expected = serde_json::json!($json);
18+
pretty_assertions::assert_eq!(schema_json, expected);
19+
}
20+
)*
21+
}
2522
};
2623
}
2724

@@ -161,21 +158,33 @@ test_type!(String = {
161158
});
162159

163160
#[cfg(feature = "uuid")]
164-
test_type!(Uuid = {
161+
test_type!(uuid::Uuid = {
165162
"type": "string",
166163
"format": "uuid"
167164
});
168165

169166
// ### date/time
170167

171168
#[cfg(feature = "chrono")]
172-
test_type!(Date<FixedOffset>, Date<Utc>, NaiveDate = {
169+
test_type!(chrono::Date<chrono::FixedOffset>, chrono::Date<chrono::Utc>, chrono::NaiveDate = {
170+
"type": "string",
171+
"format": "date"
172+
});
173+
174+
#[cfg(feature = "time")]
175+
test_type!(time::Date = {
173176
"type": "string",
174177
"format": "date"
175178
});
176179

177180
#[cfg(feature = "chrono")]
178-
test_type!(DateTime<FixedOffset>, DateTime<Utc>, NaiveDateTime = {
181+
test_type!(chrono::DateTime<chrono::FixedOffset>, chrono::DateTime<chrono::Utc>, chrono::NaiveDateTime = {
182+
"type": "string",
183+
"format": "date-time"
184+
});
185+
186+
#[cfg(feature = "time")]
187+
test_type!(time::OffsetDateTime, time::PrimitiveDateTime = {
179188
"type": "string",
180189
"format": "date-time"
181190
});

0 commit comments

Comments
 (0)