Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions cdevents-sdk/examples/samples_ok/custom_cdeventsx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"context": {
"specversion": "0.5.1",
"id": "0",
"source": "https://app.cdviz.dev/probe",
"type": "dev.cdeventsx.probes.ping.0.1.0",
"timestamp": "2026-06-25T12:00:00Z"
},
"subject": {
"id": "0",
"content": {}
}
}
13 changes: 13 additions & 0 deletions cdevents-sdk/examples/samples_ok/custom_domain.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"context": {
"specversion": "0.5.1",
"id": "0",
"source": "https://app.cdviz.dev/probe",
"type": "com.example.probes.ping.0.1.0",
"timestamp": "2026-06-25T12:00:00Z"
},
"subject": {
"id": "0",
"content": {}
}
}
12 changes: 9 additions & 3 deletions cdevents-sdk/src/generated/mod.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion cdevents-sdk/tests/specs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,12 @@ fn events_schemas() -> &'static EventsSchemas {
}

#[rstest]
fn can_serde_example(#[files("../cdevents-specs/spec-*/examples/*.json")] #[files("../cdevents-specs/spec-*/conformance/*.json")] #[files("../cdevents-specs/spec-*/custom/conformance.json")] path: PathBuf) {
fn can_serde_example(
#[files("../cdevents-specs/spec-*/examples/*.json")]
#[files("../cdevents-specs/spec-*/conformance/*.json")]
#[files("../cdevents-specs/spec-*/custom/conformance.json")]
#[files("examples/samples_ok/*.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:
Expand Down
12 changes: 9 additions & 3 deletions generator/templates/mod.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,26 @@ pub enum Content {

impl Content {
pub fn from_json(ty: &str, json: serde_json::Value) -> Result<Self, serde_json::Error>{
if ty.chars().filter(|&c| c == '.').count() != 6 {
return Err(serde_json::Error::custom(format_args!(
"invalid context.type format `{}`, expected '\{{tld}}.\{{domain}}.\{{subject}}.\{{predicate}}.\{{version_major}}.\{{version_minor}}.\{{version_patch}}(-\{{version_modifier}})?'",
ty,
)))
}
match ty {
{{#each variants }}
{{to_screaming_snake_case this.rust_module}} => {
let variant: {{this.rust_module}}::Content = serde_json::from_value(json)?;
Ok(variant.into())
},
{{/each}}
variant => if variant.starts_with("dev.cdeventsx.") {
Ok(Self::Custom{ ty: ty.to_string(), json })
} else {
variant => if variant.starts_with("dev.cdevents.") {
Err(serde_json::Error::custom(format_args!(
"unknown variant `{}`, expected 'dev.cdevents.\{{subject}}.\{{predicate}}.\{{version}}'",
variant,
)))
} else {
Ok(Self::Custom{ ty: ty.to_string(), json })
},
}
}
Expand Down
Loading