use crate::book::common::types::BookOrder;
use crate::types::OrderId;
pub const MIN_CLOSE_BATCH_EVENTS: u16 = 2;
pub const DEFAULT_CLOSE_BATCH_EVENTS: u16 = crate::config::DEFAULT_CLOSE_BATCH_MAX_EVENTS;
pub const MAX_CLOSE_BATCH_EVENTS: u16 = 4096;
pub const fn default_close_batch_events() -> u16 {
DEFAULT_CLOSE_BATCH_EVENTS
}
#[inline]
pub(crate) fn set_close_batch_max_events(target: &mut u16, batch_max_events: u16) {
*target = normalize_close_batch_events(batch_max_events);
}
#[inline]
pub fn is_valid_close_batch_events(batch_max_events: u16) -> bool {
(MIN_CLOSE_BATCH_EVENTS..=MAX_CLOSE_BATCH_EVENTS).contains(&batch_max_events)
}
#[inline]
pub fn normalize_close_batch_events(batch_max_events: u16) -> u16 {
batch_max_events.clamp(MIN_CLOSE_BATCH_EVENTS, MAX_CLOSE_BATCH_EVENTS)
}
pub fn close_start_reason(batch_max_events: u16) -> String {
debug_assert!(is_valid_close_batch_events(batch_max_events));
format!("CLOSE_START:{batch_max_events}")
}
pub fn parse_close_start_batch_max_events(reason: &str) -> Option<u16> {
reason
.strip_prefix("CLOSE_START:")
.and_then(|s| s.parse::<u16>().ok())
.filter(|batch_max_events| is_valid_close_batch_events(*batch_max_events))
}
#[inline]
pub fn is_live_order(o: &BookOrder) -> bool {
matches!(
o.info.state,
crate::book::common::types::BookOrderState::ExecutableUnmatched
| crate::book::common::types::BookOrderState::ExecutablePartiallyMatched
)
}
pub fn count_live_orders<'a>(orders: impl Iterator<Item = (OrderId, &'a BookOrder)>) -> u64 {
orders.filter(|(_, o)| is_live_order(o)).count() as u64
}