Skip to main content

CidCodec

Trait CidCodec 

Source
pub trait CidCodec: Send + Sync {
    // Required methods
    fn caps(&self) -> CidCaps;
    fn encode(&self, cid: ClientOrderId) -> Result<NativeCid, CidError>;
    fn decode(&self, native: &NativeCid) -> Option<ClientOrderId>;
}
Expand description

A pure, stateless mapping between ClientOrderId and the broker’s NativeCid.

Rules for an implementation:

  • encode is a pure function: the same cid always gives the same string, with no state and no network.
  • decode(encode(cid)) gives Some(cid) for every cid (lossless). A broker whose field cannot keep a UUID needs another carrier field, not a lossy codec.
  • decode gives None for a string that this codec did not make (for example the cid of an order placed from the broker’s app).

§Examples

use brokapi_core::{CidCodec, ClientOrderId, HexUuidCodec};

let codec = HexUuidCodec;
let cid = ClientOrderId::new_v4();
let native = codec.encode(cid).unwrap();
assert_eq!(native.as_str().len(), 32);
assert_eq!(codec.decode(&native), Some(cid));

Required Methods§

Source

fn caps(&self) -> CidCaps

What the broker’s field can hold. Checked by the connector at start.

Source

fn encode(&self, cid: ClientOrderId) -> Result<NativeCid, CidError>

The broker form of cid.

§Errors

CidError::TooLong or CidError::InvalidChar when the result does not fit the field described by caps. A correct codec never returns them for a UUID.

Source

fn decode(&self, native: &NativeCid) -> Option<ClientOrderId>

The cid behind a native string, or None if this codec did not make that string.

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§