Skip to main content

v_common_onto/
resource.rs

1use crate::datatype::{DataType, Lang};
2use derivative::Derivative;
3
4#[derive(Debug, PartialEq, Clone)]
5
6pub enum Value {
7    Int(i64),
8    Str(String, Lang),
9    Uri(String),
10    Bool(bool),
11    Num(i64, i64),
12    Binary(Vec<u8>),
13    Datetime(i64),
14}
15
16#[derive(Derivative)]
17#[derivative(Debug, PartialEq)]
18pub struct Resource {
19    pub rtype: DataType,
20    pub value: Value,
21    #[derivative(PartialEq = "ignore")]
22    pub order: u16,
23}
24
25impl Resource {
26    pub fn new_bool(data: bool) -> Self {
27        Resource {
28            rtype: DataType::Boolean,
29            order: 0,
30            value: Value::Bool(data),
31        }
32    }
33
34    pub fn new_uri(data: &str) -> Self {
35        Resource {
36            rtype: DataType::Uri,
37            order: 0,
38            value: Value::Uri(data.to_owned()),
39        }
40    }
41
42    pub fn get_copy(&self) -> Self {
43        Resource {
44            rtype: self.rtype.clone(),
45            order: self.order,
46            value: self.value.clone(),
47        }
48    }
49
50    pub fn get_binary(&self) -> &[u8] {
51        if let Value::Binary(v) = &self.value {
52            v
53        } else {
54            &[]
55        }
56    }
57
58    pub fn get_str(&self) -> &str {
59        if let Value::Str(s, _) = &self.value {
60            &s
61        } else {
62            ""
63        }
64    }
65
66    pub fn get_uri(&self) -> &str {
67        if let Value::Uri(s) = &self.value {
68            &s
69        } else {
70            ""
71        }
72    }
73
74    pub fn get_lang(&self) -> Lang {
75        if let Value::Str(_, l) = &self.value {
76            l.clone()
77        } else {
78            Lang::NONE
79        }
80    }
81
82    pub fn get_int(&self) -> i64 {
83        if let Value::Int(t) = self.value {
84            t
85        } else {
86            0
87        }
88    }
89
90    pub fn get_datetime(&self) -> i64 {
91        if let Value::Datetime(t) = self.value {
92            t
93        } else {
94            0
95        }
96    }
97
98    pub fn get_bool(&self) -> bool {
99        if let Value::Bool(t) = self.value {
100            t
101        } else {
102            false
103        }
104    }
105
106    pub fn get_num(&self) -> (i64, i64) {
107        if let Value::Num(m, e) = self.value {
108            (m, e)
109        } else {
110            (0, 0)
111        }
112    }
113
114    pub fn get_float(&self) -> f64 {
115        if let Value::Num(m, e) = self.value {
116            m as f64 * 10.0_f64.powf(e as f64)
117        } else {
118            0.0
119        }
120    }
121}
122
123impl From<Value> for i64 {
124    fn from(v: Value) -> Self {
125        if let Value::Int(t) = v {
126            t
127        } else {
128            0
129        }
130    }
131}
132
133impl From<Value> for bool {
134    fn from(v: Value) -> Self {
135        if let Value::Bool(t) = v {
136            t
137        } else {
138            false
139        }
140    }
141}