Skip to content

Commit

Permalink
♻️ use more precise type for uri & datetime than String
Browse files Browse the repository at this point in the history
- uri => fluent_uri::Uri<String>
- uri-referece => fluent_uri::Uri<String>
- datetime => time::OffsetDateTime
  • Loading branch information
davidB committed Jan 14, 2024
1 parent af73b90 commit 3e2b453
Show file tree
Hide file tree
Showing 50 changed files with 642 additions and 496 deletions.
6 changes: 4 additions & 2 deletions cdevents-sdk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"] }

Expand Down
98 changes: 98 additions & 0 deletions cdevents-sdk/src/cdevent.rs
Original file line number Diff line number Diff line change
@@ -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_json::Value>,
#[serde(
rename = "customDataContentType",
skip_serializing_if = "Option::is_none"
)]
pub custom_data_content_type: Option<String>,
}

impl<'de> Deserialize<'de> for CDEvent {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
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<V>(self, mut map: V) -> Result<CDEvent, V::Error>
where
V: MapAccess<'de>,
{
let mut context: Option<Context> = None;
let mut subject_json: Option<serde_json::value::Value> = 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)
}
}
13 changes: 13 additions & 0 deletions cdevents-sdk/src/context.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
#[serde(rename = "type")]
pub r#type: String,
#[serde(with = "crate::serde::datetime")]
pub timestamp: time::OffsetDateTime,
}
12 changes: 6 additions & 6 deletions cdevents-sdk/src/generated/artifact_packaged.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)]
pub source: Option<fluent_uri::Uri<String>>,
}

2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/artifact_published.rs
Original file line number Diff line number Diff line change
@@ -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 {
}

4 changes: 2 additions & 2 deletions cdevents-sdk/src/generated/artifact_signed.rs
Original file line number Diff line number Diff line change
@@ -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,
}

12 changes: 6 additions & 6 deletions cdevents-sdk/src/generated/branch_created.rs
Original file line number Diff line number Diff line change
@@ -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<Repository>,
}

#[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<String>,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)]
pub source: Option<fluent_uri::Uri<String>>,
}

12 changes: 6 additions & 6 deletions cdevents-sdk/src/generated/branch_deleted.rs
Original file line number Diff line number Diff line change
@@ -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<Repository>,
}

#[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<String>,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)]
pub source: Option<fluent_uri::Uri<String>>,
}

4 changes: 2 additions & 2 deletions cdevents-sdk/src/generated/build_finished.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/build_queued.rs
Original file line number Diff line number Diff line change
@@ -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 {
}

2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/build_started.rs
Original file line number Diff line number Diff line change
@@ -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 {
}

12 changes: 6 additions & 6 deletions cdevents-sdk/src/generated/change_abandoned.rs
Original file line number Diff line number Diff line change
@@ -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<Repository>,
}

#[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<String>,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)]
pub source: Option<fluent_uri::Uri<String>>,
}

12 changes: 6 additions & 6 deletions cdevents-sdk/src/generated/change_created.rs
Original file line number Diff line number Diff line change
@@ -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<Repository>,
}

#[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<String>,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)]
pub source: Option<fluent_uri::Uri<String>>,
}

12 changes: 6 additions & 6 deletions cdevents-sdk/src/generated/change_merged.rs
Original file line number Diff line number Diff line change
@@ -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<Repository>,
}

#[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<String>,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)]
pub source: Option<fluent_uri::Uri<String>>,
}

12 changes: 6 additions & 6 deletions cdevents-sdk/src/generated/change_reviewed.rs
Original file line number Diff line number Diff line change
@@ -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<Repository>,
}

#[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<String>,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)]
pub source: Option<fluent_uri::Uri<String>>,
}

12 changes: 6 additions & 6 deletions cdevents-sdk/src/generated/change_updated.rs
Original file line number Diff line number Diff line change
@@ -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<Repository>,
}

#[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<String>,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none", with = "crate::serde::uri_reference_optional",)]
pub source: Option<fluent_uri::Uri<String>>,
}

6 changes: 3 additions & 3 deletions cdevents-sdk/src/generated/environment_created.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
#[serde(rename = "url", skip_serializing_if = "Option::is_none")]
#[serde(rename = "url", default, skip_serializing_if = "Option::is_none",)]
pub url: Option<String>,
}

4 changes: 2 additions & 2 deletions cdevents-sdk/src/generated/environment_deleted.rs
Original file line number Diff line number Diff line change
@@ -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<String>,
}

Loading

0 comments on commit 3e2b453

Please sign in to comment.