qlib_rs/data/
value.rs

1use crate::{data::{Timestamp}, EntityId};
2use serde::{Deserialize, Serialize};
3
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
6pub enum Value {
7    BinaryFile(Vec<u8>),
8    Bool(bool),
9    Choice(i64),
10    EntityList(Vec<EntityId>),
11    EntityReference(Option<EntityId>),
12    Float(f64),
13    Int(i64),
14    String(String),
15    Timestamp(Timestamp),
16}
17
18impl Value {
19    pub fn is_bool(&self) -> bool {
20        matches!(self, Value::Bool(_))
21    }
22
23    pub fn is_int(&self) -> bool {
24        matches!(self, Value::Int(_))
25    }
26
27    pub fn is_float(&self) -> bool {
28        matches!(self, Value::Float(_))
29    }
30
31    pub fn is_string(&self) -> bool {
32        matches!(self, Value::String(_))
33    }
34
35    pub fn is_binary_file(&self) -> bool {
36        matches!(self, Value::BinaryFile(_))
37    }
38
39    pub fn is_entity_reference(&self) -> bool {
40        matches!(self, Value::EntityReference(_))
41    }
42
43    pub fn is_entity_list(&self) -> bool {
44        matches!(self, Value::EntityList(_))
45    }
46
47    pub fn is_choice(&self) -> bool {
48        matches!(self, Value::Choice(_))
49    }
50
51    pub fn as_bool(&self) -> Option<bool> {
52        if let Value::Bool(b) = self {
53            Some(*b)
54        } else {
55            None
56        }
57    }
58
59    pub fn as_int(&self) -> Option<i64> {
60        if let Value::Int(i) = self {
61            Some(*i)
62        } else {
63            None
64        }
65    }
66
67    pub fn as_float(&self) -> Option<f64> {
68        if let Value::Float(f) = self {
69            Some(*f)
70        } else {
71            None
72        }
73    }
74
75    pub fn as_string(&self) -> Option<&String> {
76        if let Value::String(s) = self {
77            Some(s)
78        } else {
79            None
80        }
81    }
82
83    pub fn as_binary_file(&self) -> Option<&Vec<u8>> {
84        if let Value::BinaryFile(b) = self {
85            Some(b)
86        } else {
87            None
88        }
89    }
90
91    pub fn as_entity_reference(&self) -> Option<&Option<EntityId>> {
92        if let Value::EntityReference(e) = self {
93            Some(e)
94        } else {
95            None
96        }
97    }
98
99    pub fn as_entity_list(&self) -> Option<&Vec<EntityId>> {
100        if let Value::EntityList(e) = self {
101            Some(e)
102        } else {
103            None
104        }
105    }
106
107    pub fn as_choice(&self) -> Option<i64> {
108        if let Value::Choice(c) = self {
109            Some(*c)
110        } else {
111            None
112        }
113    }
114
115    pub fn as_timestamp(&self) -> Option<Timestamp> {
116        if let Value::Timestamp(t) = self {
117            Some(*t)
118        } else {
119            None
120        }
121    }
122
123    pub fn from_bool(b: bool) -> Self {
124        Value::Bool(b)
125    }
126
127    pub fn from_int(i: i64) -> Self {
128        Value::Int(i)
129    }
130
131    pub fn from_float(f: f64) -> Self {
132        Value::Float(f)
133    }
134
135    pub fn from_string(s: String) -> Self {
136        Value::String(s)
137    }
138
139    pub fn from_binary_file(b: Vec<u8>) -> Self {
140        Value::BinaryFile(b)
141    }
142
143    pub fn from_entity_reference(e: Option<EntityId>) -> Self {
144        Value::EntityReference(e)
145    }
146
147    pub fn from_entity_list(e: Vec<EntityId>) -> Self {
148        Value::EntityList(e)
149    }
150
151    pub fn from_choice(c: i64) -> Self {
152        Value::Choice(c)
153    }
154
155    pub fn from_timestamp(t: Timestamp) -> Self {
156        Value::Timestamp(t)
157    }
158}
159
160impl Into<String> for Value {
161    fn into(self) -> String {
162        format!("{:?}", self)
163    }
164}