Skip to main content

alpaca_data/options/
enums.rs

1use std::fmt::{self, Display, Formatter};
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
6pub enum Sort {
7    #[default]
8    Asc,
9    Desc,
10}
11
12impl Display for Sort {
13    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
14        formatter.write_str(match self {
15            Self::Asc => "asc",
16            Self::Desc => "desc",
17        })
18    }
19}
20
21#[derive(Clone, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
22#[serde(transparent)]
23pub struct TimeFrame(String);
24
25impl TimeFrame {
26    #[must_use]
27    pub fn min_1() -> Self {
28        Self::from("1Min")
29    }
30
31    #[must_use]
32    pub fn day_1() -> Self {
33        Self::from("1Day")
34    }
35
36    #[must_use]
37    pub fn as_str(&self) -> &str {
38        &self.0
39    }
40}
41
42impl Default for TimeFrame {
43    fn default() -> Self {
44        Self::min_1()
45    }
46}
47
48impl From<&str> for TimeFrame {
49    fn from(value: &str) -> Self {
50        Self(value.to_owned())
51    }
52}
53
54impl From<String> for TimeFrame {
55    fn from(value: String) -> Self {
56        Self(value)
57    }
58}
59
60impl Display for TimeFrame {
61    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
62        formatter.write_str(self.as_str())
63    }
64}
65
66#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
67pub enum ContractType {
68    #[default]
69    Call,
70    Put,
71}
72
73impl ContractType {
74    #[must_use]
75    pub fn as_str(&self) -> &'static str {
76        match self {
77            Self::Call => "call",
78            Self::Put => "put",
79        }
80    }
81}
82
83impl Display for ContractType {
84    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
85        formatter.write_str(self.as_str())
86    }
87}
88
89#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
90pub enum OptionsFeed {
91    #[default]
92    Opra,
93    Indicative,
94}
95
96impl OptionsFeed {
97    #[must_use]
98    pub fn as_str(&self) -> &'static str {
99        match self {
100            Self::Opra => "opra",
101            Self::Indicative => "indicative",
102        }
103    }
104}
105
106impl Display for OptionsFeed {
107    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
108        formatter.write_str(self.as_str())
109    }
110}
111
112#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
113pub enum TickType {
114    #[default]
115    Trade,
116    Quote,
117}
118
119impl TickType {
120    #[must_use]
121    pub fn as_str(&self) -> &'static str {
122        match self {
123            Self::Trade => "trade",
124            Self::Quote => "quote",
125        }
126    }
127}
128
129impl Display for TickType {
130    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
131        formatter.write_str(self.as_str())
132    }
133}