[][src]Crate hedwig

Hedwig is a message bus library that works with arbitrary pubsub services such as AWS SNS/SQS or Google Cloud Pubsub. Messages are validated before they are published. The publisher and consumer are de-coupled and fan-out is supported out of the box.

The Rust library currently only supports publishing.

Examples

Publish a message. Payload encoded with JSON and validated using a JSON Schema.

use uuid::Uuid;
use std::{path::Path, time::SystemTime};
use futures_util::stream::StreamExt;


let schema = r#"{
    "$id": "https://hedwig.corp/schema",
    "$schema": "https://json-schema.org/draft-04/schema#",
    "description": "Example Schema",
    "schemas": {
        "user-created": {
            "1.*": {
                "description": "A new user was created",
                "type": "object",
                "x-versions": [
                    "1.0"
                ],
                "required": [
                    "user_id"
                ],
                "properties": {
                    "user_id": {
                        "$ref": "https://hedwig.corp/schema#/definitions/UserId/1.0"
                    }
                }
            }
        }
    },
    "definitions": {
        "UserId": {
            "1.0": {
                "type": "string"
            }
        }
    }
}"#;

#[derive(serde::Serialize)]
struct UserCreatedMessage {
    user_id: String,
}

impl<'a> hedwig::Message for &'a UserCreatedMessage {
    type Error = hedwig::validators::JsonSchemaValidatorError;
    type Validator = hedwig::validators::JsonSchemaValidator;
    fn topic(&self) -> &'static str { "user.created" }
    fn encode(self, validator: &Self::Validator)
    -> Result<hedwig::ValidatedMessage, Self::Error> {
        validator.validate(
            Uuid::new_v4(),
            SystemTime::now(),
            "https://hedwig.corp/schema#/schemas/user.created/1.0",
            hedwig::Headers::new(),
            self,
        )
    }
}

let publisher = /* Some publisher */
let validator = hedwig::validators::JsonSchemaValidator::new(schema)?;
let mut batch = hedwig::PublishBatch::new();
batch.message(&validator, &UserCreatedMessage { user_id: String::from("U_123") });
let mut result_stream = batch.publish(&publisher);
let mut next_batch = hedwig::PublishBatch::new();
async {
    while let Some(result) = result_stream.next().await {
        match result {
            (Ok(id), _, msg) => {
                println!("message {} published successfully: {:?}", msg.uuid(), id);
            }
            (Err(e), topic, msg) => {
                eprintln!("failed to publish {}: {}", msg.uuid(), e);
                next_batch.push(topic, msg);
            }
        }
    }
};

Modules

publishers

Publisher implementations.

validators

Implementations of validators.

Structs

PublishBatch

A convenience builder for publishing in batches.

ValidatedMessage

A validated message.

Enums

Error

All errors that may be returned when operating top level APIs.

Traits

Message

Types that can be encoded and published.

Publisher

Message publishers.

Type Definitions

Headers

Custom headers associated with a message.