Skip to main content

TaskResult

Struct TaskResult 

Source
pub struct TaskResult { /* private fields */ }
Expand description

A result envelope: the flat map of string fields a worker adds to a reply stream.

A result carries task_id and status, exactly success or error; an entry without them isn’t a result. A worker also writes model_type, model_id, task_name and model_name, echoed from the task, and on an error error, a message, and error_type, a code in lowercase snake case such as unsupported_model; it should write worker_id and processing_ms, who ran the task and for how long, and may write requested_model_name. A reader requires none of those: error types are an open set, and a reader treats an unknown or missing one as a failure that won’t pass on a retry. Every other field, such as embedding or result, is the output, which this crate keeps in order and never reads. A result parsed from a stream and written back gives the same fields in the same order.

Debug shows the identity and status fields and the names of the others, never their values.

use legit_streams::{Status, TaskResult};

let result = TaskResult::success("app_01J0000000000000000000000A")
    .worker_id("worker-1")
    .processing_ms(12)
    .field("result", "{}")
    .build()?;

assert_eq!(result.status(), Status::Success);
assert_eq!(result.processing_ms(), Some(12));
assert_eq!(TaskResult::from_fields(result.fields().to_vec())?, result);

Implementations§

Source§

impl TaskResult

Source

pub fn success(task_id: impl Into<String>) -> ResultBuilder

Start a success result for a task.

use legit_streams::TaskResult;

assert!(TaskResult::success("app_1").build()?.is_success());
Source

pub fn failure( task_id: impl Into<String>, message: impl Into<String>, ) -> ResultBuilder

Start an error result for a task, with the message for its error field.

A worker must set ResultBuilder::error_type on every error it writes, as this crate’s workers do. The builder still builds an error without one: a reader must accept such a result, from an older worker, and its tests need one to read.

use legit_streams::TaskResult;

let result = TaskResult::failure("app_1", "no such model").error_type("unsupported_model").build()?;
assert_eq!(result.error(), Some("no such model"));
assert_eq!(result.error_type(), Some("unsupported_model"));
Source

pub fn from_entry(entry: &Entry) -> Result<Self>

Read a result from a stream entry, within the default Limits.

§Errors

The errors of TaskResult::from_raw.

use legit_streams::{Entry, EntryId, TaskResult};

let entry = Entry::new(EntryId::ZERO, vec![(b"task_id".to_vec(), b"app_1".to_vec())]);
assert!(TaskResult::from_entry(&entry).is_err());
Source

pub fn from_raw(raw: &[(Vec<u8>, Vec<u8>)], limits: &Limits) -> Result<Self>

Read a result 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::MissingField without task_id or status, Error::EmptyField for an empty task_id, and Error::InvalidValue for a status other than exactly success or error. Nothing else is required: not error or error_type on an error, nor worker_id or processing_ms.

use legit_streams::{Error, Limits, TaskResult};

let raw = vec![(b"task_id".to_vec(), b"app_1".to_vec()), (b"status".to_vec(), b"done".to_vec())];
assert!(matches!(TaskResult::from_raw(&raw, &Limits::default()), Err(Error::InvalidValue { .. })));
Source

pub fn from_fields(fields: Vec<(String, String)>) -> Result<Self>

Read a result from decoded fields, within the default Limits.

§Errors

The errors of TaskResult::from_raw that apply to strings.

use legit_streams::{Error, TaskResult};

let fields = vec![("task_id".to_owned(), "app_1".to_owned()), ("status".to_owned(), "Error".to_owned())];
assert!(matches!(TaskResult::from_fields(fields), Err(Error::InvalidValue { .. })));
let fields = vec![("task_id".to_owned(), "app_1".to_owned()), ("status".to_owned(), "error".to_owned())];
assert_eq!(TaskResult::from_fields(fields)?.error_type(), None);
Source

pub fn task_id(&self) -> &str

The id of the task this is the result of.

use legit_streams::TaskResult;

