pub struct TaskBuilder { /* private fields */ }Expand description
A task being built; TaskBuilder::build writes the fields in this order, which is the
crate’s (a reader takes the fields as a map):
task_id, model_type, model_id, task_name, model_name, source, v, reply_to when
set, then the extra fields in the order they were added.
use legit_streams::Task;
let task = Task::builder("app", "post", "42")
.task_id("app_01J0000000000000000000000A")
.model_name("example-model")
.field("embed_role", "query")
.field("text_data", "Where is it?")
.build()?;
let names: Vec<&str> = task.fields().iter().map(|(name, _)| name.as_str()).collect();
assert_eq!(
names,
["task_id", "model_type", "model_id", "task_name", "model_name", "source", "v", "embed_role", "text_data"]
);Implementations§
Source§impl TaskBuilder
impl TaskBuilder
Sourcepub fn task_id(self, task_id: impl Into<String>) -> Self
pub fn task_id(self, task_id: impl Into<String>) -> Self
Use this task id rather than generating one. An empty id is ignored, and one is generated.
An id set here is the caller’s to make unguessable: a fast-lane task’s id must carry at
least 64 random bits, as the generated {app}_{ULID} does.
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 task_name(self, task_name: impl Into<String>) -> Self
pub fn task_name(self, task_name: impl Into<String>) -> Self
Set the label for logs; default when unset.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").task_name("embed").build()?;
assert_eq!(task.task_name(), "embed");Sourcepub fn model_name(self, model_name: impl Into<String>) -> Self
pub fn model_name(self, model_name: impl Into<String>) -> Self
Ask for a model; empty, the default, means the worker’s default.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").model_name("example-model").build()?;
assert_eq!(task.model_name(), "example-model");Sourcepub fn reply_to(self, reply_to: impl Into<String>) -> Self
pub fn reply_to(self, reply_to: impl Into<String>) -> Self
Name the stream the result should go to, such as reply_to("app", "hash"). Without it the envelope has no reply_to field. An empty name is ignored.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").reply_to("").build()?;
assert_eq!(task.reply_to(), None);Sourcepub fn field(self, name: impl Into<String>, value: impl Into<String>) -> Self
pub fn field(self, name: impl Into<String>, value: impl Into<String>) -> Self
Add a field of the caller’s: the payload, such as text_data, or an optional setting,
such as embed_role. Fields keep the order they’re added in.
use legit_streams::Task;
let task = Task::builder("app", "post", "42").field("embed_role", "query").build()?;
assert_eq!(task.get("embed_role"), Some("query"));Sourcepub fn build(self) -> Result<Task>
pub fn build(self) -> Result<Task>
Build the task.
§Errors
Error::EmptyFieldfor an emptysource,model_type,model_idortask_name.Error::InvalidValuefor amodel_type,model_idortask_namelonger thanIDENTITY_MAX_LEN, asourcethat failsis_valid_app, atask_idthat failsis_valid_task_id, areply_tothat failsis_valid_reply_to, or an extra field whose name isn’t 1 toFIELD_NAME_MAX_LENlowercase ASCII letters, digits and_, not starting with a digit.Error::ReservedFieldfor an extra field named like one of the envelope’s own, or with a reserved name: one starting with_, or one ofRESERVED_FIELDS.Error::DuplicateFieldfor an extra field added twice.Error::TooLargefor a task over the defaultLimits.
use legit_streams::{Error, Task};
let built = Task::builder("app", "post", "42").field("task_id", "other").build();
assert_eq!(built, Err(Error::ReservedField("task_id".into())));
let built = Task::builder("app", "post", "42").field("_trace", "x").build();
assert_eq!(built, Err(Error::ReservedField("_trace".into())));
let built = Task::builder("App", "post", "42").build();
assert!(matches!(built, Err(Error::InvalidValue { .. })));