use crate::{Side, TimeInForce, Timestamp};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct OrderFlags {
side: Side,
post_only: bool,
time_in_force: TimeInForce,
}
impl OrderFlags {
pub fn new(side: Side, post_only: bool, time_in_force: TimeInForce) -> Self {
Self {
side,
post_only,
time_in_force,
}
}
pub fn side(&self) -> Side {
self.side
}
pub fn post_only(&self) -> bool {
self.post_only
}
pub(crate) fn update_post_only(&mut self, new_post_only: bool) {
self.post_only = new_post_only;
}
pub fn time_in_force(&self) -> TimeInForce {
self.time_in_force
}
pub fn is_immediate(&self) -> bool {
self.time_in_force.is_immediate()
}
pub fn has_expiry(&self) -> bool {
self.time_in_force.has_expiry()
}
pub fn expires_at(&self) -> Option<Timestamp> {
self.time_in_force.expires_at()
}
pub fn is_expired(&self, timestamp: Timestamp) -> bool {
self.time_in_force.is_expired(timestamp)
}
pub(crate) fn update_time_in_force(&mut self, new_time_in_force: TimeInForce) {
self.time_in_force = new_time_in_force;
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{Side, TimeInForce};
fn create_order_flags() -> OrderFlags {
OrderFlags::new(Side::Buy, true, TimeInForce::Gtc)
}
#[test]
fn test_side() {
assert_eq!(create_order_flags().side(), Side::Buy);
}
#[test]
fn test_post_only() {
assert!(create_order_flags().post_only());
}
#[test]
fn test_time_in_force() {
let mut order = create_order_flags();
assert_eq!(order.time_in_force(), TimeInForce::Gtc);
assert!(!order.is_immediate());
assert!(!order.has_expiry());
assert_eq!(order.expires_at(), None);
assert!(!order.is_expired(Timestamp(1771180000)));
order.update_time_in_force(TimeInForce::Ioc);
assert!(order.is_immediate());
assert!(!order.has_expiry());
assert_eq!(order.expires_at(), None);
assert!(!order.is_expired(Timestamp(1771180000)));
order.update_time_in_force(TimeInForce::Fok);
assert!(order.is_immediate());
assert!(!order.has_expiry());
assert_eq!(order.expires_at(), None);
assert!(!order.is_expired(Timestamp(1771180000)));
order.update_time_in_force(TimeInForce::Gtd(Timestamp(1771180000 + 1000)));
assert!(!order.is_immediate());
assert!(order.has_expiry());
assert_eq!(order.expires_at(), Some(Timestamp(1771180000 + 1000)));
assert!(!order.is_expired(Timestamp(1771180000)));
assert!(order.is_expired(Timestamp(1771180000 + 1000)));
}
#[cfg(feature = "serde")]
#[test]
fn test_roundtrip_serialization() {
let order = create_order_flags();
let serialized = serde_json::to_string(&order).unwrap();
let deserialized: OrderFlags = serde_json::from_str(&serialized).unwrap();
assert_eq!(order, deserialized);
}
}