testdenali/
value.rs

1use super::{context::*, value_interpreter::*, value_raw::*};
2use num_bigint::BigUint;
3use num_traits::ToPrimitive;
4use std::{
5    cmp::{Ord, Ordering},
6    fmt,
7};
8
9pub trait InterpretableFrom<T> {
10    fn interpret_from(from: T, context: &InterpreterContext) -> Self;
11}
12
13#[derive(Clone, Debug)]
14pub struct BytesValue {
15    pub value: Vec<u8>,
16    pub original: ValueSubTree,
17}
18
19impl BytesValue {
20    pub fn empty() -> Self {
21        BytesValue {
22            value: Vec::new(),
23            original: ValueSubTree::Str(String::default()),
24        }
25    }
26}
27
28impl From<Vec<u8>> for BytesValue {
29    fn from(v: Vec<u8>) -> Self {
30        BytesValue {
31            value: v,
32            original: ValueSubTree::Str(String::default()),
33        }
34    }
35}
36
37impl InterpretableFrom<ValueSubTree> for BytesValue {
38    fn interpret_from(from: ValueSubTree, context: &InterpreterContext) -> Self {
39        BytesValue {
40            value: interpret_subtree(&from, context),
41            original: from,
42        }
43    }
44}
45
46impl Default for BytesValue {
47    fn default() -> Self {
48        Self {
49            value: Vec::new(),
50            original: ValueSubTree::Str(String::new()),
51        }
52    }
53}
54
55impl fmt::Display for BytesValue {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        self.original.fmt(f)
58    }
59}
60
61#[derive(Debug)]
62pub struct BigUintValue {
63    pub value: BigUint,
64    pub original: ValueSubTree,
65}
66
67impl InterpretableFrom<ValueSubTree> for BigUintValue {
68    fn interpret_from(from: ValueSubTree, context: &InterpreterContext) -> Self {
69        let bytes = interpret_subtree(&from, context);
70        BigUintValue {
71            value: BigUint::from_bytes_be(&bytes),
72            original: from,
73        }
74    }
75}
76
77impl fmt::Display for BigUintValue {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        self.original.fmt(f)
80    }
81}
82
83impl Default for BigUintValue {
84    fn default() -> Self {
85        BigUintValue {
86            original: ValueSubTree::default(),
87            value: BigUint::from(0u32),
88        }
89    }
90}
91
92#[derive(Debug, Default)]
93pub struct U64Value {
94    pub value: u64,
95    pub original: ValueSubTree,
96}
97
98impl InterpretableFrom<ValueSubTree> for U64Value {
99    fn interpret_from(from: ValueSubTree, context: &InterpreterContext) -> Self {
100        let bytes = interpret_subtree(&from, context);
101        let bu = BigUint::from_bytes_be(&bytes);
102        U64Value {
103            value: bu.to_u64().unwrap(),
104            original: from,
105        }
106    }
107}
108
109impl fmt::Display for U64Value {
110    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
111        self.original.fmt(f)
112    }
113}
114
115#[derive(Clone, Debug)]
116pub struct BytesKey {
117    pub value: Vec<u8>,
118    pub original: String,
119}
120
121impl From<Vec<u8>> for BytesKey {
122    fn from(v: Vec<u8>) -> Self {
123        BytesKey {
124            value: v,
125            original: String::default(),
126        }
127    }
128}
129
130impl PartialEq for BytesKey {
131    fn eq(&self, other: &Self) -> bool {
132        self.value == other.value
133    }
134}
135
136impl Eq for BytesKey {}
137
138impl PartialOrd for BytesKey {
139    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
140        self.value.partial_cmp(&other.value)
141    }
142}
143
144impl Ord for BytesKey {
145    fn cmp(&self, other: &Self) -> Ordering {
146        self.value.cmp(&other.value)
147    }
148}
149
150impl InterpretableFrom<String> for BytesKey {
151    fn interpret_from(from: String, context: &InterpreterContext) -> Self {
152        let bytes = interpret_string(&from, context);
153        BytesKey {
154            value: bytes,
155            original: from,
156        }
157    }
158}
159
160impl fmt::Display for BytesKey {
161    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
162        self.original.fmt(f)
163    }
164}