assert_eq!(TaskResult::success("app_1").build()?.task_id(), "app_1");
Source

pub fn status(&self) -> Status

Whether the task succeeded.

use legit_streams::{Status, TaskResult};

assert_eq!(TaskResult::failure("app_1", "failed").build()?.status(), Status::Error);
Source

pub fn is_success(&self) -> bool

Whether the status is success.

use legit_streams::TaskResult;

assert!(!TaskResult::failure("app_1", "failed").build()?.is_success());
Source

pub fn error(&self) -> Option<&str>

The error message, on an error.

use legit_streams::TaskResult;

assert_eq!(TaskResult::success("app_1").build()?.error(), None);
Source

pub fn error_type(&self) -> Option<&str>

The error code, such as unsupported_model, when the worker gave one.

use legit_streams::TaskResult;

assert_eq!(TaskResult::failure("app_1", "failed").build()?.error_type(), None);
Source

pub fn model_type(&self) -> Option<&str>

The task’s model_type, echoed.

use legit_streams::TaskResult;

assert_eq!(TaskResult::success("app_1").model_type("post").build()?.model_type(), Some("post"));
Source

pub fn model_id(&self) -> Option<&str>

The task’s model_id, echoed.

use legit_streams::TaskResult;

assert_eq!(TaskResult::success("app_1").model_id("42").build()?.model_id(), Some("42"));
Source

pub fn task_name(&self) -> Option<&str>

The task’s task_name, echoed.

use legit_streams::TaskResult;

assert_eq!(TaskResult::success("app_1").build()?.task_name(), None);
Source

pub fn model_name(&self) -> Option<&str>

The model the worker names: the task’s model_name echoed, or the worker’s own name for what ran.

use legit_streams::TaskResult;

let result = TaskResult::success("app_1").model_name("example-model").build()?;
assert_eq!(result.model_name(), Some("example-model"));
Source

pub fn worker_id(&self) -> Option<&str>

The consumer name of the worker that ran the task.

use legit_streams::TaskResult;

assert_eq!(TaskResult::success("app_1").worker_id("w-1").build()?.worker_id(), Some("w-1"));
Source

pub fn processing_ms(&self) -> Option<u64>

How long the worker spent on the task, in milliseconds, from taking the entry to writing the result; None when the result doesn’t say, or says it with something other than a whole number.

use legit_streams::TaskResult;

assert_eq!(TaskResult::success("app_1").build()?.processing_ms(), None);
Source

pub fn get(&self, name: &str) -> Option<&str>

The value of a field, the envelope’s own or the worker’s output.

use legit_streams::TaskResult;

let result = TaskResult::success("app_1").field("embedding_format", "base64_fp32").build()?;
assert_eq!(result.get("embedding_format"), Some("base64_fp32"));
Source

pub fn fields(&self) -> &[(String, String)]

Every field, in order.

use legit_streams::TaskResult;

assert_eq!(TaskResult::success("app_1").build()?.fields().len(), 2);
Source

pub fn into_fields(self) -> Vec<(String, String)>

The fields, in order, consuming the result.

use legit_streams::TaskResult;

let fields = TaskResult::success("app_1").build()?.into_fields();
assert_eq!(fields[0], ("task_id".to_owned(), "app_1".to_owned()));

Trait Implementations§

Source§

impl Clone for TaskResult

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for TaskResult

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for TaskResult

Source§

impl PartialEq for TaskResult

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for TaskResult

Auto Trait Implementations§

§

impl Forget for TaskResult

§

impl Freeze for TaskResult

§

impl Move for TaskResult

§

impl RefUnwindSafe for TaskResult

§

impl Send for TaskResult
where RawVecInner: Send,

§

impl Sync for TaskResult
where RawVecInner: Sync,

§

impl Unpin for TaskResult
where *const u8 is !null: Unpin,

§

impl UnsafeUnpin for TaskResult

§

impl UnwindSafe for TaskResult
where RawVecInner: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more