aboutsummaryrefslogtreecommitdiff
path: root/rust/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--rust/src/lib.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/rust/src/lib.rs b/rust/src/lib.rs
index c54b694..a96454e 100644
--- a/rust/src/lib.rs
+++ b/rust/src/lib.rs
@@ -213,6 +213,36 @@ impl Value {
213 matches!(self, Value::Null) 213 matches!(self, Value::Null)
214 } 214 }
215 215
216 pub fn get(&self, key: &str) -> Option<&Value> {
217 if let Value::Dictionary(d) = self {
218 d.get( key )
219 } else {
220 None
221 }
222 }
223
224 pub fn as_bool( &self ) -> Result<bool,Error> {
225 if let Value::Boolean(d) = self {
226 Ok(*d)
227 } else {
228 Err(Error::new(
229 ErrorKind::InvalidData,
230 "as_bool called on non boolean value.",
231 ))
232 }
233 }
234
235 pub fn as_byte_string( &self ) -> Result<&Vec<u8>,Error> {
236 if let Value::ByteString(d) = self {
237 Ok(&d)
238 } else {
239 Err(Error::new(
240 ErrorKind::InvalidData,
241 "as_byte_string called on non ByteString value.",
242 ))
243 }
244 }
245
216 pub fn write<W: Write>(&self, w: &mut W) -> Result<(), Error> { 246 pub fn write<W: Write>(&self, w: &mut W) -> Result<(), Error> {
217 match self { 247 match self {
218 Self::Integer(x) => { 248 Self::Integer(x) => {
@@ -579,6 +609,12 @@ impl From<Vec<u8>> for Value {
579 } 609 }
580} 610}
581 611
612impl From<&[u8]> for Value {
613 fn from(v: &[u8]) -> Self {
614 Value::ByteString( Vec::from(v) )
615 }
616}
617
582#[cfg(test)] 618#[cfg(test)]
583mod tests { 619mod tests {
584 use super::*; 620 use super::*;