pub trait HeaderField {
// Required method
fn header(&self) -> Option<Vec<u8>>;
}Expand description
A value a field of a headers struct holds, as the header the delivery’s header map carries.
A field of a struct deriving InboxHeaders without a role becomes the header of its
column’s name. Strings and byte vectors are their bytes, integers and bool their decimal
text, and chrono::DateTime<Utc> (feature chrono) and time::OffsetDateTime (feature
time) their RFC 3339 text. None leaves the header out. A service implements it for a type
of its own.
§Examples
use ruststream_sqlx::HeaderField;
use ruststream_sqlx::prelude::*;
/// A region code the table keeps as text.
#[derive(Debug, Clone, sqlx::Type)]
#[sqlx(transparent)]
pub struct Region(String);
// The header carries the code in capitals, as the service's consumers read it.
impl HeaderField for Region {
fn header(&self) -> Option<Vec<u8>> {
Some(self.0.to_uppercase().into_bytes())
}
}
#[derive(Debug, Clone, InboxHeaders, sqlx::FromRow)]
#[inbox(table = "shipments")]
pub struct ShipmentHeaders {
#[field(id, generated)]
id: i64,
region: Region,
}
#[derive(Debug, Clone, Inbox, sqlx::FromRow)]
pub struct Shipment {
#[field(headers)]
#[sqlx(flatten)]
headers: ShipmentHeaders,
address: String,
}
#[subscriber(InboxQueue::<Shipment>::new("shipments"))]
async fn ship(shipment: &Shipment, ctx: &mut Context<'_>) -> HandlerOutcome {
let region = ctx.headers().get_str("region").unwrap_or("none");
tracing::info!(region, to = %shipment.address, "shipping");
HandlerOutcome::ack()
}
pub fn app(pool: sqlx::PgPool) -> RustStream {
RustStream::new(AppInfo::new("shipping", "0.1.0")).with_broker(SqlxBroker::new(pool), |b| {
b.include(ship);
})
}Required Methods§
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".
Implementations on Foreign Types§
Source§impl HeaderField for DateTime<Utc>
Available on crate feature chrono only.
impl HeaderField for DateTime<Utc>
Available on crate feature
chrono only.Source§impl HeaderField for OffsetDateTime
Available on crate feature time only.
impl HeaderField for OffsetDateTime
Available on crate feature
time only.