codec
ModuleHow an envelope becomes the bytes a broker carries, and back.
Contents
use utopia_queue::codec::Compat;pub struct Compat<W: Codec = Json>Reads either encoding, writes one of them: the codec for a queue that changes formats. The reader is picked from the leading bytes (igbinary opens with a NUL, which no JSON document may contain).
use php_std::{Array, Value};
use utopia_queue::codec::{Codec, Compat, Igbinary, Json};
let value = Value::Array(Array::from_list(vec![Value::Int(1)]));
let reader = Compat::new(Json);
assert_eq!(reader.decode(&Igbinary.encode(&value)?)?, value);
assert_eq!(reader.decode(&Json.encode(&value)?)?, value);
assert_eq!(reader.content_type(), "application/json");use utopia_queue::codec::Igbinary;pub struct IgbinaryBinary envelopes in igbinary's format: smaller and faster to decode
than JSON. A broker configured with this codec alone cannot read what an
earlier release wrote; Compat makes the switch survivable.
use utopia_queue::codec::Json;pub struct JsonUndocumented.
use php_std::{Array, Value};
use utopia_queue::codec::{Codec, Json};
let mut envelope = Array::new();
envelope.set("path".into(), Value::from("a/b é"));
let bytes = Json.encode(&Value::Array(envelope))?;
assert_eq!(bytes, r#"{"path":"a\/b \u00e9"}"#.as_bytes());
assert_eq!(Json.decode(&bytes)?, Json.decode("{\"path\":\"a/b é\"}".as_bytes())?);use utopia_queue::codec::Codec;pub trait Codec: Send + SyncTurns a message envelope into bytes and back.
Both directions fail on bad input: a value that cannot be encoded, or bytes this codec did not write. A broker treats a decode failure as a poison message and parks it: unlike a cache it cannot shrug one off as a miss, because the bytes are the only copy of somebody's work.
Implement it to carry envelopes in another format.