Crate hedwig

source ·
Expand description

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 hedwig::{validators, Publisher, Consumer};

#[derive(Clone, PartialEq, Eq, prost::Message)]
struct UserCreatedMessage {
    #[prost(string, tag = "1")]
    user_id: String,
}

impl<'a> hedwig::EncodableMessage for UserCreatedMessage {
    type Error = validators::ProstValidatorError;
    type Validator = validators::ProstValidator;
    fn topic(&self) -> hedwig::Topic {
        "user.created".into()
    }
    fn encode(&self, validator: &Self::Validator) -> Result<hedwig::ValidatedMessage, Self::Error> {
        Ok(validator.validate(
            uuid::Uuid::new_v4(),
            SystemTime::now(),
            "user.created/1.0",
            Default::default(),
            self,
        )?)
    }
}

impl hedwig::DecodableMessage for UserCreatedMessage {
    type Error = validators::ProstDecodeError<validators::prost::SchemaMismatchError>;
    type Decoder =
        validators::ProstDecoder<validators::prost::ExactSchemaMatcher<UserCreatedMessage>>;

    fn decode(msg: hedwig::ValidatedMessage, decoder: &Self::Decoder) -> Result<Self, Self::Error> {
        decoder.decode(msg)
    }
}


let publisher = /* Some publisher */
let consumer = /* Consumer associated to that publisher */

let mut publish_sink = Publisher::<UserCreatedMessage>::publish_sink(publisher, validators::ProstValidator::new());
let mut consumer_stream = consumer.consume::<UserCreatedMessage>(
    validators::ProstDecoder::new(validators::prost::ExactSchemaMatcher::new("user.created/1.0")),
);

publish_sink.send(UserCreatedMessage { user_id: String::from("U_123") }).await?;

assert_eq!(
    "U_123",
    consumer_stream.next().await.unwrap()?.ack().await?.user_id
);

Modules

  • Adapters for using GCP’s PubSub as a message service for hedwig
  • Message related types
  • In-memory messaging implementations, meant to imitate distributed messaging services for test purposes.
  • Implementations of validators.

Structs

Enums

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

Traits

Type Aliases