1
0
Fork 0
mirror of https://gitlab.com/msrd0/gotham-restful.git synced 2025-02-23 13:02:28 +00:00

add chrono support

This commit is contained in:
Dominic 2019-10-01 01:11:43 +02:00
parent 887d874063
commit 84ff8acc68
Signed by: msrd0
GPG key ID: DCC8C247452E98F9
3 changed files with 29 additions and 3 deletions

1
Cargo.lock generated
View file

@ -331,6 +331,7 @@ dependencies = [
name = "gotham-restful" name = "gotham-restful"
version = "0.0.1" version = "0.0.1"
dependencies = [ 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)", "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)", "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)", "futures 0.1.29 (registry+https://github.com/rust-lang/crates.io-index)",

View file

@ -16,6 +16,7 @@ repository = "https://gitlab.com/msrd0/gotham-restful"
gitlab = { repository = "msrd0/gotham-restful", branch = "master" } gitlab = { repository = "msrd0/gotham-restful", branch = "master" }
[dependencies] [dependencies]
chrono = { version = "0.4", optional = true }
failure = "0.1" failure = "0.1"
futures = "0.1" futures = "0.1"
gotham = "0.4" gotham = "0.4"
@ -34,5 +35,5 @@ log = "0.4"
log4rs = { version = "0.8", features = ["console_appender"], default-features = false } log4rs = { version = "0.8", features = ["console_appender"], default-features = false }
[features] [features]
default = ["openapi"] default = ["openapi", "chrono"]
openapi = ["indexmap", "log", "openapiv3"] openapi = ["indexmap", "log", "openapiv3"]

View file

@ -1,5 +1,10 @@
#[cfg(feature = "chrono")]
use chrono::{
Date, DateTime, FixedOffset, Local, NaiveDate, NaiveDateTime, Utc
};
use openapiv3::{ 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())) 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); str_types!(String, &str);
@ -91,3 +110,8 @@ impl<T : OpenapiType> OpenapiType for Vec<T>
})) }))
} }
} }
#[cfg(feature = "chrono")]
str_types!(format = Date, Date<FixedOffset>, Date<Local>, Date<Utc>, NaiveDate);
#[cfg(feature = "chrono")]
str_types!(format = DateTime, DateTime<FixedOffset>, DateTime<Local>, DateTime<Utc>, NaiveDateTime);