Skip to content

Commit 35071ee

Browse files
committed
wip: add support for spec 0.5
1 parent 561d0a1 commit 35071ee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3527
-102
lines changed

.gitattributes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Auto detect text files and normalize line endings to LF
2+
* text=auto eol=lf
3+
4+
# generated & committed
5+
cdevents-sdk/src/generated/** linguist-generated=true gitlab-generated
6+
7+
# Lock files (generated but should be diffed)
8+
Cargo.lock linguist-generated=false
9+
package-lock.json linguist-generated=false
10+
bun.lockb binary

.gitmodules

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@
99
path = cdevents-specs/spec-v0.4
1010
url = https://github.com/cdevents/spec.git
1111
branch = spec-v0.4
12+
[submodule "cdevents-specs/spec-v0.5"]
13+
path = cdevents-specs/spec-v0.5
14+
url = https://github.com/cdevents/spec.git
15+
branch = spec-v0.5

.mise.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ run = [
7070
# "git submodule add -f https://github.com/cdevents/spec.git cdevents-specs/main",
7171
# "git submodule add -f -b spec-v0.3 https://github.com/cdevents/spec.git cdevents-specs/spec-v0.3",
7272
# "git submodule add -f -b spec-v0.4 https://github.com/cdevents/spec.git cdevents-specs/spec-v0.4",
73+
# "git submodule add -f -b spec-v0.5 https://github.com/cdevents/spec.git cdevents-specs/spec-v0.5",
7374
# "git submodule update -f --rebase -- cdevents-specs/main",
7475
# ]
7576

README.md

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ The SDK can be used to create CDEvents and send them as CloudEvents, as well as
99
Import the modules in your code
1010

1111
```toml
12-
cdevents-sdk = "0.1"
12+
cdevents-sdk = "0.3"
1313
```
1414

1515
To send a CDEvent as CloudEvent:
1616

17-
```rust
17+
````rust
1818
// from examples/pipelinerun_finished.rs
1919
use std::error::Error;
2020

21-
use cdevents_sdk::{CDEvent, Subject, spec_0_3_0::pipelinerun_finished, Content};
21+
use cdevents_sdk::{CDEvent, Subject, spec_0_5_0::pipelinerun_finished, Content};
2222
use cloudevents::{Event, AttributesReader};
2323

2424
fn main() -> Result<(), Box<dyn Error>> {
@@ -27,7 +27,7 @@ fn main() -> Result<(), Box<dyn Error>> {
2727
errors: Some("pipelineErrors".into()),
2828
outcome: Some("success".into()),
2929
pipeline_name: Some("testPipeline".into()),
30-
url: Some("https://dev.pipeline.run/url".into())
30+
uri: Some("https://dev.pipeline.run/url".into())
3131
})
3232
.with_id("/dev/pipeline/run/1".try_into()?)
3333
.with_source("https://dev.pipeline.run/source".try_into()?)
@@ -56,14 +56,15 @@ fn main() -> Result<(), Box<dyn Error>> {
5656
assert_eq!(cdevent_expected, cdevent_extracted);
5757
Ok(())
5858
}
59-
```
59+
````
6060

6161
See the [CloudEvents](https://github.com/cloudevents/sdk-rust) docs as well.
6262

6363
## Features
6464

65-
- [x] support cdevents spec 0.3.0
65+
- [x] support cdevents spec 0.5.0
6666
- [x] support cdevents spec 0.4.1
67+
- [x] support cdevents spec 0.3.0
6768
- [ ] support of custom event
6869
- [ ] compile-time generation of type for custom event
6970
- [ ] runtime validation (download of jsonschemas & validation)

cdevents-sdk/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ description = "A Rust SDK for CDEvents"
1111

1212
[dependencies]
1313
cloudevents-sdk = { version = "0.9", optional = true, default-features = false }
14+
enum_dispatch = "0.3"
1415
fluent-uri = { version = "0.4", features = ["serde"] }
1516
proptest = { version = "1", optional = true }
1617
proptest-derive = { version = "0.7", optional = true }

cdevents-sdk/examples/pipelinerun_finished.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ fn main() -> Result<(), Box<dyn Error>> {
77
let cdevent = CDEvent::from(
88
Subject::from(pipelinerun_finished::Content{
99
errors: Some("pipelineErrors".into()),
10-
outcome: Some("success".into()),
10+
outcome: Some(pipelinerun_finished::ContentOutcome::Success),
1111
pipeline_name: Some("testPipeline".into()),
12-
url: Some("https://dev.pipeline.run/url".into())
12+
uri: Some("https://dev.pipeline.run/url".try_into()?)
1313
})
1414
.with_id("/dev/pipeline/run/1".try_into()?)
1515
.with_source("https://dev.pipeline.run/source".try_into()?)
@@ -38,4 +38,3 @@ fn main() -> Result<(), Box<dyn Error>> {
3838
assert_eq!(cdevent_expected, cdevent_extracted);
3939
Ok(())
4040
}
41-

cdevents-sdk/src/cdevent.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Context, Id, Subject, UriReference};
1+
use crate::{Context, ContextEnum, Id, Subject, UriReference};
22
use serde::{
33
de::{self, Deserializer, MapAccess, Visitor},
44
Deserialize, Serialize,
@@ -8,7 +8,7 @@ use std::fmt;
88
#[derive(Debug, Clone, Serialize, PartialEq, Eq)]
99
#[serde(deny_unknown_fields)]
1010
pub struct CDEvent {
11-
context: Context,
11+
context: ContextEnum,
1212
subject: Subject,
1313
#[serde(rename = "customData", skip_serializing_if = "Option::is_none")]
1414
custom_data: Option<serde_json::Value>,
@@ -21,10 +21,8 @@ pub struct CDEvent {
2121

2222
impl From<Subject> for CDEvent {
2323
fn from(subject: Subject) -> Self {
24-
let context = Context {
25-
ty: subject.content().ty().into(),
26-
..Default::default()
27-
};
24+
// TODO select context from subject version
25+
let context = crate::new_context(subject.content().ty());
2826
Self {
2927
context,
3028
subject,
@@ -37,41 +35,41 @@ impl From<Subject> for CDEvent {
3735
impl CDEvent {
3836
/// see <https://github.com/cdevents/spec/blob/main/spec.md#version>
3937
pub fn version(&self) -> &str {
40-
self.context.version.as_str()
38+
self.context.version()
4139
}
4240

43-
pub fn with_version<T>(mut self, v: T) -> Self where T: Into<String> {
44-
self.context.version = v.into();
45-
self
46-
}
41+
// pub fn with_version<T>(mut self, v: T) -> Self where T: Into<String> {
42+
// self.context.with_version(v);
43+
// self
44+
// }
4745

4846
/// see <https://github.com/cdevents/spec/blob/main/spec.md#id-context>
4947
pub fn id(&self) -> &Id {
50-
&self.context.id
48+
self.context.id()
5149
}
5250

5351
pub fn with_id(mut self, v: Id) -> Self {
54-
self.context.id = v;
52+
self.context = self.context.with_id(v);
5553
self
5654
}
5755

5856
/// see <https://github.com/cdevents/spec/blob/main/spec.md#source-context>
5957
pub fn source(&self) -> &UriReference {
60-
&self.context.source
58+
self.context.source()
6159
}
6260

6361
pub fn with_source(mut self, v: UriReference) -> Self {
64-
self.context.source = v;
62+
self.context = self.context.with_source(v);
6563
self
6664
}
6765

6866
/// see <https://github.com/cdevents/spec/blob/main/spec.md#timestamp>
6967
pub fn timestamp(&self) -> &time::OffsetDateTime {
70-
&self.context.timestamp
68+
self.context.timestamp()
7169
}
7270

7371
pub fn with_timestamp(mut self, v: time::OffsetDateTime) -> Self {
74-
self.context.timestamp = v;
72+
self.context = self.context.with_timestamp(v);
7573
self
7674
}
7775

@@ -84,7 +82,7 @@ impl CDEvent {
8482
/// derived from subject.content
8583
pub fn ty(&self) -> &str {
8684
//self.subject.content().ty()
87-
self.context.ty.as_str()
85+
self.context.ty()
8886
}
8987

9088
/// see <https://github.com/cdevents/spec/blob/main/spec.md#customdata>
@@ -127,6 +125,7 @@ impl<'de> Deserialize<'de> for CDEvent {
127125

128126
struct CDEventVisitor;
129127

128+
// TODO remove dependencie to serde_json
130129
impl<'de> Visitor<'de> for CDEventVisitor {
131130
type Value = CDEvent;
132131

@@ -138,17 +137,17 @@ impl<'de> Deserialize<'de> for CDEvent {
138137
where
139138
V: MapAccess<'de>,
140139
{
141-
let mut context: Option<Context> = None;
140+
let mut context_json: Option<serde_json::value::Value> = None;
142141
let mut subject_json: Option<serde_json::value::Value> = None;
143142
let mut custom_data = None;
144143
let mut custom_data_content_type = None;
145144
while let Some(key) = map.next_key()? {
146145
match key {
147146
Field::Context => {
148-
if context.is_some() {
147+
if context_json.is_some() {
149148
return Err(de::Error::duplicate_field("context"));
150149
}
151-
context = Some(map.next_value()?);
150+
context_json = Some(map.next_value()?);
152151
}
153152
Field::Subject => {
154153
if subject_json.is_some() {
@@ -170,11 +169,14 @@ impl<'de> Deserialize<'de> for CDEvent {
170169
}
171170
}
172171
}
173-
let context = context.ok_or_else(|| de::Error::missing_field("context"))?;
174-
let subject_json =
175-
subject_json.ok_or_else(|| de::Error::missing_field("subject"))?;
176-
let subject =
177-
Subject::from_json(&context.ty, subject_json).map_err(de::Error::custom)?;
172+
let context = {
173+
let context_json = context_json.ok_or_else(|| de::Error::missing_field("context"))?;
174+
ContextEnum::from_json(context_json).map_err(de::Error::custom)?
175+
};
176+
let subject = {
177+
let subject_json = subject_json.ok_or_else(|| de::Error::missing_field("subject"))?;
178+
Subject::from_json(context.ty(), subject_json).map_err(de::Error::custom)?
179+
};
178180

179181
Ok(CDEvent {
180182
context,
@@ -221,4 +223,4 @@ mod tests {
221223
assert_eq!(s, actual);
222224
}
223225
}
224-
}
226+
}

0 commit comments

Comments
 (0)