Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Exchange example

Multi-schema exchange-shaped sample. It uses with_conversion only for Decimal, with no with_domain_type. publish = false. The Aeron calls it makes (connect, offer, poll, close) one by one: rusteron-client.

Conversion style

with_conversion versus with_domain_type is with_conversion vs with_domain_type. This sample uses only with_conversion.

#![allow(unused)]
fn main() {
    use ergo_sbe::{ConversionSelector, GenerationConfig};
    // A — generic converter: one wire type, many app types
    let _cfg =
        GenerationConfig::new("msgs").with_conversion(ConversionSelector::named_type("Decimal"));
}

(From book/examples/conversion-config.rs. Full multi-schema build.rs: exchange-example.)

That emits generic methods. The app supplies TryFromSbe / TryToSbe:

// encode: app Decimal to wire
e.price_from(&d)?;

// decode: wire to app Decimal
let d: rust_decimal::Decimal = level.price_as()?;

You still have wire accessors (price_value / price_wire). You do not get a concrete price() -> rust_decimal::Decimal; that requires with_domain_type(..., "rust_decimal::Decimal") (see L3 order book).

SampleConfig
This cratewith_conversion only
L3 order bookwith_domain_type only
SBE feature tourboth (demo_conversion_only is conversion-only)

Run

cargo test --manifest-path samples/exchange-example/Cargo.toml
just run-sample           # its messages, live, in ergo-aeron
just run-sample archive   # the same, recorded by a Java Aeron Archive

What run-sample starts, its keys and what each mode needs: samples/README.md.

JSON labels from schema marks

The sample’s build.rs registers a hook that reads jsonValue marks off each enum’s validValue elements at generation time and emits a json_label() accessor. Enums whose validValues are all labelled get the method; partially labelled or unmarked enums are skipped, so the other two schemas generate byte-identical code.

// tests/json_label_test.rs
use exchange_example::<venue>_spot::AccountType; // the shipped venue schema's module

assert_eq!(AccountType::Spot.json_label(), "SPOT");
assert_eq!(AccountType::NullVal.json_label(), "UNKNOWN");

Worked example: extension XML

This sample is the worked example for Extension XML. Its build.rs handles the extension marks of the published venue schema it ships (the first file in schemas/, generated as the <venue>_spot module) through the generic API only, keyed on that schema’s own attribute names:

    // Extension marks through the generic API only, keyed on the venue
    // schema's own attribute names: labels from `json_label_hook`,
    // mantissa/exponent pairs from the `pairs` recipe (group mantissas with
    // message-level exponents included), and every mark (`jsonPath`,
    // `jsonDefaultValue`, `jsonOmitNull`, ...) as a const. The other two
    // sample schemas carry no marks, so their output does not change.
    let config = config
        .with_hook(json_label_hook)
        .with_try_hook(ext_recipes::pairs("exponent", "with_exponent"))
        .with_try_hook(json_hook)
        .with_extension_consts(true);

ext_recipes is ergo-sbe’s venue-neutral example-hook file (sbe/tests/common/ext_recipes.rs), included with #[path]. The other two sample schemas carry no marks, so their generated code does not change.

MarkGenerated
jsonValue on validValuesFilterType::MaxAsset.json_label() == "MAX_ASSET" (json_label_hook) and FilterType::MAX_ASSET_JSON_VALUE; as_str stays "MaxAsset"
exponent naming a field of the same blockmax_qty_with_exponent() -> (i64, i8), exactly what both accessors return
exponent naming a message field from a groupbid.price_with_exponent(&message) -> (i64, i8) on the depth response’s entries
jsonPath, jsonDefaultValue, jsonOmitNullMaxAssetFilterSchema::MAX_QTY_JSON_PATH == "limit", TPlusSellFilterSchema::END_TIME_JSON_DEFAULT_VALUE == "-1", the group-level ExchangeInfoResponseSchema::SORS_JSON_OMIT_NULL == "true"

The assertions live in tests/extension_fixture_test.rs. cargo run --example sbe_text decodes a filter message and prints the label, an exact mantissa × 10^exponent rendering and the asset text.

SBE responses back to the venue’s JSON

The venue’s REST API answers each request as JSON or, on request, as SBE. Its schema’s marks record how one maps to the other: jsonPath the JSON key ([] an array position, .. the group’s array is the parent value), exponent the field that scales a mantissa into a decimal string, jsonDefaultValue the JSON for an absent optional. A try-hook in build.rs (json_hook) reads those marks and the decoder accessors named in the hook context, and emits a write_json method on each covered response decoder, group decoder and entry decoder; an entry whose mantissa is scaled by a message-level exponent takes the enclosing decoders as arguments. src/sbe_json.rs formats values (exact decimals, integer arithmetic only) and dispatches on the template id.

# Recorded real responses render to JSON equal to the venue's own JSON
cargo test --manifest-path samples/exchange-example/Cargo.toml --test json_fixture_test
# Render a saved SBE response body
cargo run --manifest-path samples/exchange-example/Cargo.toml --example sbe_json -- <file.sbe>

tests/fixtures/ holds one SBE body and the JSON body of the same public market-data request per endpoint (order book, trades, aggregate trades, klines, average price, price and book tickers); the test compares them as JSON values. scripts/ has the live check that recorded them. The sample’s README section in samples/README.md records what each mark is for and what recording found.