Skip to main content

Module wire_result

Module wire_result 

Source
Expand description

The wire encoding of a Result item: Ok(t) ⇄ {"ok": t}, Err(e) ⇄ {"err": e}.

A method that returns Result<T, E> (or streams Result items) sends each item as data — an err is the activation saying no, not the call failing — and this is its shape. Use it as a serde with module:

use plexus_client::wire_result;
use serde::{Deserialize, Serialize};

#[derive(Debug, PartialEq, Serialize, Deserialize)]
struct Wrapped(#[serde(with = "wire_result")] Result<u32, String>);

let ok = serde_json::to_value(Wrapped(Ok(7))).unwrap();
assert_eq!(ok, serde_json::json!({"ok": 7}));

let err: Wrapped = serde_json::from_value(serde_json::json!({"err": "no"})).unwrap();
assert_eq!(err, Wrapped(Err("no".into())));

or call serialize / deserialize directly with any serde (de)serializer — a serde_json::Value is one. from_value and to_value are those two for a data frame’s content; from_value is the item decoder a generated client hands MethodStream for a Result method.

Decoding accepts exactly an object with one key, ok or err; anything else (another key, both keys, no key, a non-object) is an error whose message names both keys.

Works inside adjacently-tagged enums (#[serde(tag = "method", content = "event")]) on a newtype variant — the shape of the derived {Ns}Event aggregate.

Functions§

deserialize
{"ok": t} → Ok(t), {"err": e} → Err(e); any other shape is an error naming both keys.
from_content
from_value from a data frame’s Content: straight from the wire’s text where the transport kept it (PERF.115-36). What a generated client decodes a Result method’s items with.
from_value
A data frame’s content → a Result item: {"ok": t} → Ok(t), {"err": e} → Err(e). As a Decoder, what a generated client decodes a Result method’s items with.
serialize
Ok(t) → {"ok": t}, Err(e) → {"err": e}.
to_value
A Result item → a data frame’s content: Ok(t) → {"ok": t}, Err(e) → {"err": e}. What a test scripts a Result method’s items with.