pub struct Task { /* private fields */ }Expand description
A v1 task envelope: the flat map of string fields a producer adds to a task stream.
Every task carries task_id, model_type, model_id, task_name, model_name, source and
v, and reply_to when the producer names its reply stream. Every other field, the payload
and the optional settings alike, is the caller’s: this crate keeps them in order and never
reads them. A task parsed from a stream and written back gives the same fields in the same
order.
Debug shows the identity fields and the names of the others, never their values: a payload
can be large, or a credential such as a presigned URL.
use legit_streams::Task;
let task = Task::builder("app", "post", "42")
.task_id("app_01J0000000000000000000000A")
.field("text_data", "Hello")
.build()?;
assert_eq!(task.get("text_data"), Some("Hello"));
assert_eq!(task.task_name(), "default");
assert_eq!(Task::from_fields(task.fields().to_vec())?, task);Implementations§
Source§impl Task
impl Task
Sourcepub fn builder(
source: impl Into<String>,
model_type: impl Into<String>,
model_id: impl Into<String>,
) -> TaskBuilder
pub fn builder( source: impl Into<String>, model_type: impl Into<String>, model_id: impl Into<String>, ) -> TaskBuilder
Start a task for the application source, about the caller’s record model_id of type
model_type. The worker echoes both back untouched.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").build()?;
assert!(task.task_id().starts_with("app_"));Sourcepub fn from_entry(entry: &Entry) -> Result<Self>
pub fn from_entry(entry: &Entry) -> Result<Self>
Read a task from a stream entry, within the default Limits.
§Errors
The errors of Task::from_raw.
use legit_streams::{Entry, EntryId, Task};
let entry = Entry::new(EntryId::ZERO, vec![(b"v".to_vec(), b"2".to_vec())]);
assert!(Task::from_entry(&entry).is_err());Sourcepub fn from_raw(raw: &[(Vec<u8>, Vec<u8>)], limits: &Limits) -> Result<Self>
pub fn from_raw(raw: &[(Vec<u8>, Vec<u8>)], limits: &Limits) -> Result<Self>
Read a task from raw stream fields.
§Errors
Error::TooLarge for an entry over limits, Error::InvalidUtf8 for a name or value
that isn’t UTF-8, Error::DuplicateField for a name given twice,
Error::UnsupportedVersion when v is present and isn’t 1 (a task without v, or
with an empty one, is version 1), Error::MissingField when another of the envelope’s
own fields is absent, Error::EmptyField when one of them but model_name is empty, and
Error::InvalidValue when task_id fails is_valid_task_id. Any non-empty source is
taken: only a producer keeps to is_valid_app, and only a fast reply key’s {app} is
checked against it. A model_type, model_id or task_name longer than
IDENTITY_MAX_LEN is taken too; a worker’s result carries UNKNOWN in its place.
Fields the envelope doesn’t name,
reserved names among them, are kept as they are, and so is a missing or empty v: a
Producer or FastCaller that sends the task on
writes v = 1.
use legit_streams::{Error, Limits, Task};
let raw = vec![(b"task_id".to_vec(), vec![0xff])];
assert!(matches!(Task::from_raw(&raw, &Limits::default()), Err(Error::InvalidUtf8 { .. })));Sourcepub fn from_fields(fields: Vec<(String, String)>) -> Result<Self>
pub fn from_fields(fields: Vec<(String, String)>) -> Result<Self>
Read a task from decoded fields, within the default Limits.
§Errors
The errors of Task::from_raw that apply to strings.
use legit_streams::{Error, Task};
let fields = vec![("v".to_owned(), "1".to_owned())];
assert_eq!(Task::from_fields(fields), Err(Error::MissingField("task_id")));Sourcepub fn task_id(&self) -> &str
pub fn task_id(&self) -> &str
The task id: {app}_{ULID} when this crate’s builder generated it.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").task_id("app_1").build()?;
assert_eq!(task.task_id(), "app_1");Sourcepub fn model_type(&self) -> &str
pub fn model_type(&self) -> &str
The type of the caller’s record, echoed in the result.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.model_type(), "post");Sourcepub fn model_id(&self) -> &str
pub fn model_id(&self) -> &str
The id of the caller’s record, echoed in the result.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.model_id(), "42");Sourcepub fn task_name(&self) -> &str
pub fn task_name(&self) -> &str
A free-form label for logs; default unless the producer set one.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.task_name(), "default");Sourcepub fn model_name(&self) -> &str
pub fn model_name(&self) -> &str
Which model the task asks for; empty means the worker’s default.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.model_name(), "");Sourcepub fn source(&self) -> &str
pub fn source(&self) -> &str
The producing application.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.source(), "app");Sourcepub fn reply_to(&self) -> Option<&str>
pub fn reply_to(&self) -> Option<&str>
Where the producer wants the result, when it named a stream. An empty reply_to is an
absent one.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").reply_to("results:app:hash").build()?;
assert_eq!(task.reply_to(), Some("results:app:hash"));
let mut fields = Task::builder("app", "post", "42").build()?.into_fields();
fields.push(("reply_to".to_owned(), String::new()));
assert_eq!(Task::from_fields(fields)?.reply_to(), None);Sourcepub fn get(&self, name: &str) -> Option<&str>
pub fn get(&self, name: &str) -> Option<&str>
The value of a field, the envelope’s own or the caller’s.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").field("prefix", "").build()?;
assert_eq!(task.get("prefix"), Some(""));
assert_eq!(task.get("embed_role"), None);
assert_eq!(task.get("v"), Some("1"));Sourcepub fn fields(&self) -> &[(String, String)]
pub fn fields(&self) -> &[(String, String)]
Every field, in order.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.fields()[6], ("v".to_owned(), "1".to_owned()));Sourcepub fn extra(&self) -> impl Iterator<Item = (&str, &str)>
pub fn extra(&self) -> impl Iterator<Item = (&str, &str)>
The fields that aren’t the envelope’s own: the payload and the optional settings, in order.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").reply_to("results:app:hash").field("text_data", "Hi").build()?;
let extra: Vec<_> = task.extra().collect();
assert_eq!(extra, [("text_data", "Hi")]);Sourcepub fn into_fields(self) -> Vec<(String, String)>
pub fn into_fields(self) -> Vec<(String, String)>
The fields, in order, consuming the task.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").build()?;
assert_eq!(task.into_fields().len(), 7);