From 84ff8acc68b158c136bd0310f831e3b5ad7d9a22 Mon Sep 17 00:00:00 2001 From: Dominic Date: Tue, 1 Oct 2019 01:11:43 +0200 Subject: [PATCH] add chrono support --- Cargo.lock | 1 + Cargo.toml | 3 ++- src/openapi/types.rs | 28 ++++++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c4f12c1..944d6ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -331,6 +331,7 @@ dependencies = [ name = "gotham-restful" version = "0.0.1" dependencies = [ + "chrono 0.4.9 (registry+https://github.com/rust-lang/crates.io-index)", "failure 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", "fake 2.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)", diff --git a/Cargo.toml b/Cargo.toml index c798ee3..0803aff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,6 +16,7 @@ repository = "https://gitlab.com/msrd0/gotham-restful" gitlab = { repository = "msrd0/gotham-restful", branch = "master" } [dependencies] +chrono = { version = "0.4", optional = true } failure = "0.1" futures = "0.1" gotham = "0.4" @@ -34,5 +35,5 @@ log = "0.4" log4rs = { version = "0.8", features = ["console_appender"], default-features = false } [features] -default = ["openapi"] +default = ["openapi", "chrono"] openapi = ["indexmap", "log", "openapiv3"] diff --git a/src/openapi/types.rs b/src/openapi/types.rs index 3b70003..1338526 100644 --- a/src/openapi/types.rs +++ b/src/openapi/types.rs @@ -1,5 +1,10 @@ +#[cfg(feature = "chrono")] +use chrono::{ + Date, DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, Utc +}; use openapiv3::{ - ArrayType, IntegerType, NumberType, ObjectType, ReferenceOr::Item, Schema, SchemaData, SchemaKind, StringType, Type + ArrayType, IntegerType, NumberType, ObjectType, ReferenceOr::Item, Schema, SchemaData, SchemaKind, + StringFormat, StringType, Type, VariantOrUnknownOrEmpty }; @@ -66,7 +71,21 @@ macro_rules! str_types { SchemaKind::Type(Type::String(StringType::default())) } } - )*} + )*}; + + (format = $format:ident, $($str_ty:ty),*) => {$( + impl OpenapiType for $str_ty + { + fn to_schema() -> SchemaKind + { + SchemaKind::Type(Type::String(StringType { + format: VariantOrUnknownOrEmpty::Item(StringFormat::$format), + pattern: None, + enumeration: Vec::new() + })) + } + } + )*}; } str_types!(String, &str); @@ -91,3 +110,8 @@ impl OpenapiType for Vec })) } } + +#[cfg(feature = "chrono")] +str_types!(format = Date, Date, Date, Date, NaiveDate); +#[cfg(feature = "chrono")] +str_types!(format = DateTime, DateTime, DateTime, DateTime, NaiveDateTime);