From 3e2b453a60ed9e7e98da2c6c0f3c7b484b0f9721 Mon Sep 17 00:00:00 2001 From: David Bernard Date: Sun, 14 Jan 2024 19:32:08 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20use=20more=20precise=20typ?= =?UTF-8?q?e=20for=20uri=20=20&=20datetime=20than=20String?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - uri => fluent_uri::Uri - uri-referece => fluent_uri::Uri - datetime => time::OffsetDateTime --- cdevents-sdk/Cargo.toml | 6 +- cdevents-sdk/src/cdevent.rs | 98 ++++++++++ cdevents-sdk/src/context.rs | 13 ++ .../src/generated/artifact_packaged.rs | 12 +- .../src/generated/artifact_published.rs | 2 +- cdevents-sdk/src/generated/artifact_signed.rs | 4 +- cdevents-sdk/src/generated/branch_created.rs | 12 +- cdevents-sdk/src/generated/branch_deleted.rs | 12 +- cdevents-sdk/src/generated/build_finished.rs | 4 +- cdevents-sdk/src/generated/build_queued.rs | 2 +- cdevents-sdk/src/generated/build_started.rs | 2 +- .../src/generated/change_abandoned.rs | 12 +- cdevents-sdk/src/generated/change_created.rs | 12 +- cdevents-sdk/src/generated/change_merged.rs | 12 +- cdevents-sdk/src/generated/change_reviewed.rs | 12 +- cdevents-sdk/src/generated/change_updated.rs | 12 +- .../src/generated/environment_created.rs | 6 +- .../src/generated/environment_deleted.rs | 4 +- .../src/generated/environment_modified.rs | 6 +- .../src/generated/incident_detected.rs | 26 +-- .../src/generated/incident_reported.rs | 30 +-- .../src/generated/incident_resolved.rs | 26 +-- cdevents-sdk/src/generated/mod.rs | 6 +- .../src/generated/pipelinerun_finished.rs | 10 +- .../src/generated/pipelinerun_queued.rs | 6 +- .../src/generated/pipelinerun_started.rs | 6 +- .../src/generated/repository_created.rs | 10 +- .../src/generated/repository_deleted.rs | 10 +- .../src/generated/repository_modified.rs | 10 +- .../src/generated/service_deployed.rs | 14 +- .../src/generated/service_published.rs | 12 +- cdevents-sdk/src/generated/service_removed.rs | 12 +- .../src/generated/service_rolledback.rs | 14 +- .../src/generated/service_upgraded.rs | 14 +- .../src/generated/taskrun_finished.rs | 20 +- cdevents-sdk/src/generated/taskrun_started.rs | 16 +- .../src/generated/testcaserun_finished.rs | 42 ++--- .../src/generated/testcaserun_queued.rs | 46 ++--- .../src/generated/testcaserun_started.rs | 46 ++--- .../src/generated/testoutput_published.rs | 18 +- .../src/generated/testsuiterun_finished.rs | 32 ++-- .../src/generated/testsuiterun_queued.rs | 36 ++-- .../src/generated/testsuiterun_started.rs | 36 ++-- cdevents-sdk/src/lib.rs | 171 +----------------- cdevents-sdk/src/serde.rs | 77 ++++++++ cdevents-sdk/src/subject.rs | 43 +++++ cdevents-sdk/tests/specs.rs | 29 +++ generator/src/main.rs | 64 +++++-- generator/templates/mod.hbs | 2 +- generator/templates/variant.hbs | 11 +- 50 files changed, 642 insertions(+), 496 deletions(-) create mode 100644 cdevents-sdk/src/cdevent.rs create mode 100644 cdevents-sdk/src/context.rs create mode 100644 cdevents-sdk/src/serde.rs create mode 100644 cdevents-sdk/src/subject.rs create mode 100644 cdevents-sdk/tests/specs.rs diff --git a/cdevents-sdk/Cargo.toml b/cdevents-sdk/Cargo.toml index 117cc99..3df0ae8 100644 --- a/cdevents-sdk/Cargo.toml +++ b/cdevents-sdk/Cargo.toml @@ -11,9 +11,11 @@ publish = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -http = "1" -http-serde = "2" +# http = "1" # doesn't support uri-reference +# http-serde = "2" +fluent-uri = "0.1" serde = { version = "1.0", features = ["derive"] } +serde_with = "3.4" serde_json = "1.0" time = { version = "0.3", features = ["serde-human-readable"] } diff --git a/cdevents-sdk/src/cdevent.rs b/cdevents-sdk/src/cdevent.rs new file mode 100644 index 0000000..84ed810 --- /dev/null +++ b/cdevents-sdk/src/cdevent.rs @@ -0,0 +1,98 @@ +use crate::{Context, Subject}; +use serde::{ + de::{self, Deserializer, MapAccess, Visitor}, + Deserialize, Serialize, +}; +use std::fmt; + +#[derive(Debug, Clone, Serialize)] +pub struct CDEvent { + pub context: Context, + pub subject: Subject, + #[serde(rename = "customData", skip_serializing_if = "Option::is_none")] + pub custom_data: Option, + #[serde( + rename = "customDataContentType", + skip_serializing_if = "Option::is_none" + )] + pub custom_data_content_type: Option, +} + +impl<'de> Deserialize<'de> for CDEvent { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + #[derive(Deserialize)] + #[serde(field_identifier, rename_all = "camelCase")] + enum Field { + Context, + Subject, + CustomData, + CustomDataContentType, + } + + struct CDEventVisitor; + + impl<'de> Visitor<'de> for CDEventVisitor { + type Value = CDEvent; + + fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { + formatter.write_str("struct CDEvent") + } + + fn visit_map(self, mut map: V) -> Result + where + V: MapAccess<'de>, + { + let mut context: Option = None; + let mut subject_json: Option = None; + let mut custom_data = None; + let mut custom_data_content_type = None; + while let Some(key) = map.next_key()? { + match key { + Field::Context => { + if context.is_some() { + return Err(de::Error::duplicate_field("context")); + } + context = Some(map.next_value()?); + } + Field::Subject => { + if subject_json.is_some() { + return Err(de::Error::duplicate_field("subject")); + } + subject_json = Some(map.next_value()?); + } + Field::CustomData => { + if custom_data.is_some() { + return Err(de::Error::duplicate_field("customData")); + } + custom_data = Some(map.next_value()?); + } + Field::CustomDataContentType => { + if custom_data_content_type.is_some() { + return Err(de::Error::duplicate_field("customDataContentType")); + } + custom_data_content_type = Some(map.next_value()?); + } + } + } + let context = context.ok_or_else(|| de::Error::missing_field("context"))?; + let subject_json = + subject_json.ok_or_else(|| de::Error::missing_field("subject"))?; + let subject = + Subject::from_json(&context.r#type, subject_json).map_err(de::Error::custom)?; + + Ok(CDEvent { + context, + subject, + custom_data, + custom_data_content_type, + }) + } + } + + const FIELDS: &'static [&'static str] = &["context", "subject"]; + deserializer.deserialize_struct("CDEvent", FIELDS, CDEventVisitor) + } +} diff --git a/cdevents-sdk/src/context.rs b/cdevents-sdk/src/context.rs new file mode 100644 index 0000000..e54d022 --- /dev/null +++ b/cdevents-sdk/src/context.rs @@ -0,0 +1,13 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Context { + pub version: String, + pub id: String, + #[serde(with = "crate::serde::uri_reference")] + pub source: fluent_uri::Uri, + #[serde(rename = "type")] + pub r#type: String, + #[serde(with = "crate::serde::datetime")] + pub timestamp: time::OffsetDateTime, +} diff --git a/cdevents-sdk/src/generated/artifact_packaged.rs b/cdevents-sdk/src/generated/artifact_packaged.rs index 460a266..481a3e9 100644 --- a/cdevents-sdk/src/generated/artifact_packaged.rs +++ b/cdevents-sdk/src/generated/artifact_packaged.rs @@ -1,17 +1,17 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "change")] + #[serde(rename = "change",)] pub change: Change, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Change { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/artifact_published.rs b/cdevents-sdk/src/generated/artifact_published.rs index 4367d0a..afc7ec5 100644 --- a/cdevents-sdk/src/generated/artifact_published.rs +++ b/cdevents-sdk/src/generated/artifact_published.rs @@ -1,7 +1,7 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { } diff --git a/cdevents-sdk/src/generated/artifact_signed.rs b/cdevents-sdk/src/generated/artifact_signed.rs index 0876c99..a57374f 100644 --- a/cdevents-sdk/src/generated/artifact_signed.rs +++ b/cdevents-sdk/src/generated/artifact_signed.rs @@ -1,9 +1,9 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "signature")] + #[serde(rename = "signature",)] pub signature: String, } diff --git a/cdevents-sdk/src/generated/branch_created.rs b/cdevents-sdk/src/generated/branch_created.rs index 6a21b4d..203a406 100644 --- a/cdevents-sdk/src/generated/branch_created.rs +++ b/cdevents-sdk/src/generated/branch_created.rs @@ -1,17 +1,17 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "repository", skip_serializing_if = "Option::is_none")] + #[serde(rename = "repository", default, skip_serializing_if = "Option::is_none",)] pub repository: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Repository { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/branch_deleted.rs b/cdevents-sdk/src/generated/branch_deleted.rs index 6a21b4d..203a406 100644 --- a/cdevents-sdk/src/generated/branch_deleted.rs +++ b/cdevents-sdk/src/generated/branch_deleted.rs @@ -1,17 +1,17 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "repository", skip_serializing_if = "Option::is_none")] + #[serde(rename = "repository", default, skip_serializing_if = "Option::is_none",)] pub repository: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Repository { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/build_finished.rs b/cdevents-sdk/src/generated/build_finished.rs index 6a27785..dd28b25 100644 --- a/cdevents-sdk/src/generated/build_finished.rs +++ b/cdevents-sdk/src/generated/build_finished.rs @@ -1,9 +1,9 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "artifactId", skip_serializing_if = "Option::is_none")] + #[serde(rename = "artifactId", default, skip_serializing_if = "Option::is_none",)] pub artifact_id: Option, } diff --git a/cdevents-sdk/src/generated/build_queued.rs b/cdevents-sdk/src/generated/build_queued.rs index 4367d0a..afc7ec5 100644 --- a/cdevents-sdk/src/generated/build_queued.rs +++ b/cdevents-sdk/src/generated/build_queued.rs @@ -1,7 +1,7 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { } diff --git a/cdevents-sdk/src/generated/build_started.rs b/cdevents-sdk/src/generated/build_started.rs index 4367d0a..afc7ec5 100644 --- a/cdevents-sdk/src/generated/build_started.rs +++ b/cdevents-sdk/src/generated/build_started.rs @@ -1,7 +1,7 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { } diff --git a/cdevents-sdk/src/generated/change_abandoned.rs b/cdevents-sdk/src/generated/change_abandoned.rs index 6a21b4d..203a406 100644 --- a/cdevents-sdk/src/generated/change_abandoned.rs +++ b/cdevents-sdk/src/generated/change_abandoned.rs @@ -1,17 +1,17 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "repository", skip_serializing_if = "Option::is_none")] + #[serde(rename = "repository", default, skip_serializing_if = "Option::is_none",)] pub repository: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Repository { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/change_created.rs b/cdevents-sdk/src/generated/change_created.rs index 6a21b4d..203a406 100644 --- a/cdevents-sdk/src/generated/change_created.rs +++ b/cdevents-sdk/src/generated/change_created.rs @@ -1,17 +1,17 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "repository", skip_serializing_if = "Option::is_none")] + #[serde(rename = "repository", default, skip_serializing_if = "Option::is_none",)] pub repository: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Repository { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/change_merged.rs b/cdevents-sdk/src/generated/change_merged.rs index 6a21b4d..203a406 100644 --- a/cdevents-sdk/src/generated/change_merged.rs +++ b/cdevents-sdk/src/generated/change_merged.rs @@ -1,17 +1,17 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "repository", skip_serializing_if = "Option::is_none")] + #[serde(rename = "repository", default, skip_serializing_if = "Option::is_none",)] pub repository: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Repository { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/change_reviewed.rs b/cdevents-sdk/src/generated/change_reviewed.rs index 6a21b4d..203a406 100644 --- a/cdevents-sdk/src/generated/change_reviewed.rs +++ b/cdevents-sdk/src/generated/change_reviewed.rs @@ -1,17 +1,17 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "repository", skip_serializing_if = "Option::is_none")] + #[serde(rename = "repository", default, skip_serializing_if = "Option::is_none",)] pub repository: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Repository { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/change_updated.rs b/cdevents-sdk/src/generated/change_updated.rs index 6a21b4d..203a406 100644 --- a/cdevents-sdk/src/generated/change_updated.rs +++ b/cdevents-sdk/src/generated/change_updated.rs @@ -1,17 +1,17 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "repository", skip_serializing_if = "Option::is_none")] + #[serde(rename = "repository", default, skip_serializing_if = "Option::is_none",)] pub repository: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Repository { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/environment_created.rs b/cdevents-sdk/src/generated/environment_created.rs index 5efe460..9e7e3bd 100644 --- a/cdevents-sdk/src/generated/environment_created.rs +++ b/cdevents-sdk/src/generated/environment_created.rs @@ -1,11 +1,11 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, - #[serde(rename = "url", skip_serializing_if = "Option::is_none")] + #[serde(rename = "url", default, skip_serializing_if = "Option::is_none",)] pub url: Option, } diff --git a/cdevents-sdk/src/generated/environment_deleted.rs b/cdevents-sdk/src/generated/environment_deleted.rs index 1e25b81..0ed9572 100644 --- a/cdevents-sdk/src/generated/environment_deleted.rs +++ b/cdevents-sdk/src/generated/environment_deleted.rs @@ -1,9 +1,9 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, } diff --git a/cdevents-sdk/src/generated/environment_modified.rs b/cdevents-sdk/src/generated/environment_modified.rs index 5efe460..9e7e3bd 100644 --- a/cdevents-sdk/src/generated/environment_modified.rs +++ b/cdevents-sdk/src/generated/environment_modified.rs @@ -1,11 +1,11 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, - #[serde(rename = "url", skip_serializing_if = "Option::is_none")] + #[serde(rename = "url", default, skip_serializing_if = "Option::is_none",)] pub url: Option, } diff --git a/cdevents-sdk/src/generated/incident_detected.rs b/cdevents-sdk/src/generated/incident_detected.rs index a444e59..21c52e9 100644 --- a/cdevents-sdk/src/generated/incident_detected.rs +++ b/cdevents-sdk/src/generated/incident_detected.rs @@ -1,31 +1,31 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "artifactId", skip_serializing_if = "Option::is_none")] + #[serde(rename = "artifactId", default, skip_serializing_if = "Option::is_none",)] pub artifact_id: Option, - #[serde(rename = "description", skip_serializing_if = "Option::is_none")] + #[serde(rename = "description", default, skip_serializing_if = "Option::is_none",)] pub description: Option, - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, - #[serde(rename = "service", skip_serializing_if = "Option::is_none")] + #[serde(rename = "service", default, skip_serializing_if = "Option::is_none",)] pub service: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Service { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/incident_reported.rs b/cdevents-sdk/src/generated/incident_reported.rs index 43ed85d..19789d8 100644 --- a/cdevents-sdk/src/generated/incident_reported.rs +++ b/cdevents-sdk/src/generated/incident_reported.rs @@ -1,33 +1,33 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "artifactId", skip_serializing_if = "Option::is_none")] + #[serde(rename = "artifactId", default, skip_serializing_if = "Option::is_none",)] pub artifact_id: Option, - #[serde(rename = "description", skip_serializing_if = "Option::is_none")] + #[serde(rename = "description", default, skip_serializing_if = "Option::is_none",)] pub description: Option, - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, - #[serde(rename = "service", skip_serializing_if = "Option::is_none")] + #[serde(rename = "service", default, skip_serializing_if = "Option::is_none",)] pub service: Option, - #[serde(rename = "ticketURI")] - pub ticket_uri: String, + #[serde(rename = "ticketURI", with = "crate::serde::uri",)] + pub ticket_uri: fluent_uri::Uri, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Service { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/incident_resolved.rs b/cdevents-sdk/src/generated/incident_resolved.rs index a444e59..21c52e9 100644 --- a/cdevents-sdk/src/generated/incident_resolved.rs +++ b/cdevents-sdk/src/generated/incident_resolved.rs @@ -1,31 +1,31 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "artifactId", skip_serializing_if = "Option::is_none")] + #[serde(rename = "artifactId", default, skip_serializing_if = "Option::is_none",)] pub artifact_id: Option, - #[serde(rename = "description", skip_serializing_if = "Option::is_none")] + #[serde(rename = "description", default, skip_serializing_if = "Option::is_none",)] pub description: Option, - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, - #[serde(rename = "service", skip_serializing_if = "Option::is_none")] + #[serde(rename = "service", default, skip_serializing_if = "Option::is_none",)] pub service: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Service { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/mod.rs b/cdevents-sdk/src/generated/mod.rs index 879534f..c49590a 100644 --- a/cdevents-sdk/src/generated/mod.rs +++ b/cdevents-sdk/src/generated/mod.rs @@ -42,7 +42,7 @@ pub mod testsuiterun_finished; pub mod testsuiterun_queued; pub mod testsuiterun_started; -use serde::{Deserialize, Serialize}; +use serde::{Serialize, Deserialize}; pub const ARTIFACT_PACKAGED: &str = "dev.cdevents.artifact.packaged.0.1.1"; pub const ARTIFACT_PUBLISHED: &str = "dev.cdevents.artifact.published.0.1.1"; @@ -84,7 +84,7 @@ pub const TESTSUITERUN_FINISHED: &str = "dev.cdevents.testsuiterun.finished.0.1. pub const TESTSUITERUN_QUEUED: &str = "dev.cdevents.testsuiterun.queued.0.1.0"; pub const TESTSUITERUN_STARTED: &str = "dev.cdevents.testsuiterun.started.0.1.0"; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug,Clone,Serialize,Deserialize)] #[serde(untagged)] // TODO how to use content of context.type as discriminator ? pub enum Content { ArtifactPackaged(artifact_packaged::Content), @@ -129,7 +129,7 @@ pub enum Content { } impl Content { - pub fn from_json(ty: &str, json: serde_json::Value) -> Result { + pub fn from_json(ty: &str, json: serde_json::Value) -> Result{ match ty { ARTIFACT_PACKAGED => { let variant: artifact_packaged::Content = serde_json::from_value(json)?; diff --git a/cdevents-sdk/src/generated/pipelinerun_finished.rs b/cdevents-sdk/src/generated/pipelinerun_finished.rs index 56c979f..91302b0 100644 --- a/cdevents-sdk/src/generated/pipelinerun_finished.rs +++ b/cdevents-sdk/src/generated/pipelinerun_finished.rs @@ -1,15 +1,15 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "errors", skip_serializing_if = "Option::is_none")] + #[serde(rename = "errors", default, skip_serializing_if = "Option::is_none",)] pub errors: Option, - #[serde(rename = "outcome", skip_serializing_if = "Option::is_none")] + #[serde(rename = "outcome", default, skip_serializing_if = "Option::is_none",)] pub outcome: Option, - #[serde(rename = "pipelineName", skip_serializing_if = "Option::is_none")] + #[serde(rename = "pipelineName", default, skip_serializing_if = "Option::is_none",)] pub pipeline_name: Option, - #[serde(rename = "url", skip_serializing_if = "Option::is_none")] + #[serde(rename = "url", default, skip_serializing_if = "Option::is_none",)] pub url: Option, } diff --git a/cdevents-sdk/src/generated/pipelinerun_queued.rs b/cdevents-sdk/src/generated/pipelinerun_queued.rs index 1885da8..3a79f47 100644 --- a/cdevents-sdk/src/generated/pipelinerun_queued.rs +++ b/cdevents-sdk/src/generated/pipelinerun_queued.rs @@ -1,11 +1,11 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "pipelineName", skip_serializing_if = "Option::is_none")] + #[serde(rename = "pipelineName", default, skip_serializing_if = "Option::is_none",)] pub pipeline_name: Option, - #[serde(rename = "url", skip_serializing_if = "Option::is_none")] + #[serde(rename = "url", default, skip_serializing_if = "Option::is_none",)] pub url: Option, } diff --git a/cdevents-sdk/src/generated/pipelinerun_started.rs b/cdevents-sdk/src/generated/pipelinerun_started.rs index d832d1b..8c27f88 100644 --- a/cdevents-sdk/src/generated/pipelinerun_started.rs +++ b/cdevents-sdk/src/generated/pipelinerun_started.rs @@ -1,11 +1,11 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "pipelineName")] + #[serde(rename = "pipelineName",)] pub pipeline_name: String, - #[serde(rename = "url")] + #[serde(rename = "url",)] pub url: String, } diff --git a/cdevents-sdk/src/generated/repository_created.rs b/cdevents-sdk/src/generated/repository_created.rs index 9ca92ef..168050b 100644 --- a/cdevents-sdk/src/generated/repository_created.rs +++ b/cdevents-sdk/src/generated/repository_created.rs @@ -1,15 +1,15 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "name")] + #[serde(rename = "name",)] pub name: String, - #[serde(rename = "owner", skip_serializing_if = "Option::is_none")] + #[serde(rename = "owner", default, skip_serializing_if = "Option::is_none",)] pub owner: Option, - #[serde(rename = "url")] + #[serde(rename = "url",)] pub url: String, - #[serde(rename = "viewUrl", skip_serializing_if = "Option::is_none")] + #[serde(rename = "viewUrl", default, skip_serializing_if = "Option::is_none",)] pub view_url: Option, } diff --git a/cdevents-sdk/src/generated/repository_deleted.rs b/cdevents-sdk/src/generated/repository_deleted.rs index faac696..16bd662 100644 --- a/cdevents-sdk/src/generated/repository_deleted.rs +++ b/cdevents-sdk/src/generated/repository_deleted.rs @@ -1,15 +1,15 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, - #[serde(rename = "owner", skip_serializing_if = "Option::is_none")] + #[serde(rename = "owner", default, skip_serializing_if = "Option::is_none",)] pub owner: Option, - #[serde(rename = "url", skip_serializing_if = "Option::is_none")] + #[serde(rename = "url", default, skip_serializing_if = "Option::is_none",)] pub url: Option, - #[serde(rename = "viewUrl", skip_serializing_if = "Option::is_none")] + #[serde(rename = "viewUrl", default, skip_serializing_if = "Option::is_none",)] pub view_url: Option, } diff --git a/cdevents-sdk/src/generated/repository_modified.rs b/cdevents-sdk/src/generated/repository_modified.rs index faac696..16bd662 100644 --- a/cdevents-sdk/src/generated/repository_modified.rs +++ b/cdevents-sdk/src/generated/repository_modified.rs @@ -1,15 +1,15 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, - #[serde(rename = "owner", skip_serializing_if = "Option::is_none")] + #[serde(rename = "owner", default, skip_serializing_if = "Option::is_none",)] pub owner: Option, - #[serde(rename = "url", skip_serializing_if = "Option::is_none")] + #[serde(rename = "url", default, skip_serializing_if = "Option::is_none",)] pub url: Option, - #[serde(rename = "viewUrl", skip_serializing_if = "Option::is_none")] + #[serde(rename = "viewUrl", default, skip_serializing_if = "Option::is_none",)] pub view_url: Option, } diff --git a/cdevents-sdk/src/generated/service_deployed.rs b/cdevents-sdk/src/generated/service_deployed.rs index 11ac67f..40f0f34 100644 --- a/cdevents-sdk/src/generated/service_deployed.rs +++ b/cdevents-sdk/src/generated/service_deployed.rs @@ -1,19 +1,19 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "artifactId")] + #[serde(rename = "artifactId",)] pub artifact_id: String, - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/service_published.rs b/cdevents-sdk/src/generated/service_published.rs index 64a31e3..b4571ed 100644 --- a/cdevents-sdk/src/generated/service_published.rs +++ b/cdevents-sdk/src/generated/service_published.rs @@ -1,17 +1,17 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "environment", skip_serializing_if = "Option::is_none")] + #[serde(rename = "environment", default, skip_serializing_if = "Option::is_none",)] pub environment: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/service_removed.rs b/cdevents-sdk/src/generated/service_removed.rs index 64a31e3..b4571ed 100644 --- a/cdevents-sdk/src/generated/service_removed.rs +++ b/cdevents-sdk/src/generated/service_removed.rs @@ -1,17 +1,17 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "environment", skip_serializing_if = "Option::is_none")] + #[serde(rename = "environment", default, skip_serializing_if = "Option::is_none",)] pub environment: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/service_rolledback.rs b/cdevents-sdk/src/generated/service_rolledback.rs index 11ac67f..40f0f34 100644 --- a/cdevents-sdk/src/generated/service_rolledback.rs +++ b/cdevents-sdk/src/generated/service_rolledback.rs @@ -1,19 +1,19 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "artifactId")] + #[serde(rename = "artifactId",)] pub artifact_id: String, - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/service_upgraded.rs b/cdevents-sdk/src/generated/service_upgraded.rs index 11ac67f..40f0f34 100644 --- a/cdevents-sdk/src/generated/service_upgraded.rs +++ b/cdevents-sdk/src/generated/service_upgraded.rs @@ -1,19 +1,19 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "artifactId")] + #[serde(rename = "artifactId",)] pub artifact_id: String, - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/taskrun_finished.rs b/cdevents-sdk/src/generated/taskrun_finished.rs index 247873c..f1bdb00 100644 --- a/cdevents-sdk/src/generated/taskrun_finished.rs +++ b/cdevents-sdk/src/generated/taskrun_finished.rs @@ -1,25 +1,25 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "errors", skip_serializing_if = "Option::is_none")] + #[serde(rename = "errors", default, skip_serializing_if = "Option::is_none",)] pub errors: Option, - #[serde(rename = "outcome", skip_serializing_if = "Option::is_none")] + #[serde(rename = "outcome", default, skip_serializing_if = "Option::is_none",)] pub outcome: Option, - #[serde(rename = "pipelineRun", skip_serializing_if = "Option::is_none")] + #[serde(rename = "pipelineRun", default, skip_serializing_if = "Option::is_none",)] pub pipeline_run: Option, - #[serde(rename = "taskName", skip_serializing_if = "Option::is_none")] + #[serde(rename = "taskName", default, skip_serializing_if = "Option::is_none",)] pub task_name: Option, - #[serde(rename = "url", skip_serializing_if = "Option::is_none")] + #[serde(rename = "url", default, skip_serializing_if = "Option::is_none",)] pub url: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct PipelineRun { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/taskrun_started.rs b/cdevents-sdk/src/generated/taskrun_started.rs index 92089fd..6c703a6 100644 --- a/cdevents-sdk/src/generated/taskrun_started.rs +++ b/cdevents-sdk/src/generated/taskrun_started.rs @@ -1,21 +1,21 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "pipelineRun", skip_serializing_if = "Option::is_none")] + #[serde(rename = "pipelineRun", default, skip_serializing_if = "Option::is_none",)] pub pipeline_run: Option, - #[serde(rename = "taskName", skip_serializing_if = "Option::is_none")] + #[serde(rename = "taskName", default, skip_serializing_if = "Option::is_none",)] pub task_name: Option, - #[serde(rename = "url", skip_serializing_if = "Option::is_none")] + #[serde(rename = "url", default, skip_serializing_if = "Option::is_none",)] pub url: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct PipelineRun { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/testcaserun_finished.rs b/cdevents-sdk/src/generated/testcaserun_finished.rs index c42bdbc..0abaaff 100644 --- a/cdevents-sdk/src/generated/testcaserun_finished.rs +++ b/cdevents-sdk/src/generated/testcaserun_finished.rs @@ -1,49 +1,49 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, - #[serde(rename = "outcome")] + #[serde(rename = "outcome",)] pub outcome: String, - #[serde(rename = "reason", skip_serializing_if = "Option::is_none")] + #[serde(rename = "reason", default, skip_serializing_if = "Option::is_none",)] pub reason: Option, - #[serde(rename = "severity", skip_serializing_if = "Option::is_none")] + #[serde(rename = "severity", default, skip_serializing_if = "Option::is_none",)] pub severity: Option, - #[serde(rename = "testCase", skip_serializing_if = "Option::is_none")] + #[serde(rename = "testCase", default, skip_serializing_if = "Option::is_none",)] pub test_case: Option, - #[serde(rename = "testSuiteRun", skip_serializing_if = "Option::is_none")] + #[serde(rename = "testSuiteRun", default, skip_serializing_if = "Option::is_none",)] pub test_suite_run: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TestSuiteRun { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)] pub source: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TestCase { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, - #[serde(rename = "type", skip_serializing_if = "Option::is_none")] + #[serde(rename = "type", default, skip_serializing_if = "Option::is_none",)] pub r#type: Option, - #[serde(rename = "uri", skip_serializing_if = "Option::is_none")] - pub uri: Option, - #[serde(rename = "version", skip_serializing_if = "Option::is_none")] + #[serde(rename = "uri", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub uri: Option>, + #[serde(rename = "version", default, skip_serializing_if = "Option::is_none",)] pub version: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/testcaserun_queued.rs b/cdevents-sdk/src/generated/testcaserun_queued.rs index 00c2194..dcdd6a9 100644 --- a/cdevents-sdk/src/generated/testcaserun_queued.rs +++ b/cdevents-sdk/src/generated/testcaserun_queued.rs @@ -1,53 +1,53 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, - #[serde(rename = "testCase", skip_serializing_if = "Option::is_none")] + #[serde(rename = "testCase", default, skip_serializing_if = "Option::is_none",)] pub test_case: Option, - #[serde(rename = "testSuiteRun", skip_serializing_if = "Option::is_none")] + #[serde(rename = "testSuiteRun", default, skip_serializing_if = "Option::is_none",)] pub test_suite_run: Option, - #[serde(rename = "trigger", skip_serializing_if = "Option::is_none")] + #[serde(rename = "trigger", default, skip_serializing_if = "Option::is_none",)] pub trigger: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Trigger { - #[serde(rename = "type", skip_serializing_if = "Option::is_none")] + #[serde(rename = "type", default, skip_serializing_if = "Option::is_none",)] pub r#type: Option, - #[serde(rename = "uri", skip_serializing_if = "Option::is_none")] - pub uri: Option, + #[serde(rename = "uri", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub uri: Option>, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TestSuiteRun { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)] pub source: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TestCase { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, - #[serde(rename = "type", skip_serializing_if = "Option::is_none")] + #[serde(rename = "type", default, skip_serializing_if = "Option::is_none",)] pub r#type: Option, - #[serde(rename = "uri", skip_serializing_if = "Option::is_none")] - pub uri: Option, - #[serde(rename = "version", skip_serializing_if = "Option::is_none")] + #[serde(rename = "uri", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub uri: Option>, + #[serde(rename = "version", default, skip_serializing_if = "Option::is_none",)] pub version: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/testcaserun_started.rs b/cdevents-sdk/src/generated/testcaserun_started.rs index 00c2194..dcdd6a9 100644 --- a/cdevents-sdk/src/generated/testcaserun_started.rs +++ b/cdevents-sdk/src/generated/testcaserun_started.rs @@ -1,53 +1,53 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, - #[serde(rename = "testCase", skip_serializing_if = "Option::is_none")] + #[serde(rename = "testCase", default, skip_serializing_if = "Option::is_none",)] pub test_case: Option, - #[serde(rename = "testSuiteRun", skip_serializing_if = "Option::is_none")] + #[serde(rename = "testSuiteRun", default, skip_serializing_if = "Option::is_none",)] pub test_suite_run: Option, - #[serde(rename = "trigger", skip_serializing_if = "Option::is_none")] + #[serde(rename = "trigger", default, skip_serializing_if = "Option::is_none",)] pub trigger: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Trigger { - #[serde(rename = "type", skip_serializing_if = "Option::is_none")] + #[serde(rename = "type", default, skip_serializing_if = "Option::is_none",)] pub r#type: Option, - #[serde(rename = "uri", skip_serializing_if = "Option::is_none")] - pub uri: Option, + #[serde(rename = "uri", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub uri: Option>, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TestSuiteRun { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)] pub source: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TestCase { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, - #[serde(rename = "type", skip_serializing_if = "Option::is_none")] + #[serde(rename = "type", default, skip_serializing_if = "Option::is_none",)] pub r#type: Option, - #[serde(rename = "uri", skip_serializing_if = "Option::is_none")] - pub uri: Option, - #[serde(rename = "version", skip_serializing_if = "Option::is_none")] + #[serde(rename = "uri", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub uri: Option>, + #[serde(rename = "version", default, skip_serializing_if = "Option::is_none",)] pub version: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/testoutput_published.rs b/cdevents-sdk/src/generated/testoutput_published.rs index 56eb4e6..f12c6ec 100644 --- a/cdevents-sdk/src/generated/testoutput_published.rs +++ b/cdevents-sdk/src/generated/testoutput_published.rs @@ -1,23 +1,23 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "format")] + #[serde(rename = "format",)] pub format: String, - #[serde(rename = "outputType")] + #[serde(rename = "outputType",)] pub output_type: String, - #[serde(rename = "testCaseRun", skip_serializing_if = "Option::is_none")] + #[serde(rename = "testCaseRun", default, skip_serializing_if = "Option::is_none",)] pub test_case_run: Option, - #[serde(rename = "uri", skip_serializing_if = "Option::is_none")] - pub uri: Option, + #[serde(rename = "uri", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub uri: Option>, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TestCaseRun { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)] pub source: Option, } diff --git a/cdevents-sdk/src/generated/testsuiterun_finished.rs b/cdevents-sdk/src/generated/testsuiterun_finished.rs index 923e301..5f3f6b8 100644 --- a/cdevents-sdk/src/generated/testsuiterun_finished.rs +++ b/cdevents-sdk/src/generated/testsuiterun_finished.rs @@ -1,37 +1,37 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, - #[serde(rename = "outcome")] + #[serde(rename = "outcome",)] pub outcome: String, - #[serde(rename = "reason", skip_serializing_if = "Option::is_none")] + #[serde(rename = "reason", default, skip_serializing_if = "Option::is_none",)] pub reason: Option, - #[serde(rename = "severity", skip_serializing_if = "Option::is_none")] + #[serde(rename = "severity", default, skip_serializing_if = "Option::is_none",)] pub severity: Option, - #[serde(rename = "testSuite", skip_serializing_if = "Option::is_none")] + #[serde(rename = "testSuite", default, skip_serializing_if = "Option::is_none",)] pub test_suite: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TestSuite { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, - #[serde(rename = "uri", skip_serializing_if = "Option::is_none")] - pub uri: Option, - #[serde(rename = "version", skip_serializing_if = "Option::is_none")] + #[serde(rename = "uri", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub uri: Option>, + #[serde(rename = "version", default, skip_serializing_if = "Option::is_none",)] pub version: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/testsuiterun_queued.rs b/cdevents-sdk/src/generated/testsuiterun_queued.rs index 6a42577..9c74cb2 100644 --- a/cdevents-sdk/src/generated/testsuiterun_queued.rs +++ b/cdevents-sdk/src/generated/testsuiterun_queued.rs @@ -1,41 +1,41 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, - #[serde(rename = "testSuite", skip_serializing_if = "Option::is_none")] + #[serde(rename = "testSuite", default, skip_serializing_if = "Option::is_none",)] pub test_suite: Option, - #[serde(rename = "trigger", skip_serializing_if = "Option::is_none")] + #[serde(rename = "trigger", default, skip_serializing_if = "Option::is_none",)] pub trigger: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Trigger { - #[serde(rename = "type", skip_serializing_if = "Option::is_none")] + #[serde(rename = "type", default, skip_serializing_if = "Option::is_none",)] pub r#type: Option, - #[serde(rename = "uri", skip_serializing_if = "Option::is_none")] - pub uri: Option, + #[serde(rename = "uri", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub uri: Option>, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TestSuite { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, - #[serde(rename = "url", skip_serializing_if = "Option::is_none")] - pub url: Option, - #[serde(rename = "version", skip_serializing_if = "Option::is_none")] + #[serde(rename = "url", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub url: Option>, + #[serde(rename = "version", default, skip_serializing_if = "Option::is_none",)] pub version: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/generated/testsuiterun_started.rs b/cdevents-sdk/src/generated/testsuiterun_started.rs index 63ce7db..b028508 100644 --- a/cdevents-sdk/src/generated/testsuiterun_started.rs +++ b/cdevents-sdk/src/generated/testsuiterun_started.rs @@ -1,41 +1,41 @@ // code generated by cdevents/sdk-rust/generator (subject.hbs) use serde::{Serialize, Deserialize}; -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Content { - #[serde(rename = "environment")] + #[serde(rename = "environment",)] pub environment: Environment, - #[serde(rename = "testSuite", skip_serializing_if = "Option::is_none")] + #[serde(rename = "testSuite", default, skip_serializing_if = "Option::is_none",)] pub test_suite: Option, - #[serde(rename = "trigger", skip_serializing_if = "Option::is_none")] + #[serde(rename = "trigger", default, skip_serializing_if = "Option::is_none",)] pub trigger: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Trigger { - #[serde(rename = "type", skip_serializing_if = "Option::is_none")] + #[serde(rename = "type", default, skip_serializing_if = "Option::is_none",)] pub r#type: Option, - #[serde(rename = "uri", skip_serializing_if = "Option::is_none")] - pub uri: Option, + #[serde(rename = "uri", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub uri: Option>, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct TestSuite { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "name", skip_serializing_if = "Option::is_none")] + #[serde(rename = "name", default, skip_serializing_if = "Option::is_none",)] pub name: Option, - #[serde(rename = "uri", skip_serializing_if = "Option::is_none")] - pub uri: Option, - #[serde(rename = "version", skip_serializing_if = "Option::is_none")] + #[serde(rename = "uri", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_optional",)] + pub uri: Option>, + #[serde(rename = "version", default, skip_serializing_if = "Option::is_none",)] pub version: Option, } -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] +#[derive(Debug, Clone, Serialize, Deserialize)] pub struct Environment { - #[serde(rename = "id")] + #[serde(rename = "id",)] pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, + #[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)] + pub source: Option>, } diff --git a/cdevents-sdk/src/lib.rs b/cdevents-sdk/src/lib.rs index caca2dd..d4be46c 100644 --- a/cdevents-sdk/src/lib.rs +++ b/cdevents-sdk/src/lib.rs @@ -1,169 +1,12 @@ // TODO remove unwrap(), expect(...) // TODO reduce clone() +mod cdevent; +mod context; mod generated; +pub(crate) mod serde; +mod subject; +pub use cdevent::*; +pub use context::*; pub use generated::*; -use serde::{ - de::{self, Deserializer, MapAccess, Visitor}, - Deserialize, Serialize, -}; -use std::fmt; - -#[derive(Debug, Clone, Serialize, PartialEq, Eq)] -pub struct CDEvent { - pub context: Context, - pub subject: Subject, - #[serde(rename = "customData", skip_serializing_if = "Option::is_none")] - pub custom_data: Option, - #[serde( - rename = "customDataContentType", - skip_serializing_if = "Option::is_none" - )] - pub custom_data_content_type: Option, -} - -impl<'de> Deserialize<'de> for CDEvent { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - #[derive(Deserialize)] - #[serde(field_identifier, rename_all = "camelCase")] - enum Field { - Context, - Subject, - CustomData, - CustomDataContentType, - } - - struct CDEventVisitor; - - impl<'de> Visitor<'de> for CDEventVisitor { - type Value = CDEvent; - - fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - formatter.write_str("struct CDEvent") - } - - fn visit_map(self, mut map: V) -> Result - where - V: MapAccess<'de>, - { - let mut context: Option = None; - let mut subject_json: Option = None; - let mut custom_data = None; - let mut custom_data_content_type = None; - while let Some(key) = map.next_key()? { - match key { - Field::Context => { - if context.is_some() { - return Err(de::Error::duplicate_field("context")); - } - context = Some(map.next_value()?); - } - Field::Subject => { - if subject_json.is_some() { - return Err(de::Error::duplicate_field("subject")); - } - subject_json = Some(map.next_value()?); - } - Field::CustomData => { - if custom_data.is_some() { - return Err(de::Error::duplicate_field("customData")); - } - custom_data = Some(map.next_value()?); - } - Field::CustomDataContentType => { - if custom_data_content_type.is_some() { - return Err(de::Error::duplicate_field("customDataContentType")); - } - custom_data_content_type = Some(map.next_value()?); - } - } - } - let context = context.ok_or_else(|| de::Error::missing_field("context"))?; - let subject_json = - subject_json.ok_or_else(|| de::Error::missing_field("subject"))?; - let subject = - Subject::from_json(&context.r#type, subject_json).map_err(de::Error::custom)?; - - Ok(CDEvent { - context, - subject, - custom_data, - custom_data_content_type, - }) - } - } - - const FIELDS: &'static [&'static str] = &["context", "subject"]; - deserializer.deserialize_struct("CDEvent", FIELDS, CDEventVisitor) - } -} - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct Context { - version: String, - id: String, - #[serde(with = "http_serde::uri")] - source: http::Uri, - #[serde(rename = "type")] - r#type: String, - #[serde(with = "time::serde::rfc3339")] - timestamp: time::OffsetDateTime, -} - -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct Subject { - #[serde(rename = "content")] - pub content: Content, - #[serde(rename = "id")] - pub id: String, - #[serde(rename = "source", skip_serializing_if = "Option::is_none")] - pub source: Option, - #[serde(rename = "type")] - pub r#type: String, -} - -impl Subject { - pub fn from_json(ty: &str, json: serde_json::Value) -> Result { - Ok(Subject { - id: json["id"] - .as_str() - .ok_or_else(|| serde::de::Error::missing_field("id"))? - .to_string(), - r#type: json["type"] - .as_str() - .ok_or_else(|| serde::de::Error::missing_field("type"))? - .to_string(), - source: json["source"].as_str().map(|s| s.to_string()), - content: Content::from_json(ty, json["content"].clone())?, - }) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use assert_json_diff::assert_json_eq; - use rstest::rstest; - use std::fs; - use std::path::PathBuf; - - #[rstest] - fn for_each_example(#[files("../cdevents-spec/examples/*.json")] path: PathBuf) { - let example_json: serde_json::Value = serde_json::from_str( - fs::read_to_string(&path) - .expect("to read file as string") - .as_str(), - ) - .expect("to parse as json"); - dbg!(&example_json); - let cdevent: CDEvent = - serde_json::from_value(example_json.clone()).expect("to parse as cdevent"); - dbg!(&cdevent); - let cdevent_json = serde_json::to_value(cdevent).expect("to convert into json"); - dbg!(&cdevent_json); - assert_json_eq!(example_json, cdevent_json); - } -} +pub use subject::*; diff --git a/cdevents-sdk/src/serde.rs b/cdevents-sdk/src/serde.rs new file mode 100644 index 0000000..b0eca8a --- /dev/null +++ b/cdevents-sdk/src/serde.rs @@ -0,0 +1,77 @@ +// currently there is no generic way to handle Option +// 2 alertnatives: +// - write custom serializer +// - try to use the crate serde_with (serde_as) +// +// Creating, aliasing module with custom (de)serializer, +// also simplify the generation and introduce a small layer of abstraction/documentation. +// +// see [Using de/serialize\_with inside of an Option, Map, Vec · Issue #723 · serde-rs/serde](https://github.com/serde-rs/serde/issues/723) + +pub(crate) mod datetime { + use serde::{Deserializer, Serializer}; + + pub fn deserialize<'de, D>(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + time::serde::rfc3339::deserialize(deserializer) + } + + pub fn serialize(input: &time::OffsetDateTime, serializer: S) -> Result + where + S: Serializer, + { + time::serde::rfc3339::serialize(input, serializer) + } +} + +pub(crate) use uri_reference as uri; +pub(crate) use uri_reference_optional as uri_optional; + +pub(crate) mod uri_reference_optional { + use serde::{Deserialize, Deserializer, Serializer}; + + #[derive(Deserialize)] + struct Wrapper(#[serde(with = "crate::serde::uri_reference")] fluent_uri::Uri); + + pub fn deserialize<'de, D>(deserializer: D) -> Result>, D::Error> + where + D: Deserializer<'de>, + { + let v = Option::deserialize(deserializer)?; + Ok(v.map(|Wrapper(a)| a)) + } + + pub fn serialize( + input: &Option>, + serializer: S, + ) -> Result + where + S: Serializer, + { + match input { + None => serializer.serialize_none(), + Some(input) => crate::serde::uri_reference::serialize(input, serializer), + } + } +} + +pub(crate) mod uri_reference { + use serde::{de::Error, Deserialize, Deserializer, Serializer}; + + pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> + where + D: Deserializer<'de>, + { + let txt = String::deserialize(deserializer)?; + fluent_uri::Uri::parse_from(txt).map_err(|e| D::Error::custom(e.1)) + } + + pub fn serialize(input: &fluent_uri::Uri, serializer: S) -> Result + where + S: Serializer, + { + serializer.collect_str(input.as_str()) + } +} diff --git a/cdevents-sdk/src/subject.rs b/cdevents-sdk/src/subject.rs new file mode 100644 index 0000000..314ab4e --- /dev/null +++ b/cdevents-sdk/src/subject.rs @@ -0,0 +1,43 @@ +use serde::{Deserialize, Serialize}; + +use crate::Content; + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Subject { + #[serde(rename = "content")] + pub content: Content, + #[serde(rename = "id")] + pub id: String, + #[serde( + rename = "source", + default, + skip_serializing_if = "Option::is_none", + with = "crate::serde::uri_reference_optional" + )] + pub source: Option>, + #[serde(rename = "type")] + pub r#type: String, +} + +impl Subject { + pub fn from_json(ty: &str, json: serde_json::Value) -> Result { + Ok(Subject { + id: json["id"] + .as_str() + .ok_or_else(|| serde::de::Error::missing_field("id"))? + .to_string(), + r#type: json["type"] + .as_str() + .ok_or_else(|| serde::de::Error::missing_field("type"))? + .to_string(), + source: match json["source"].as_str() { + None => None, + Some(s) => Some( + fluent_uri::Uri::parse_from(s.to_owned()) + .map_err(|e| serde::de::Error::custom(e.1))?, + ), + }, + content: Content::from_json(ty, json["content"].clone())?, + }) + } +} diff --git a/cdevents-sdk/tests/specs.rs b/cdevents-sdk/tests/specs.rs new file mode 100644 index 0000000..3bab7d8 --- /dev/null +++ b/cdevents-sdk/tests/specs.rs @@ -0,0 +1,29 @@ +use assert_json_diff::assert_json_eq; +use cdevents_sdk::CDEvent; +use rstest::rstest; +use std::fs; +use std::path::PathBuf; + +#[rstest] +fn for_each_example(#[files("../cdevents-spec/examples/*.json")] path: PathBuf) { + let example_txt = fs::read_to_string(&path).expect("to read file as string"); + //HACK uri are stored ad http::Uri, they are "normalized" when serialized, so prenormalization to avoid failure like + // json atoms at path ".subject.content.repository.source" are not equal: + // lhs: + // "https://example.org" + // rhs: + // "https://example.org/" + // But it's not the case with fluent_uri::Uri + // + // example_txt = example_txt.replace("\"https://example.org\"", "\"https://example.org/\""); + + let example_json: serde_json::Value = + serde_json::from_str(&example_txt).expect("to parse as json"); + dbg!(&example_json); + let cdevent: CDEvent = + serde_json::from_value(example_json.clone()).expect("to parse as cdevent"); + dbg!(&cdevent); + let cdevent_json = serde_json::to_value(cdevent).expect("to convert into json"); + dbg!(&cdevent_json); + assert_json_eq!(example_json, cdevent_json); +} diff --git a/generator/src/main.rs b/generator/src/main.rs index 793af06..2be652f 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -1,6 +1,6 @@ use anyhow::{Context, Result}; use clap::Parser; -use cruet::{to_class_case, Inflector}; +use cruet::Inflector; use handlebars::{DirectorySourceOptions, Handlebars}; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; @@ -118,7 +118,11 @@ fn build_data_for_variants(jsonschema: Value) -> Value { }) } -type RustTypeName = String; +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +struct TypeInfo { + type_declaration: String, + serde_with: Option, +} #[derive(Debug, Clone, Serialize, Deserialize)] struct VariantInfo { @@ -128,7 +132,7 @@ struct VariantInfo { #[derive(Debug, Clone, Serialize, Deserialize)] struct StructDef { - type_name: RustTypeName, + type_info: TypeInfo, json_definition: Value, fields: Vec, } @@ -137,7 +141,7 @@ struct StructDef { struct FieldDef { rust_name: String, serde_name: String, - type_name: RustTypeName, + type_info: TypeInfo, is_optional: bool, } @@ -145,11 +149,41 @@ fn collect_structs( structs: &mut Vec, field_name: &str, json_definition: &Value, -) -> RustTypeName { +) -> TypeInfo { match json_definition["type"].as_str() { - Some("string") => "String".to_string(), + Some("string") => match json_definition["format"].as_str() { + Some("date-time") => TypeInfo { + type_declaration: "time::OffsetDateTime".to_string(), + serde_with: Some("crate::serde::datetime".to_string()), + ..Default::default() + }, + //TODO manage purl + Some("uri-reference") => TypeInfo { + type_declaration: "fluent_uri::Uri".to_string(), + serde_with: Some("crate::serde::uri_reference".to_string()), + ..Default::default() + }, + Some("uri") => TypeInfo { + type_declaration: "fluent_uri::Uri".to_string(), + serde_with: Some("crate::serde::uri".to_string()), + ..Default::default() + }, + // Some("uri") => TypeInfo { + // type_declaration: "http::Uri".to_string(), + // serde_with: Some("crate::serde::uri".to_string()), + // ..Default::default() + // }, + //TODO manage enum + _ => TypeInfo { + type_declaration: "String".to_string(), + ..Default::default() + }, + }, Some("object") => match json_definition["properties"].as_object() { - None => "serde_json::Map".to_string(), + None => TypeInfo { + type_declaration: "serde_json::Map".to_string(), + ..Default::default() + }, Some(fields_kv) => { let required = json_definition["required"].as_array(); let fields = fields_kv @@ -161,28 +195,32 @@ fn collect_structs( } else { serde_name.to_snake_case() }; - let mut type_name = collect_structs(structs, &serde_name, field.1); + let mut type_info = collect_structs(structs, &serde_name, field.1); let field_name = json!(&serde_name); let is_optional = required.map(|a| !a.contains(&field_name)).unwrap_or(true); if is_optional { - type_name = format!("Option<{}>", type_name); + type_info.type_declaration = + format!("Option<{}>", type_info.type_declaration); } FieldDef { rust_name, serde_name, - type_name, + type_info, is_optional, } }) .collect::>(); - let type_name = to_class_case(field_name); + let type_info = TypeInfo { + type_declaration: field_name.to_class_case(), + ..Default::default() + }; structs.push(StructDef { - type_name: type_name.clone(), + type_info: type_info.clone(), fields, json_definition: json_definition.clone(), }); - type_name + type_info } }, Some(x) => todo!("impl for type='{}'", x), diff --git a/generator/templates/mod.hbs b/generator/templates/mod.hbs index c918aff..8244a10 100644 --- a/generator/templates/mod.hbs +++ b/generator/templates/mod.hbs @@ -12,7 +12,7 @@ use serde::{Serialize, Deserialize}; pub const {{to_screaming_snake_case this.rust_module}}: &str = "{{ this.context_type }}"; {{/each}} -#[derive(Debug,Clone,Serialize,Deserialize, PartialEq, Eq)] +#[derive(Debug,Clone,Serialize,Deserialize)] #[serde(untagged)] // TODO how to use content of context.type as discriminator ? pub enum Content { {{#each variants }} diff --git a/generator/templates/variant.hbs b/generator/templates/variant.hbs index 6854e7d..41a7a25 100644 --- a/generator/templates/variant.hbs +++ b/generator/templates/variant.hbs @@ -2,11 +2,14 @@ use serde::{Serialize, Deserialize}; {{#each structs }} -#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] -pub struct {{ this.type_name }} { +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct {{ this.type_info.type_declaration }} { {{#each this.fields }} - #[serde(rename = "{{ this.serde_name }}"{{#if this.is_optional }}, skip_serializing_if = "Option::is_none"{{/if}})] - pub {{ this.rust_name }}: {{this.type_name }}, + #[serde(rename = "{{ this.serde_name }}", + {{~#if this.is_optional }} default, skip_serializing_if = "Option::is_none",{{/if~}} + {{~#if this.type_info.serde_with }} with = "{{ this.type_info.serde_with }}{{#if this.is_optional }}_optional{{/if}}",{{/if~}} + )] + pub {{ this.rust_name }}: {{this.type_info.type_declaration }}, {{/each}} }