pragma_common/
instrument_type.rs1use std::{fmt::Display, str::FromStr};
2
3#[derive(Debug, thiserror::Error)]
4pub enum InstrumentTypeError {
5 #[error("Unknown instrument_type")]
6 Unknown,
7}
8
9#[derive(Debug, PartialEq, Eq, Hash, Clone, Copy)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize,))]
11#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
12pub enum InstrumentType {
13 Spot,
14 Perp,
15}
16
17impl InstrumentType {
18 pub const ALL: [Self; 2] = [Self::Spot, Self::Perp];
19
20 pub const fn to_id(&self) -> i32 {
21 match self {
22 Self::Spot => 1,
23 Self::Perp => 2,
24 }
25 }
26
27 pub const fn is_spot(&self) -> bool {
28 match self {
29 Self::Spot => true,
30 Self::Perp => false,
31 }
32 }
33
34 pub const fn is_perp(&self) -> bool {
35 match self {
36 Self::Spot => false,
37 Self::Perp => true,
38 }
39 }
40}
41
42impl Display for InstrumentType {
43 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44 match self {
45 Self::Spot => write!(f, "spot"),
46 Self::Perp => write!(f, "perp"),
47 }
48 }
49}
50
51impl TryFrom<i32> for InstrumentType {
52 type Error = InstrumentTypeError;
53 fn try_from(value: i32) -> Result<Self, Self::Error> {
54 match value {
55 1 => Ok(Self::Spot),
56 2 => Ok(Self::Perp),
57 _ => Err(InstrumentTypeError::Unknown),
58 }
59 }
60}
61
62impl FromStr for InstrumentType {
63 type Err = InstrumentTypeError;
64
65 fn from_str(s: &str) -> Result<Self, Self::Err> {
66 match s {
67 "spot" => Ok(Self::Spot),
68 "perp" => Ok(Self::Perp),
69 _ => Err(InstrumentTypeError::Unknown),
70 }
71 }
72}