aboutsummaryrefslogtreecommitdiff
path: root/rust/src
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--rust/src/lib.rs51
1 files changed, 50 insertions, 1 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index 10ca18e..c54b694 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -80,6 +80,16 @@ impl PartialEq<Vec<u8>> for Value {
80 } 80 }
81} 81}
82 82
83impl PartialEq<[u8]> for Value {
84 fn eq(&self, other: &[u8]) -> bool {
85 if let Value::ByteString(x) = self {
86 x == other
87 } else {
88 false
89 }
90 }
91}
92
83impl PartialEq<bool> for Value { 93impl PartialEq<bool> for Value {
84 fn eq(&self, other: &bool) -> bool { 94 fn eq(&self, other: &bool) -> bool {
85 if let Value::Boolean(x) = self { 95 if let Value::Boolean(x) = self {
@@ -167,6 +177,38 @@ fn read_packed_int<R: Read>(r: &mut R) -> Result<i64, Error> {
167} 177}
168 178
169impl Value { 179impl Value {
180 pub fn new_int( value: i64 ) -> Value {
181 Value::Integer( value )
182 }
183
184 pub fn new_byte_string( data: &[u8] ) -> Value {
185 Value::ByteString( data.to_vec() )
186 }
187
188 pub fn new_bool( value: bool ) -> Value {
189 Value::Boolean( value )
190 }
191
192 pub fn new_list() -> Value {
193 Value::List( Vec::new() )
194 }
195
196 pub fn new_dict() -> Value {
197 Value::Dictionary(HashMap::new())
198 }
199
200 pub fn dict_from<const N: usize>( data: [(String,Value); N] ) -> Value {
201 Value::Dictionary(HashMap::from( data ))
202 }
203
204 pub fn new_float( value: f64 ) -> Value {
205 Value::Float( value )
206 }
207
208 pub fn new_null() -> Value {
209 Value::Null
210 }
211
170 pub fn is_null(&self) -> bool { 212 pub fn is_null(&self) -> bool {
171 matches!(self, Value::Null) 213 matches!(self, Value::Null)
172 } 214 }
@@ -531,11 +573,16 @@ impl From<bool> for Value {
531 } 573 }
532} 574}
533 575
576impl From<Vec<u8>> for Value {
577 fn from(v: Vec<u8>) -> Self {
578 Value::ByteString( v )
579 }
580}
581
534#[cfg(test)] 582#[cfg(test)]
535mod tests { 583mod tests {
536 use super::*; 584 use super::*;
537 use core::error::Error; 585 use core::error::Error;
538 use std::fs::File;
539 use std::io::Cursor; 586 use std::io::Cursor;
540 587
541 #[test] 588 #[test]
@@ -546,6 +593,8 @@ mod tests {
546 assert!(v == 987.654); 593 assert!(v == 987.654);
547 let v = Value::from(true); 594 let v = Value::from(true);
548 assert!(v == true); 595 assert!(v == true);
596 let v = Value::from(b"hello".to_vec());
597 assert!(v == b"hello".to_vec());
549 } 598 }
550 599
551 #[test] 600 #[test]