Crate hedwig[−][src]
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::publish::EncodableMessage for &'a UserCreatedMessage { type Error = hedwig::validators::JsonSchemaValidatorError; type Validator = hedwig::validators::JsonSchemaValidator; fn topic(&self) -> hedwig::Topic { "user.created".into() } 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::publish::PublishBatch::new(); batch.message(&validator, &UserCreatedMessage { user_id: String::from("U_123") }); let mut result_stream = batch.publish(&publisher); let mut next_batch = hedwig::publish::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
consume | consume Types, traits, and functions necessary to consume messages using hedwig |
publish | publish Types, traits, and functions necessary to publish messages using hedwig |
validators | Implementations of validators. |
Structs
Topic | A message queue topic name to which messages can be published |
ValidatedMessage | A validated message. |
Enums
Error | All errors that may be returned when operating top level APIs. |
Type Definitions
Headers | Custom headers associated with a message. |