Skip to main content

HeaderFields

Trait HeaderFields 

Source
pub trait HeaderFields {
    const NAMES: &'static [&'static str];

    // Required method
    fn header_map(&self) -> HeaderMap;
}
Expand description

Header fields of the row: where its description sets InboxSpec::header_fields, the delivery builds its header map from them on its first read.

§Examples

use chrono::{DateTime, Utc};
use ruststream::HeaderMap;
use ruststream::runtime::{Input, SoloCarried};
use ruststream_sqlx::dialect::Column;
use ruststream_sqlx::prelude::*;
use ruststream_sqlx::spec::{self, Lease};
use ruststream_sqlx::{HeaderFields, InboxSpec, InboxTable, put_header};
use sqlx::{Sqlite, SqlitePool};

#[derive(Debug, Clone, sqlx::FromRow)]
pub struct OrderJob {
    job_id: i64,
    tenant: String,
    trace: Option<String>,
    note: Option<String>,
}

impl InboxTable for OrderJob {
    type Id = i64;
    type Table = InboxSpec<(Lease<DateTime<Utc>>, spec::HeaderFields)>;
    const TABLE: Self::Table = InboxSpec::new("order_jobs", Column::new("job_id").generated())
        .lease(Column::new("locked_until"))
        .group(Column::new("name"))
        .data(&[Column::new("tenant"), Column::new("trace"), Column::new("note")])
        .header_fields();

    fn id(&self) -> &i64 {
        &self.job_id
    }
}

impl Input for OrderJob {
    type Axis = SoloCarried<Self>;
}

impl HeaderFields for OrderJob {
    const NAMES: &'static [&'static str] = &["tenant", "trace"];

    fn header_map(&self) -> HeaderMap {
        let mut headers = HeaderMap::with_capacity(Self::NAMES.len());
        put_header(&mut headers, "tenant", &self.tenant);
        put_header(&mut headers, "trace", &self.trace);
        headers
    }
}

// The tenant's header reads like any other; the row holds the note.
#[subscriber(InboxQueue::<OrderJob>::new("orders"))]
async fn ship(job: &OrderJob, ctx: &mut Context<'_>) -> HandlerOutcome {
    match (ctx.headers().get_str("tenant"), job.note.as_deref()) {
        (Some(_), Some("fragile")) => HandlerOutcome::retry(),
        _ => HandlerOutcome::ack(),
    }
}

pub fn app(pool: SqlitePool) -> RustStream {
    RustStream::new(AppInfo::new("shop", "1.0.0")).with_broker(
        SqlxBroker::<Sqlite>::new(pool),
        |b| {
            b.include(ship);
        },
    )
}

Required Associated Constants§

Source

const NAMES: &'static [&'static str]

The headers the fields hold, by name; a publish that carries another is refused.

Required Methods§

Source

fn header_map(&self) -> HeaderMap

The header map: each field under its name, through put_header.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§