5 releases

Uses new Rust 2024

new 0.2.0 Apr 16, 2025
0.1.3 Jan 9, 2025
0.1.2 Jan 7, 2025
0.1.1 Jan 7, 2025
0.1.0 Sep 1, 2024

#15 in #fluent

Download history 171/week @ 2025-01-02 168/week @ 2025-01-09 18/week @ 2025-01-16 37/week @ 2025-01-23 17/week @ 2025-01-30 45/week @ 2025-02-06 6/week @ 2025-02-13 18/week @ 2025-02-20 19/week @ 2025-02-27 7/week @ 2025-03-06 2/week @ 2025-03-13 9/week @ 2025-03-20 40/week @ 2025-03-27 90/week @ 2025-04-03 77/week @ 2025-04-10

220 downloads per month
Used in cdviz-collector

Apache-2.0

255KB
5.5K SLoC

Rust CDEvents SDK

Rust SDK to emit CDEvents.

The SDK can be used to create CDEvents and send them as CloudEvents, as well as parse a received CloudEvent into a CDEvent.

Create and send your first CDEvent as CloudEvent

Import the modules in your code

cdevents-sdk = "0.1"

To send a CDEvent as CloudEvent:

// from examples/pipelinerun_finished.rs
use std::error::Error;

use cdevents_sdk::{CDEvent, Subject, spec_0_3_0::pipelinerun_finished, Content};
use cloudevents::{Event, AttributesReader};

fn main() -> Result<(), Box<dyn Error>> {
    let cdevent = CDEvent::from(
        Subject::from(pipelinerun_finished::Content{
            errors: Some("pipelineErrors".into()),
            outcome: Some("success".into()),
            pipeline_name: Some("testPipeline".into()),
            url: Some("https://dev.pipeline.run/url".into())
        })
        .with_id("/dev/pipeline/run/1".try_into()?)
        .with_source("https://dev.pipeline.run/source".try_into()?)
    )
    .with_id("271069a8-fc18-44f1-b38f-9d70a1695819".try_into()?)
    .with_source("https://dev.cdevents".try_into()?)
    ;

    let cdevent_expected = cdevent.clone();

    // shortcut for creating cloudevents with
    //
    // ```rust
    // use cloudevents::event::EventBuilderV10;
    // use cdevents_sdk::cloudevents::BuilderExt;
    //
    // let mut cloudevent = EventBuilderV10::new().with_cdevent(cdevent.clone())?.build()?;
    // ```
    let cloudevent: Event = cdevent.try_into()?;

    // zero transport, but cloning
    let cloudevent_received: Event = cloudevent.clone();
    let cdevent_extracted: CDEvent = cloudevent_received.try_into()?;

    assert_eq!(cloudevent.id(), cdevent_extracted.id().to_string());
    assert_eq!(cdevent_expected, cdevent_extracted);
    Ok(())
}

See the CloudEvents docs as well.

Features

  • support cdevents spec 0.3.0
  • support cdevents spec 0.4.1
  • support of custom event
    • compile-time generation of type for custom event
    • runtime validation (download of jsonschemas & validation)
    • serialize/deserialize of custom event (type dev.cdeventsx.{subject}.{predicate}.{version})
    • partial validation of custom event (subject.content is not validated, stored as json)
  • Cloudevents support (provide wrapper/extractor for CDEvent)
  • CDEvent stored into static types (=> no use of jsonshema at runtime)
  • rutime validation for scalar types (purl, datetime, uri-reference)
  • report clear and readable error messages on deserialization
  • provide type with builder pattern (fluent)
  • provide random/sample generator for CDEvent (property based testing)
  • test serialization(deserialization(of spec's examples & conformances)) == spec's examples & conformances
  • test serialization(random CDEvent) matches the jsonschemas

References

Dependencies

~1.6–5MB
~94K SLoC