use crate::{error::RithmicError, types::ManualOrAutoEntry};
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
#[must_use = "a cancellation does nothing until passed to a plant handle"]
pub struct RithmicCancelOrder {
pub id: String,
pub manual_or_auto: ManualOrAutoEntry,
pub window_name: Option<String>,
}
impl RithmicCancelOrder {
pub fn new() -> Self {
Self::default()
}
pub fn id(mut self, id: impl Into<String>) -> Self {
self.id = id.into();
self
}
pub fn manual_or_auto(mut self, manual_or_auto: ManualOrAutoEntry) -> Self {
self.manual_or_auto = manual_or_auto;
self
}
pub fn window_name(mut self, window_name: impl Into<String>) -> Self {
self.window_name = Some(window_name.into());
self
}
pub fn validate(&self) -> Result<(), RithmicError> {
if self.id.is_empty() {
return Err(RithmicError::InvalidArgument(
"a cancel requires the basket_id of the order it cancels".to_string(),
));
}
Ok(())
}
pub fn build(self) -> Result<Self, RithmicError> {
self.validate()?;
Ok(self)
}
}
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
#[must_use = "a cancellation does nothing until passed to a plant handle"]
pub struct RithmicCancelAllOrders {
pub manual_or_auto: ManualOrAutoEntry,
}
impl RithmicCancelAllOrders {
pub fn new() -> Self {
Self::default()
}
pub fn manual_or_auto(mut self, manual_or_auto: ManualOrAutoEntry) -> Self {
self.manual_or_auto = manual_or_auto;
self
}
pub fn build(self) -> Result<Self, RithmicError> {
Ok(self)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_cancel_requires_the_basket_id() {
assert!(RithmicCancelOrder::new().build().is_err());
assert!(RithmicCancelOrder::new().id("123456").build().is_ok());
assert!(RithmicCancelAllOrders::new().build().is_ok());
}
}