use async_trait::async_trait;
use longtrader_contract::ext::DecimalConvertError;
use rust_decimal::Decimal;
use tokio::sync::mpsc;
use crate::proto::{common, market, trading, worker};
#[inline]
#[must_use]
pub fn exchange_id(id: &str, label: &str) -> common::ExchangeId {
common::ExchangeId { id: id.to_string(), label: label.to_string(), ..Default::default() }
}
#[inline]
pub fn is_sequence_gap(prev: u64, next: u64) -> bool {
next != prev.wrapping_add(1)
}
#[inline]
pub fn has_sequence_gap(prev: Option<u64>, header: &common::EventHeader) -> bool {
if header.sequence == 0 {
return false;
}
prev.is_some_and(|p| is_sequence_gap(p, header.sequence))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OverflowPolicy {
DropOldest,
Coalesce,
Block,
}
pub const OVERFLOW_TICKER: OverflowPolicy = OverflowPolicy::DropOldest;
pub const OVERFLOW_ORDERBOOK: OverflowPolicy = OverflowPolicy::Coalesce;
pub const OVERFLOW_ORDERS: OverflowPolicy = OverflowPolicy::Block;
pub const OVERFLOW_TRADES: OverflowPolicy = OverflowPolicy::DropOldest;
pub const OVERFLOW_OHLCV: OverflowPolicy = OverflowPolicy::DropOldest;
pub const OVERFLOW_BALANCES: OverflowPolicy = OverflowPolicy::Block;
pub const OVERFLOW_POSITIONS: OverflowPolicy = OverflowPolicy::Block;
pub const DEFAULT_PAGE_LIMIT: u32 = 100;
pub const MAX_PAGE_LIMIT: u32 = 1000;
#[inline]
#[must_use]
pub fn page_limit_or_default(limit: u32) -> u32 {
if limit == 0 { DEFAULT_PAGE_LIMIT } else { limit.min(MAX_PAGE_LIMIT) }
}
#[inline]
pub fn overflow_policy_for_channel(channel: market::StreamChannel) -> OverflowPolicy {
match channel {
market::StreamChannel::Orderbook => OVERFLOW_ORDERBOOK,
market::StreamChannel::Ticker |
market::StreamChannel::Trades |
market::StreamChannel::Ohlcv => OVERFLOW_TICKER,
_ => OVERFLOW_ORDERS,
}
}
#[derive(Debug, thiserror::Error)]
pub enum PortError {
#[error("transport error: {0}")]
Transport(String),
#[error("rpc error ({code}): {message}")]
Rpc { code: u32, message: String },
#[error("operation unsupported by this backend: {0}")]
Unsupported(String),
#[error("missing required field: {0}")]
MissingField(String),
#[error("invalid argument: {0}")]
InvalidArgument(String),
#[error("not found: {0}")]
NotFound(String),
#[error(transparent)]
Decimal(#[from] DecimalConvertError),
}
pub type MarketEventStream = mpsc::Receiver<market::MarketDataEvent>;
#[async_trait]
pub trait TradingGateway: Send + Sync {
async fn create_order(
&self,
req: trading::CreateOrderRequest,
) -> Result<trading::Order, PortError>;
async fn batch_create_orders(
&self,
req: trading::CreateOrdersRequest,
) -> Result<Vec<trading::Order>, PortError>;
async fn cancel_order(
&self,
req: trading::CancelOrderRequest,
) -> Result<trading::Order, PortError>;
async fn cancel_all_orders(
&self,
req: trading::CancelAllOrdersRequest,
) -> Result<Vec<trading::Order>, PortError>;
async fn fetch_open_orders(
&self,
req: trading::FetchOpenOrdersRequest,
) -> Result<Vec<trading::Order>, PortError>;
async fn get_account(
&self,
req: trading::GetAccountRequest,
) -> Result<trading::GetAccountResponse, PortError>;
async fn get_positions(
&self,
req: trading::GetPositionsRequest,
) -> Result<trading::GetPositionsResponse, PortError>;
async fn get_order_history(
&self,
req: trading::GetOrderHistoryRequest,
) -> Result<trading::GetOrderHistoryResponse, PortError>;
async fn get_closed_positions(
&self,
req: trading::GetClosedPositionsRequest,
) -> Result<trading::GetClosedPositionsResponse, PortError>;
async fn close_position(
&self,
req: trading::ClosePositionRequest,
) -> Result<trading::ClosePositionResponse, PortError>;
async fn close_all_positions(
&self,
req: trading::CloseAllPositionsRequest,
) -> Result<trading::CloseAllPositionsResponse, PortError>;
async fn modify_position(
&self,
req: trading::ModifyPositionRequest,
) -> Result<trading::ModifyPositionResponse, PortError>;
async fn sync_state(
&self,
exchange_id: &common::ExchangeId,
) -> Result<worker::ReconcileStateResponse, PortError>;
}
#[async_trait]
pub trait MarketDataSource: Send + Sync {
async fn fetch_ticker(
&self,
req: market::FetchTickerRequest,
) -> Result<market::Ticker, PortError>;
async fn fetch_order_book(
&self,
req: market::FetchOrderBookRequest,
) -> Result<market::OrderBook, PortError>;
async fn get_candles(
&self,
req: market::GetCandlesRequest,
) -> Result<market::GetCandlesResponse, PortError>;
async fn list_symbols(
&self,
req: market::ListSymbolsRequest,
) -> Result<market::ListSymbolsResponse, PortError>;
async fn search_symbols(
&self,
req: market::SearchSymbolsRequest,
) -> Result<market::SearchSymbolsResponse, PortError>;
async fn list_tickers(
&self,
req: market::ListTickersRequest,
) -> Result<market::ListTickersResponse, PortError>;
async fn subscribe_market_data(
&self,
req: market::StreamMarketDataRequest,
policy: OverflowPolicy,
) -> Result<MarketEventStream, PortError>;
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FundingRateSnapshot {
pub symbol: String,
pub rate: Decimal,
pub next_funding_time_ms: i64,
pub mark_price: Option<Decimal>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FundingRatePoint {
pub rate: Decimal,
pub time_ms: i64,
}
#[async_trait]
pub trait FundingRateSource: Send + Sync {
async fn fetch_funding_rate(
&self,
exchange_id: &common::ExchangeId,
symbol: &str,
) -> Result<FundingRateSnapshot, PortError>;
async fn fetch_funding_rate_history(
&self,
exchange_id: &common::ExchangeId,
symbol: &str,
limit: u32,
) -> Result<Vec<FundingRatePoint>, PortError>;
async fn list_funding_rates(
&self,
exchange_id: &common::ExchangeId,
symbols: &[String],
) -> Result<Vec<FundingRateSnapshot>, PortError>;
}
#[derive(Debug, Clone)]
pub struct TriggerOrderRequest {
pub client_order_id: String,
pub symbol: String,
pub is_buy: bool,
pub trigger_price: Decimal,
pub qty: Decimal,
pub reduce_only: bool,
}
#[async_trait]
pub trait TriggerOrderGateway: Send + Sync {
async fn create_trigger_order(
&self,
exchange_id: &common::ExchangeId,
req: TriggerOrderRequest,
) -> Result<TriggerOrder, PortError>;
async fn cancel_trigger_order(
&self,
exchange_id: &common::ExchangeId,
order_id: &str,
symbol: &str,
) -> Result<TriggerOrder, PortError>;
async fn list_trigger_orders(
&self,
exchange_id: &common::ExchangeId,
symbols: &[String],
) -> Result<Vec<TriggerOrder>, PortError>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum TriggerOrderStatus {
#[default]
Open,
Triggered,
Canceled,
Rejected,
}
impl std::fmt::Display for TriggerOrderStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl TriggerOrderStatus {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Open => "open",
Self::Triggered => "triggered",
Self::Canceled => "canceled",
Self::Rejected => "rejected",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TriggerOrder {
pub id: String,
pub client_order_id: String,
pub symbol: String,
pub is_buy: bool,
pub trigger_price: Decimal,
pub qty: Decimal,
pub reduce_only: bool,
pub status: TriggerOrderStatus,
pub created_at_ms: i64,
pub order_id: Option<String>,
pub triggered_at_ms: Option<i64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum VenueOpParamType {
#[default]
Unspecified,
String,
Int64,
Decimal,
Bool,
Enum,
List,
Map,
}
impl VenueOpParamType {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Unspecified => "PARAM_TYPE_UNSPECIFIED",
Self::String => "PARAM_TYPE_STRING",
Self::Int64 => "PARAM_TYPE_INT64",
Self::Decimal => "PARAM_TYPE_DECIMAL",
Self::Bool => "PARAM_TYPE_BOOL",
Self::Enum => "PARAM_TYPE_ENUM",
Self::List => "PARAM_TYPE_LIST",
Self::Map => "PARAM_TYPE_MAP",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct VenueOpParam {
pub name: String,
pub r#type: VenueOpParamType,
pub required: bool,
pub doc: String,
pub enum_values: Vec<String>,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct VenueOpDescriptor {
pub name: String,
pub category: String,
pub summary: String,
pub mutating: bool,
pub params: Vec<VenueOpParam>,
}
#[async_trait]
pub trait VenueOpInvoker: Send + Sync {
async fn invoke_venue_op(
&self,
exchange_id: &common::ExchangeId,
op: &str,
params: serde_json::Map<String, serde_json::Value>,
) -> Result<serde_json::Value, PortError>;
async fn list_venue_ops(
&self,
exchange_id: &common::ExchangeId,
) -> Result<Vec<VenueOpDescriptor>, PortError>;
async fn describe_venue_op(
&self,
exchange_id: &common::ExchangeId,
op: &str,
) -> Result<VenueOpDescriptor, PortError> {
let ops = self.list_venue_ops(exchange_id).await?;
ops.into_iter()
.find(|o| o.name == op)
.ok_or_else(|| PortError::NotFound(format!("venue operation {op}")))
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LedgerEntry {
pub id: String,
pub currency: String,
pub amount: Decimal,
pub entry_type: String,
pub completed: bool,
pub time_ms: i64,
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct TransferReceipt {
pub transfer_id: String,
pub entry: Option<LedgerEntry>,
}
#[async_trait]
pub trait WalletGateway: Send + Sync {
async fn list_ledger_entries(
&self,
exchange_id: &common::ExchangeId,
currency: &str,
entry_type: &str,
limit: u32,
) -> Result<Vec<LedgerEntry>, PortError>;
async fn fetch_deposits(
&self,
exchange_id: &common::ExchangeId,
limit: u32,
) -> Result<Vec<LedgerEntry>, PortError> {
self.list_ledger_entries(exchange_id, "", "deposit", limit).await
}
#[allow(clippy::too_many_arguments)]
async fn transfer(
&self,
exchange_id: &common::ExchangeId,
asset: &str,
amount: Decimal,
dest_label: &str,
client_transfer_id: &str,
) -> Result<TransferReceipt, PortError>;
}
#[cfg(test)]
mod tests {
use async_trait::async_trait;
use longtrader_contract::ext::common_to_decimal;
use proptest::prelude::*;
use rust_decimal_macros::dec;
use super::*;
fn out_of_range(payload: &str) -> DecimalConvertError {
let wire = common::Decimal { value: payload.to_string(), ..Default::default() };
match common_to_decimal(&wire) {
Err(err) => err,
Ok(_) => unreachable!("{payload} must not decode"),
}
}
#[test]
fn consecutive_sequences_are_not_a_gap() {
assert!(!is_sequence_gap(0, 1));
assert!(!is_sequence_gap(1, 2));
assert!(!is_sequence_gap(u64::MAX - 1, u64::MAX));
}
#[test]
fn the_sequence_wraps_through_zero_without_a_gap() {
assert!(!is_sequence_gap(u64::MAX, 0), "u64::MAX -> 0 is contiguous");
}
#[test]
fn skipped_duplicate_and_regressed_sequences_are_gaps() {
assert!(is_sequence_gap(0, 0), "a duplicate is not an increment");
assert!(is_sequence_gap(0, 2), "a skip is a gap");
assert!(is_sequence_gap(5, 1), "a regression is a gap");
assert!(is_sequence_gap(1, 0), "one backwards is a gap");
}
#[test]
fn a_missing_watermark_is_never_a_gap() {
let header = common::EventHeader { sequence: 4_242, ..Default::default() };
assert!(!has_sequence_gap(None, &header), "the first event cannot gap");
}
#[test]
fn an_unset_header_sequence_is_never_a_gap() {
let header = common::EventHeader::default();
assert!(!has_sequence_gap(None, &header));
assert!(!has_sequence_gap(Some(0), &header));
assert!(!has_sequence_gap(Some(5), &header));
assert!(!has_sequence_gap(Some(u64::MAX), &header));
}
#[test]
fn has_sequence_gap_forwards_the_watermark() {
let header = common::EventHeader { sequence: 10, ..Default::default() };
assert!(!has_sequence_gap(Some(9), &header));
assert!(has_sequence_gap(Some(10), &header));
assert!(has_sequence_gap(Some(3), &header));
}
#[test]
fn orderbook_coalesces_ticker_drops_and_unknown_blocks() {
assert_eq!(
overflow_policy_for_channel(market::StreamChannel::Orderbook),
OverflowPolicy::Coalesce
);
for channel in [
market::StreamChannel::Ticker,
market::StreamChannel::Trades,
market::StreamChannel::Ohlcv,
] {
assert_eq!(
overflow_policy_for_channel(channel),
OverflowPolicy::DropOldest,
"{channel:?} must drop the oldest"
);
}
assert_eq!(
overflow_policy_for_channel(market::StreamChannel::Unspecified),
OverflowPolicy::Block,
"Unspecified must fail closed to Block"
);
}
#[test]
fn the_documented_policy_constants_are_stable() {
assert_eq!(OVERFLOW_TICKER, OverflowPolicy::DropOldest);
assert_eq!(OVERFLOW_ORDERBOOK, OverflowPolicy::Coalesce);
assert_eq!(OVERFLOW_ORDERS, OverflowPolicy::Block);
assert_eq!(OVERFLOW_TRADES, OverflowPolicy::DropOldest);
assert_eq!(OVERFLOW_OHLCV, OverflowPolicy::DropOldest);
assert_eq!(OVERFLOW_BALANCES, OverflowPolicy::Block);
assert_eq!(OVERFLOW_POSITIONS, OverflowPolicy::Block);
}
#[test]
fn every_port_error_renders_a_distinct_message() {
let errors = [
PortError::Transport("socket closed".into()),
PortError::Rpc { code: 5, message: "not found".into() },
PortError::Unsupported("trigger orders".into()),
PortError::MissingField("last".into()),
PortError::InvalidArgument("qty must be positive".into()),
PortError::NotFound("venue operation nope".into()),
];
let rendered: Vec<String> = errors.iter().map(ToString::to_string).collect();
for pair in rendered.iter().zip(rendered.iter().skip(1)) {
assert_ne!(pair.0, pair.1, "two errors share the message {}", pair.0);
}
for message in &rendered {
assert!(!message.is_empty(), "an error rendered as an empty string");
}
assert!(rendered.iter().any(|m| m.contains('5') && m.contains("not found")));
}
#[test]
fn not_found_and_unsupported_are_distinguishable_in_the_message() {
let not_found = PortError::NotFound("thing".into()).to_string();
let unsupported = PortError::Unsupported("thing".into()).to_string();
assert_ne!(not_found, unsupported);
assert!(not_found.contains("not found"));
assert!(unsupported.contains("unsupported"));
}
#[test]
fn a_decimal_convert_error_converts_into_a_port_error() {
let payload = "9".repeat(29);
let err: PortError = out_of_range(&payload).into();
assert!(matches!(err, PortError::Decimal(_)));
assert!(err.to_string().contains(&payload), "the payload must be echoed: {err}");
}
#[test]
fn trigger_order_status_wire_names() {
assert_eq!(TriggerOrderStatus::Open.as_str(), "open");
assert_eq!(TriggerOrderStatus::Triggered.as_str(), "triggered");
assert_eq!(TriggerOrderStatus::Canceled.as_str(), "canceled");
assert_eq!(TriggerOrderStatus::Rejected.as_str(), "rejected");
}
#[test]
fn trigger_order_status_display_matches_as_str() {
for status in [
TriggerOrderStatus::Open,
TriggerOrderStatus::Triggered,
TriggerOrderStatus::Canceled,
TriggerOrderStatus::Rejected,
] {
assert_eq!(status.to_string(), status.as_str(), "{status:?}");
}
}
#[test]
fn trigger_order_status_defaults_to_open() {
assert_eq!(TriggerOrderStatus::default(), TriggerOrderStatus::Open);
}
#[test]
fn venue_op_param_type_wire_names() {
assert_eq!(VenueOpParamType::Unspecified.as_str(), "PARAM_TYPE_UNSPECIFIED");
assert_eq!(VenueOpParamType::String.as_str(), "PARAM_TYPE_STRING");
assert_eq!(VenueOpParamType::Int64.as_str(), "PARAM_TYPE_INT64");
assert_eq!(VenueOpParamType::Decimal.as_str(), "PARAM_TYPE_DECIMAL");
assert_eq!(VenueOpParamType::Bool.as_str(), "PARAM_TYPE_BOOL");
assert_eq!(VenueOpParamType::Enum.as_str(), "PARAM_TYPE_ENUM");
assert_eq!(VenueOpParamType::List.as_str(), "PARAM_TYPE_LIST");
assert_eq!(VenueOpParamType::Map.as_str(), "PARAM_TYPE_MAP");
}
#[test]
fn venue_op_param_type_defaults_to_unspecified() {
assert_eq!(VenueOpParamType::default(), VenueOpParamType::Unspecified);
}
#[test]
fn every_param_type_name_is_unique() {
let all = [
VenueOpParamType::Unspecified,
VenueOpParamType::String,
VenueOpParamType::Int64,
VenueOpParamType::Decimal,
VenueOpParamType::Bool,
VenueOpParamType::Enum,
VenueOpParamType::List,
VenueOpParamType::Map,
];
let mut names: Vec<&str> = all.iter().map(|t| t.as_str()).collect();
names.sort_unstable();
let count = names.len();
names.dedup();
assert_eq!(names.len(), count, "two param types share a wire name");
}
struct StubOps {
ops: Vec<VenueOpDescriptor>,
}
#[async_trait]
impl VenueOpInvoker for StubOps {
async fn invoke_venue_op(
&self,
_exchange_id: &common::ExchangeId,
_op: &str,
_params: serde_json::Map<String, serde_json::Value>,
) -> Result<serde_json::Value, PortError> {
Err(PortError::Unsupported("invoke".into()))
}
async fn list_venue_ops(
&self,
_exchange_id: &common::ExchangeId,
) -> Result<Vec<VenueOpDescriptor>, PortError> {
Ok(self.ops.clone())
}
}
fn op(name: &str) -> VenueOpDescriptor {
VenueOpDescriptor {
name: name.into(),
category: "account".into(),
summary: String::new(),
mutating: false,
params: Vec::new(),
}
}
#[tokio::test]
async fn the_default_describe_venue_op_finds_a_known_operation() {
let stub = StubOps { ops: vec![op("account.balance"), op("account.transfer")] };
let exchange = common::ExchangeId { id: "mock".into(), ..Default::default() };
let found = stub.describe_venue_op(&exchange, "account.transfer").await.expect("found");
assert_eq!(found.name, "account.transfer");
}
#[tokio::test]
async fn the_default_describe_venue_op_reports_not_found() {
let stub = StubOps { ops: vec![op("account.balance")] };
let exchange = common::ExchangeId { id: "mock".into(), ..Default::default() };
let err = stub.describe_venue_op(&exchange, "nope").await.expect_err("must not resolve");
assert!(matches!(err, PortError::NotFound(_)), "got {err:?}");
assert!(err.to_string().contains("nope"), "the message must name the op: {err}");
}
#[tokio::test]
async fn the_default_describe_venue_op_on_an_empty_registry_is_not_found() {
let stub = StubOps { ops: Vec::new() };
let exchange = common::ExchangeId { id: "mock".into(), ..Default::default() };
let err = stub.describe_venue_op(&exchange, "account.balance").await.expect_err("empty");
assert!(matches!(err, PortError::NotFound(_)), "got {err:?}");
}
#[tokio::test]
async fn the_default_fetch_deposits_forwards_the_deposit_filter() {
struct RecordingWallet {
seen: std::sync::Mutex<Option<(String, String, u32)>>,
}
#[async_trait]
impl WalletGateway for RecordingWallet {
async fn list_ledger_entries(
&self,
_exchange_id: &common::ExchangeId,
currency: &str,
entry_type: &str,
limit: u32,
) -> Result<Vec<LedgerEntry>, PortError> {
*self.seen.lock().expect("wallet mutex") =
Some((currency.to_string(), entry_type.to_string(), limit));
Ok(Vec::new())
}
async fn transfer(
&self,
_exchange_id: &common::ExchangeId,
_asset: &str,
_amount: Decimal,
_dest_label: &str,
_client_transfer_id: &str,
) -> Result<TransferReceipt, PortError> {
Err(PortError::Unsupported("transfer".into()))
}
}
let wallet = RecordingWallet { seen: std::sync::Mutex::new(None) };
let exchange = common::ExchangeId { id: "mock".into(), ..Default::default() };
let rows = wallet.fetch_deposits(&exchange, 42).await.expect("deposits");
assert!(rows.is_empty());
let seen = wallet.seen.lock().expect("wallet mutex").clone();
assert_eq!(
seen,
Some((String::new(), "deposit".to_string(), 42)),
"the default must forward an empty currency filter and the deposit type"
);
}
#[test]
fn ledger_entries_keep_the_direction_in_the_sign() {
let deposit = LedgerEntry {
id: "d1".into(),
currency: "USDT".into(),
amount: dec!(100),
entry_type: "deposit".into(),
completed: true,
time_ms: 0,
};
let withdrawal = LedgerEntry { amount: dec!(-100), ..deposit.clone() };
assert!(deposit.amount.is_sign_positive());
assert!(withdrawal.amount.is_sign_negative());
assert_ne!(deposit, withdrawal);
}
#[test]
fn unknown_channels_fail_closed_to_block() {
assert_eq!(
overflow_policy_for_channel(market::StreamChannel::Unspecified),
OverflowPolicy::Block
);
}
proptest! {
#![proptest_config(ProptestConfig::with_cases(256))]
#[test]
fn a_sequence_is_gap_free_exactly_when_it_wraps_to_prev_plus_one(
prev in any::<u64>(),
delta in 0u64..8,
) {
let next = prev.wrapping_add(delta);
prop_assert_eq!(is_sequence_gap(prev, next), delta != 1, "prev={} delta={}", prev, delta );
}
#[test]
fn incrementing_then_checking_is_never_a_gap(prev in any::<u64>()) {
let next = prev.wrapping_add(1);
let header = common::EventHeader { sequence: next, ..Default::default() };
prop_assert!(!is_sequence_gap(prev, next));
prop_assert!(!has_sequence_gap(Some(prev), &header));
}
#[test]
fn repeating_a_sequence_is_always_a_gap(sequence in any::<u64>()) {
prop_assume!(sequence != 0);
let header = common::EventHeader { sequence, ..Default::default() };
prop_assert!(is_sequence_gap(sequence, sequence));
prop_assert!(has_sequence_gap(Some(sequence), &header));
}
#[test]
fn only_the_orderbook_channel_coalesces(
channel in prop::sample::select(&[
market::StreamChannel::Unspecified,
market::StreamChannel::Ticker,
market::StreamChannel::Orderbook,
market::StreamChannel::Trades,
market::StreamChannel::Ohlcv,
]),
) {
let policy = overflow_policy_for_channel(channel);
prop_assert_eq!(
policy == OverflowPolicy::Coalesce,
channel == market::StreamChannel::Orderbook, "{:?} mapped to {:?}", channel, policy
);
}
#[test]
fn trigger_order_status_names_are_stable(idx in 0u8..4) {
let status = match idx {
0 => TriggerOrderStatus::Open,
1 => TriggerOrderStatus::Triggered,
2 => TriggerOrderStatus::Canceled,
_ => TriggerOrderStatus::Rejected,
};
let name = status.as_str();
prop_assert!(!name.is_empty());
prop_assert_eq!(name.to_lowercase(), name);
prop_assert_eq!(status.to_string(), name);
}
#[test]
fn an_out_of_range_decimal_always_converts(digits in 29usize..40) {
let payload = "9".repeat(digits);
let err: PortError = out_of_range(&payload).into();
prop_assert!(matches!(err, PortError::Decimal(_)));
prop_assert!(err.to_string().contains(&payload), "{err}");
}
}
}