openlimits_exchange/model/
time_in_force_visitor.rs

1use chrono::Duration;
2use serde::de::Visitor;
3use serde::de;
4use std::fmt;
5use super::TimeInForce;
6
7/// This struct uses the time in force enum
8pub struct TimeInForceVisitor;
9
10impl<'de> Visitor<'de> for TimeInForceVisitor {
11    type Value = TimeInForce;
12
13    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
14        formatter.write_str("an string, either GTC, IOC, FOK, GTT,duration")
15    }
16    fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
17    where
18        E: de::Error,
19    {
20        if v.starts_with("GTT,") {
21            match v[4..].parse::<u64>() {
22                Ok(v) => Ok(TimeInForce::GoodTillTime(Duration::milliseconds(v as i64))),
23                _ => Err(E::custom(format!("Invalid GTG: {}", v))),
24            }
25        } else {
26            match v {
27                "GTC" => Ok(TimeInForce::GoodTillCancelled),
28                "IOC" => Ok(TimeInForce::ImmediateOrCancelled),
29                "FOK" => Ok(TimeInForce::FillOrKill),
30                _ => Err(E::custom(format!("Invalid string: {}", v))),
31            }
32        }
33    }
34}