use std::collections::BTreeMap;
use std::fmt;
use crate::core::to_sml;
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
Null,
Bool(bool),
Int(i64),
Float(f64),
Str(String),
Array(Vec<Value>),
Object(BTreeMap<String, Value>),
}
impl Value {
pub fn get(&self, path: &str) -> Option<&Value> {
let mut cur = self;
for seg in path.split('.') {
match cur {
Value::Object(m) => cur = m.get(seg)?,
_ => return None,
}
}
Some(cur)
}
pub fn as_str(&self) -> Option<&str> {
match self {
Value::Str(s) => Some(s),
_ => None,
}
}
}
impl fmt::Display for Value {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", to_sml(self))
}
}