Skip to main content

slouch/types/
order_token.rs

1
2use nsdq_util::{
3    define_str,
4    types::string::helper::*,
5};
6
7use nsdq_util::error::TypeError;
8
9define_str!{
10    OrderToken [14usize] 
11        "Strong type for `ClOrdId` that ensures protocol compliance.
12        Can be used to differentiate strategies, etc.
13        Will not be checked for day-uniqueness for each OUCH account."
14}
15
16
17impl OrderToken {
18
19    /// Generate a new OrderToken from a protocol-compliant string.
20    /// May only contain ASCII alphanumeric characters and spaces.
21    /// Only up to the first 14 characters will be included, others ignored.
22    pub fn from(s: impl AsRef<str>) -> Result<Self, TypeError> {
23
24        let s = s.as_ref();
25
26        if !is_alphanumeric(s) {
27            return Err(
28                TypeError::InvalidString(
29                    "OrderToken".to_string(), 
30                    s.to_string()
31                )
32            );
33        }
34
35        Ok(OrderToken(fixed_str::<14usize>(s)))
36    }
37}
38