From 96d34cc09da69d7443fa05e3245f8b042f3cf24a Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 25 Nov 2025 10:24:55 -0800 Subject: Added more helper functions. --- rust/src/lib.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'rust/src/lib.rs') 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> for Value { } } +impl PartialEq<[u8]> for Value { + fn eq(&self, other: &[u8]) -> bool { + if let Value::ByteString(x) = self { + x == other + } else { + false + } + } +} + impl PartialEq for Value { fn eq(&self, other: &bool) -> bool { if let Value::Boolean(x) = self { @@ -167,6 +177,38 @@ fn read_packed_int(r: &mut R) -> Result { } impl Value { + pub fn new_int( value: i64 ) -> Value { + Value::Integer( value ) + } + + pub fn new_byte_string( data: &[u8] ) -> Value { + Value::ByteString( data.to_vec() ) + } + + pub fn new_bool( value: bool ) -> Value { + Value::Boolean( value ) + } + + pub fn new_list() -> Value { + Value::List( Vec::new() ) + } + + pub fn new_dict() -> Value { + Value::Dictionary(HashMap::new()) + } + + pub fn dict_from( data: [(String,Value); N] ) -> Value { + Value::Dictionary(HashMap::from( data )) + } + + pub fn new_float( value: f64 ) -> Value { + Value::Float( value ) + } + + pub fn new_null() -> Value { + Value::Null + } + pub fn is_null(&self) -> bool { matches!(self, Value::Null) } @@ -531,11 +573,16 @@ impl From for Value { } } +impl From> for Value { + fn from(v: Vec) -> Self { + Value::ByteString( v ) + } +} + #[cfg(test)] mod tests { use super::*; use core::error::Error; - use std::fs::File; use std::io::Cursor; #[test] @@ -546,6 +593,8 @@ mod tests { assert!(v == 987.654); let v = Value::from(true); assert!(v == true); + let v = Value::from(b"hello".to_vec()); + assert!(v == b"hello".to_vec()); } #[test] -- cgit v1.2.3