Skip to content

Commit

Permalink
fix: enforce non-empty string for id and hardcode mapping between c…
Browse files Browse the repository at this point in the history
…ontext.type and subject.type

Signed-off-by: David Bernard <[email protected]>
  • Loading branch information
davidB committed Jan 30, 2024
1 parent 7b1ce85 commit c60aa73
Show file tree
Hide file tree
Showing 40 changed files with 469 additions and 111 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ fn main() -> Result<(), Box<dyn Error>> {
pipeline_name: Some("testPipeline".into()),
url: Some("https://dev.pipeline.run/url".into())
})
.with_id("/dev/pipeline/run/1")
.with_id("/dev/pipeline/run/1".try_into()?)
.with_source("https://dev.pipeline.run/source".try_into()?)
)
.with_id("271069a8-fc18-44f1-b38f-9d70a1695819")
.with_id("271069a8-fc18-44f1-b38f-9d70a1695819".try_into()?)
.with_source("https://dev.cdevents".try_into()?)
;

Expand All @@ -52,7 +52,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let cloudevent_received: Event = cloudevent.clone();
let cdevent_extracted: CDEvent = cloudevent_received.try_into()?;

assert_eq!(cloudevent.id(), cdevent_extracted.id());
assert_eq!(cloudevent.id(), cdevent_extracted.id().to_string());
assert_eq!(cdevent_expected, cdevent_extracted);
Ok(())
}
Expand Down
6 changes: 3 additions & 3 deletions cdevents-sdk/examples/pipelinerun_finished.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ fn main() -> Result<(), Box<dyn Error>> {
pipeline_name: Some("testPipeline".into()),
url: Some("https://dev.pipeline.run/url".into())
})
.with_id("/dev/pipeline/run/1")
.with_id("/dev/pipeline/run/1".try_into()?)
.with_source("https://dev.pipeline.run/source".try_into()?)
)
.with_id("271069a8-fc18-44f1-b38f-9d70a1695819")
.with_id("271069a8-fc18-44f1-b38f-9d70a1695819".try_into()?)
.with_source("https://dev.cdevents".try_into()?)
;

