Skip to content

Commit

Permalink
Make chrono and time optional
Browse files Browse the repository at this point in the history
  • Loading branch information
fussybeaver committed Jun 11, 2022
1 parent 4d06b1e commit 973495a
Show file tree
Hide file tree
Showing 17 changed files with 170 additions and 66 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
- checkout
- setup_remote_docker
- run: docker build -t bollard .
- run: docker run -ti --rm bollard cargo test --target x86_64-unknown-linux-gnu --doc
- run: docker run -ti --rm bollard cargo test --features time --target x86_64-unknown-linux-gnu --doc
test_clippy:
docker:
- image: docker:20.10.16
Expand Down
7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ test_macos = []
# Enable rustls / ssl
ssl = ["dirs-next", "hyper-rustls", "rustls", "rustls-native-certs", "rustls-pemfile", "webpki", "webpki-roots"]
ct_logs = ["ssl", "ct-logs"]
chrono = ["dep:chrono", "bollard-stubs/chrono"]
time = ["dep:time", "bollard-stubs/time"]

[dependencies]
base64 = "0.13"
bollard-stubs = { version = "=1.42.0-rc.2", path = "codegen/target/generated-sources" }
bollard-stubs = { version = "=1.42.0-rc.2", path = "codegen/target/generated-sources", default-features = false }
bytes = "1"
chrono = { version = "0.4", features = ["serde"], optional = true }
ct-logs = { version = "0.9.0", optional = true }
dirs-next = { version = "2.0", optional = true }
futures-core = "0.3"
Expand All @@ -45,7 +48,7 @@ serde_json = "1.0"
serde_urlencoded = "0.7"
tokio = { version = "1.7", features = ["time", "net", "io-util"] }
thiserror = "1.0"
time = { version = "0.3", features = ["formatting", "parsing"] }
time = { version = "0.3", features = ["formatting", "parsing"], optional = true }
tokio-util = { version = "0.7", features = ["codec"] }
url = "2.2"
webpki-roots = { version = "0.22", optional = true }
Expand Down
6 changes: 3 additions & 3 deletions codegen/src/main/java/bollard/BollardCodegen.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class BollardCodegen extends RustServerCodegen {

public BollardCodegen() {
super();
typeMapping.put("DateTime", "OffsetDateTime");
typeMapping.put("DateTime", "BollardDate");
}

// Declare custom additions to inline enums that are behaving differently
Expand Down Expand Up @@ -124,10 +124,10 @@ public Map<String, Object> postProcessAllModels(Map<String, Object> objs) {
} else if (prop.name.equals("_type")) {
prop.name = "typ";
}
if (prop.dataFormat != null && (prop.dataFormat.equals("dateTime") || prop.datatype.equals("OffsetDateTime"))) {
if (prop.dataFormat != null && (prop.dataFormat.equals("dateTime") || prop.datatype.equals("BollardDate"))) {
// set DateTime format on properties where appropriate
prop.vendorExtensions.put("x-rustgen-is-datetime", true);
prop.datatype = "OffsetDateTime";
prop.datatype = "BollardDate";
}
if (prop.isEnum) {
if (enumToString.contains(model.classname)) {
Expand Down
3 changes: 2 additions & 1 deletion codegen/src/main/resources/bollard/Cargo.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ license = "Apache-2.0"
edition = "2018"

[dependencies]
chrono = { version = "0.4", features = ["serde"], optional = true }
serde = { version = "1.0", features = ["derive"] }
time = { version = "0.3", features = ["formatting", "parsing"] }
time = { version = "0.3", features = ["formatting", "parsing"], optional = true }

serde_with = "1.4"
32 changes: 27 additions & 5 deletions codegen/src/main/resources/bollard/models.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use std::collections::HashMap;
use std::default::Default;
use std::hash::Hash;

use time::OffsetDateTime;

fn deserialize_nonoptional_vec<'de, D: Deserializer<'de>, T: DeserializeOwned>(
d: D,
) -> Result<Vec<T>, D::Error> {
Expand All @@ -23,28 +21,52 @@ fn deserialize_nonoptional_map<'de, D: Deserializer<'de>, T: DeserializeOwned>(
serde::Deserialize::deserialize(d).map(|x: Option<_>| x.unwrap_or(HashMap::new()))
}

#[cfg(feature = "time")]
pub type BollardDate = time::OffsetDateTime;
#[cfg(feature = "chrono")]
pub type BollardDate = chrono::DateTime<chrono::Utc>;
#[cfg(not(any(feature = "chrono", feature = "time")))]
pub type BollardDate = String;

#[cfg(feature = "time")]
fn deserialize_timestamp<'de, D: Deserializer<'de>>(
d: D
) -> Result<Option<OffsetDateTime>, D::Error> {
) -> Result<Option<BollardDate>, D::Error> {
let opt: Option<String> = serde::Deserialize::deserialize(d)?;
if let Some(s) = opt {
Ok(Some(
OffsetDateTime::parse(&s, &time::format_description::well_known::Rfc3339)
time::OffsetDateTime::parse(&s, &time::format_description::well_known::Rfc3339)
.map_err(|e| serde::de::Error::custom(format!("{:?}", e)))?,
))
} else {
Ok(None)
}
}

fn serialize_timestamp<S: Serializer>(date: &Option<OffsetDateTime>, s: S) -> Result<S::Ok, S::Error> {
#[cfg(not(feature = "time"))]
fn deserialize_timestamp<'de, D: Deserializer<'de>>(
d: D
) -> Result<Option<BollardDate>, D::Error> {
serde::Deserialize::deserialize(d)
}

#[cfg(feature = "time")]
fn serialize_timestamp<S: Serializer>(date: &Option<BollardDate>, s: S) -> Result<S::Ok, S::Error> {
match date {
Some(inner) => Ok(s.serialize_str(&inner.format(&time::format_description::well_known::Rfc3339)
.map_err(|e| serde::ser::Error::custom(format!("{:?}", e)))?)?),
None => Ok(s.serialize_str("")?)
}
}

#[cfg(not(feature = "time"))]
fn serialize_timestamp<S: Serializer>(date: &Option<BollardDate>, s: S) -> Result<S::Ok, S::Error> {
match date {
Some(inner) => s.serialize_some(inner),
None => s.serialize_none()
}
}

{{#models}}{{#model}}
{{#description}}/// {{{description}}}
{{/description}}{{#isEnum}}/// Enumeration of values.
Expand Down
3 changes: 2 additions & 1 deletion codegen/target/generated-sources/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ license = "Apache-2.0"
edition = "2018"

[dependencies]
chrono = { version = "0.4", features = ["serde"], optional = true }
serde = { version = "1.0", features = ["derive"] }
time = { version = "0.3", features = ["formatting", "parsing"] }
time = { version = "0.3", features = ["formatting", "parsing"], optional = true }

serde_with = "1.4"
2 changes: 1 addition & 1 deletion codegen/target/generated-sources/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ To see how to make this your own, look here:

- API version: 1.42.0-rc.2
- Code generation suffix: 1.42.0-rc.2
- Build date: 2022-06-11T11:56:09.243+01:00
- Build date: 2022-06-11T13:13:57.645+01:00

This autogenerated project defines an API crate `bollard-stubs` which contains:
* Data types representing the underlying data model.
Expand Down
Loading

0 comments on commit 973495a

Please sign in to comment.