value-trait 0.1.10

Traits to deal with JSONesque values
Documentation
use crate::*;
use serde_yaml::{Mapping, Number, Value as YamlValue};
use std::borrow::Borrow;
use std::hash::Hash;

impl Object for Mapping<String, YamlValue> {
    type Key = String;
    type Element = YamlValue;
    fn get<Q: ?Sized>(&self, k: &Q) -> Option<&Self::Element>
    where
        Self::Key: Borrow<Q> + Hash + Eq,
        Q: Hash + Eq + Ord,
    {
        Map::get(self, k)
    }
    fn get_mut<Q: ?Sized>(&mut self, k: &Q) -> Option<&mut Self::Element>
    where
        Self::Key: Borrow<Q> + Hash + Eq,
        Q: Hash + Eq + Ord,
    {
        Map::get_mut(self, k)
    }
    fn insert<K, V>(&mut self, k: K, v: V) -> Option<Self::Element>
    where
        K: Into<Self::Key>,
        V: Into<Self::Element>,
        Self::Key: Hash + Eq,
    {
        Map::insert(self, k.into(), v.into())
    }
    fn remove<Q: ?Sized>(&mut self, k: &Q) -> Option<Self::Element>
    where
        Self::Key: Borrow<Q>,
        Q: Hash + Eq + Ord,
    {
        Map::remove(self, k)
    }
    fn iter<'i>(&'i self) -> Box<dyn Iterator<Item = (&Self::Key, &Self::Element)> + 'i> {
        Box::new(Map::iter(self))
    }
}

impl Value for YamlValue {
    type Key = String;
    type Array = Vec<YamlValue>;
    type Object = Map<<Self as Value>::Key, Self>;
    fn value_type(&self) -> ValueType {
        unimplemented!()
    }
    fn is_null(&self) -> bool {
        self == &YamlValue::Null
    }
    fn as_bool(&self) -> Option<bool> {
        match self {
            YamlValue::Bool(b) => Some(*b),
            _ => None,
        }
    }
    fn as_i64(&self) -> Option<i64> {
        match self {
            YamlValue::Number(n) => n.as_i64(),
            _ => None,
        }
    }
    fn as_u64(&self) -> Option<u64> {
        match self {
            YamlValue::Number(n) => n.as_u64(),
            _ => None,
        }
    }
    fn as_f64(&self) -> Option<f64> {
        match self {
            YamlValue::Number(n) => n.as_f64(),
            _ => None,
        }
    }
    fn as_str(&self) -> Option<&str> {
        match self {
            YamlValue::String(s) => Some(&s),
            _ => None,
        }
    }
    fn as_array(&self) -> Option<&<Self as Value>::Array> {
        match self {
            YamlValue::Array(a) => Some(a),
            _ => None,
        }
    }
    fn as_object(&self) -> Option<&<Self as Value>::Object> {
        match self {
            YamlValue::Object(o) => Some(o),
            _ => None,
        }
    }
}

impl Mutable for YamlValue {
    fn as_array_mut(&mut self) -> Option<&mut <Self as Value>::Array> {
        match self {
            YamlValue::Array(a) => Some(a),
            _ => None,
        }
    }
    fn as_object_mut(&mut self) -> Option<&mut <Self as Value>::Object> {
        match self {
            YamlValue::Object(o) => Some(o),
            _ => None,
        }
    }
}

impl From<StaticNode> for YamlValue {
    fn from(n: StaticNode) -> Self {
        match n {
            StaticNode::Bool(b) => YamlValue::Bool(b),
            StaticNode::F64(f) => {
                if let Some(f) = Number::from_f64(f) {
                    YamlValue::Number(f)
                } else {
                    YamlValue::Null
                }
            }
            StaticNode::Null => YamlValue::Null,
            StaticNode::I64(i) => YamlValue::Number(Number::from(i)),
            StaticNode::U64(u) => YamlValue::Number(Number::from(u)),
        }
    }
}
impl<'input> Builder<'input> for YamlValue {
    fn array_with_capacity(capacity: usize) -> Self {
        YamlValue::Array(Vec::with_capacity(capacity))
    }
    fn object_with_capacity(capacity: usize) -> Self {
        YamlValue::Object(Map::with_capacity(capacity))
    }
    fn null() -> Self {
        YamlValue::Null
    }
}