Expand All @@ -34,7 +34,7 @@ fn main() -> Result<(), Box<dyn Error>> {
let cloudevent_received: Event = cloudevent.clone();
let cdevent_extracted: CDEvent = cloudevent_received.try_into()?;

assert_eq!(cloudevent.id(), cdevent_extracted.id());
assert_eq!(cloudevent.id(), cdevent_extracted.id().to_string());
assert_eq!(cdevent_expected, cdevent_extracted);
Ok(())
}
Expand Down
26 changes: 11 additions & 15 deletions cdevents-sdk/src/cdevent.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{Context, Subject, UriReference};
use crate::{Context, Id, Subject, UriReference};
use serde::{
de::{self, Deserializer, MapAccess, Visitor},
Deserialize, Serialize,
Expand All @@ -22,7 +22,7 @@ pub struct CDEvent {
impl From<Subject> for CDEvent {
fn from(subject: Subject) -> Self {
let context = Context {
ty: subject.ty().into(),
ty: subject.content().ty().into(),
..Default::default()
};
Self {
Expand All @@ -46,12 +46,12 @@ impl CDEvent {
}

/// see <https://github.com/cdevents/spec/blob/main/spec.md#id-context>
pub fn id(&self) -> &str {
self.context.id.as_str()
pub fn id(&self) -> &Id {
&self.context.id
}

pub fn with_id<T>(mut self, v: T) -> Self where T: Into<String> {
self.context.id = v.into();
pub fn with_id(mut self, v: Id) -> Self {
self.context.id = v;
self
}

Expand Down Expand Up @@ -83,8 +83,8 @@ impl CDEvent {
/// see <https://github.com/cdevents/spec/blob/main/spec.md#type-context>
/// derived from subject.content
pub fn ty(&self) -> &str {
//self.context.ty()
self.subject.ty()
//self.subject.content().ty()
self.context.ty.as_str()
}

/// see <https://github.com/cdevents/spec/blob/main/spec.md#customdata>
Expand Down Expand Up @@ -199,14 +199,10 @@ impl<> proptest::arbitrary::Arbitrary for CDEvent {
use proptest::prelude::*;
(
any::<Subject>(),
"\\PC*",
any::<Option<UriReference>>(),
any::<Id>(),
any::<UriReference>(),
).prop_map(|(subject, id, source)| {
let mut cdevent = CDEvent::from(subject).with_id(id);
if let Some(source) = source {
cdevent = cdevent.with_source(source);
}
cdevent
CDEvent::from(subject).with_id(id).with_source(source)
}).boxed()
}
}
Expand Down
8 changes: 4 additions & 4 deletions cdevents-sdk/src/cloudevents.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ mod tests {
fn test_into_cloudevent() -> Result<(), Box<dyn std::error::Error>> {
let cdevent = CDEvent::from(
Subject::from(build_queued::Content{})
.with_id("subject123")
.with_id("subject123".try_into()?)
.with_source("/event/source/123".try_into()?)
)
.with_id("271069a8-fc18-44f1-b38f-9d70a1695819")
.with_id("271069a8-fc18-44f1-b38f-9d70a1695819".try_into()?)
.with_source("https://dev.cdevents".try_into()?)
;

Expand All @@ -82,11 +82,11 @@ mod tests {
assert_eq!(cloudevent_via_builder, cloudevent);

assert_eq!(cloudevent.id(), "271069a8-fc18-44f1-b38f-9d70a1695819");
assert_eq!(cloudevent.id(), cdevent.id());
assert_eq!(cloudevent.id(), cdevent.id().to_string());

let (_, _, data) = cloudevent.take_data();
let cdevent_extracted: CDEvent = data.ok_or(Error::DataNotFoundInCloudEvent)?.try_into()?;
assert_eq!(cloudevent.id(), cdevent_extracted.id());
assert_eq!(cloudevent.id(), cdevent_extracted.id().to_string());
assert_eq!(cdevent, cdevent_extracted);
Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions cdevents-sdk/src/context.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use serde::{Deserialize, Serialize};

use crate::UriReference;
use crate::{Id, UriReference};

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(deny_unknown_fields)]
pub(crate) struct Context {
pub(crate) version: String,
pub(crate) id: String,
pub(crate) id: Id,
pub(crate) source: UriReference,
#[serde(rename = "type")]
pub(crate) ty: String,
Expand All @@ -18,8 +18,8 @@ impl Default for Context {
fn default() -> Self {
Self {
version: "0.3.0".into(),
id: "00000000-0000-0000-0000-000000000000".into(),
source: UriReference::default(),
id: Id::default(),
source: "/undef".try_into().expect("/undef is a valid uri-reference"),
ty: "dev.cdevents.undef.undef.0.0.0".into(),
timestamp: time::OffsetDateTime::now_utc(),
}
Expand Down
4 changes: 4 additions & 0 deletions cdevents-sdk/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use thiserror::Error;

// type Result<T> = std::result::Result<T, Error>;

#[derive(Error, Debug)]
pub enum Error {
#[error("Empty data in cloudevent")]
Expand All @@ -10,4 +12,6 @@ pub enum Error {
SerdeJsonError( #[from] serde_json::Error),
#[error("unknown error")]
Unknown,
#[error("{0} should be non-empty")]
EmptyString(&'static str)
}
2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/artifact_packaged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentChange {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/branch_created.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentRepository {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/branch_deleted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentRepository {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/change_abandoned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentRepository {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/change_created.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentRepository {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/change_merged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentRepository {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/change_reviewed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentRepository {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
2 changes: 1 addition & 1 deletion cdevents-sdk/src/generated/change_updated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentRepository {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
6 changes: 3 additions & 3 deletions cdevents-sdk/src/generated/incident_detected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Serialize, Deserialize};
#[serde(deny_unknown_fields)]
pub struct Content {
#[serde(rename = "artifactId", default, skip_serializing_if = "Option::is_none",)]
pub artifact_id: Option<String>,
pub artifact_id: Option<crate::Id>,
#[serde(rename = "description", default, skip_serializing_if = "Option::is_none",)]
pub description: Option<String>,
#[serde(rename = "environment",)]
Expand All @@ -23,7 +23,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentService {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand All @@ -33,7 +33,7 @@ pub struct ContentService {
#[serde(deny_unknown_fields)]
pub struct ContentEnvironment {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
6 changes: 3 additions & 3 deletions cdevents-sdk/src/generated/incident_reported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Serialize, Deserialize};
#[serde(deny_unknown_fields)]
pub struct Content {
#[serde(rename = "artifactId", default, skip_serializing_if = "Option::is_none",)]
pub artifact_id: Option<String>,
pub artifact_id: Option<crate::Id>,
#[serde(rename = "description", default, skip_serializing_if = "Option::is_none",)]
pub description: Option<String>,
#[serde(rename = "environment",)]
Expand All @@ -25,7 +25,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentService {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand All @@ -35,7 +35,7 @@ pub struct ContentService {
#[serde(deny_unknown_fields)]
pub struct ContentEnvironment {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
6 changes: 3 additions & 3 deletions cdevents-sdk/src/generated/incident_resolved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use serde::{Serialize, Deserialize};
#[serde(deny_unknown_fields)]
pub struct Content {
#[serde(rename = "artifactId", default, skip_serializing_if = "Option::is_none",)]
pub artifact_id: Option<String>,
pub artifact_id: Option<crate::Id>,
#[serde(rename = "description", default, skip_serializing_if = "Option::is_none",)]
pub description: Option<String>,
#[serde(rename = "environment",)]
Expand All @@ -23,7 +23,7 @@ pub struct Content {
#[serde(deny_unknown_fields)]
pub struct ContentService {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand All @@ -33,7 +33,7 @@ pub struct ContentService {
#[serde(deny_unknown_fields)]
pub struct ContentEnvironment {
#[serde(rename = "id",)]
pub id: String,
pub id: crate::Id,
#[serde(rename = "source", default, skip_serializing_if = "Option::is_none",)]
pub source: Option<crate::UriReference>,
}
Expand Down
Loading

0 comments on commit c60aa73

Please sign in to comment.