1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
use super::context::*;
use super::value_interpreter::*;
use super::value_raw::*;
use num_bigint::BigUint;
use num_traits::ToPrimitive;
use std::cmp::{Ord, Ordering};
use std::fmt;

pub trait InterpretableFrom<T> {
	fn interpret_from(from: T, context: &InterpreterContext) -> Self;
}

#[derive(Clone, Debug)]
pub struct BytesValue {
	pub value: Vec<u8>,
	pub original: ValueSubTree,
}

impl BytesValue {
	pub fn empty() -> Self {
		BytesValue {
			value: Vec::new(),
			original: ValueSubTree::Str(String::default()),
		}
	}
}

impl From<Vec<u8>> for BytesValue {
	fn from(v: Vec<u8>) -> Self {
		BytesValue {
			value: v,
			original: ValueSubTree::Str(String::default()),
		}
	}
}

impl InterpretableFrom<ValueSubTree> for BytesValue {
	fn interpret_from(from: ValueSubTree, context: &InterpreterContext) -> Self {
		BytesValue {
			value: interpret_subtree(&from, context),
			original: from,
		}
	}
}

impl fmt::Display for BytesValue {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		self.original.fmt(f)
	}
}

#[derive(Debug)]
pub struct BigUintValue {
	pub value: BigUint,
	pub original: ValueSubTree,
}

impl InterpretableFrom<ValueSubTree> for BigUintValue {
	fn interpret_from(from: ValueSubTree, context: &InterpreterContext) -> Self {
		let bytes = interpret_subtree(&from, context);
		BigUintValue {
			value: BigUint::from_bytes_be(&bytes),
			original: from,
		}
	}
}

impl fmt::Display for BigUintValue {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		self.original.fmt(f)
	}
}

impl Default for BigUintValue {
	fn default() -> Self {
		BigUintValue {
			original: ValueSubTree::default(),
			value: BigUint::from(0u32),
		}
	}
}

#[derive(Debug)]
pub struct U64Value {
	pub value: u64,
	pub original: ValueSubTree,
}

impl InterpretableFrom<ValueSubTree> for U64Value {
	fn interpret_from(from: ValueSubTree, context: &InterpreterContext) -> Self {
		let bytes = interpret_subtree(&from, context);
		let bu = BigUint::from_bytes_be(&bytes);
		U64Value {
			value: bu.to_u64().unwrap(),
			original: from,
		}
	}
}

impl fmt::Display for U64Value {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		self.original.fmt(f)
	}
}

#[derive(Clone, Debug)]
pub struct BytesKey {
	pub value: Vec<u8>,
	pub original: String,
}

impl From<Vec<u8>> for BytesKey {
	fn from(v: Vec<u8>) -> Self {
		BytesKey {
			value: v,
			original: String::default(),
		}
	}
}

impl PartialEq for BytesKey {
	fn eq(&self, other: &Self) -> bool {
		self.value == other.value
	}
}

impl Eq for BytesKey {}

impl PartialOrd for BytesKey {
	fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
		self.value.partial_cmp(&other.value)
	}
}

impl Ord for BytesKey {
	fn cmp(&self, other: &Self) -> Ordering {
		self.value.cmp(&other.value)
	}
}

impl InterpretableFrom<String> for BytesKey {
	fn interpret_from(from: String, context: &InterpreterContext) -> Self {
		let bytes = interpret_string(&from, context);
		BytesKey {
			value: bytes,
			original: from,
		}
	}
}

impl fmt::Display for BytesKey {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		self.original.fmt(f)
	}
}