@@ -1,10 +1,7 @@
|
||||
use std::{
|
||||
collections::BTreeMap,
|
||||
path::{Path, PathBuf},
|
||||
};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use anyhow::anyhow;
|
||||
use serde::Serialize;
|
||||
use cuddle_value::Value;
|
||||
use toml::Table;
|
||||
|
||||
use crate::project::CUDDLE_PROJECT_FILE;
|
||||
@@ -48,43 +45,3 @@ impl Project {
|
||||
Self::from_file(&cuddle_project_file, path)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Debug, Serialize)]
|
||||
#[serde(untagged)]
|
||||
pub enum Value {
|
||||
String(String),
|
||||
Bool(bool),
|
||||
Array(Vec<Value>),
|
||||
Map(BTreeMap<String, Value>),
|
||||
}
|
||||
|
||||
impl From<&toml::Value> for Value {
|
||||
fn from(value: &toml::Value) -> Self {
|
||||
match value {
|
||||
toml::Value::String(s) => Self::String(s.clone()),
|
||||
toml::Value::Integer(i) => Self::String(i.to_string()),
|
||||
toml::Value::Float(f) => Self::String(f.to_string()),
|
||||
toml::Value::Boolean(b) => Self::Bool(*b),
|
||||
toml::Value::Datetime(dt) => Self::String(dt.to_string()),
|
||||
toml::Value::Array(array) => Self::Array(array.iter().map(|i| i.into()).collect()),
|
||||
toml::Value::Table(tbl) => {
|
||||
Self::Map(tbl.iter().map(|(k, v)| (k.clone(), v.into())).collect())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Value {
|
||||
pub fn get(&self, path: &[&str]) -> Option<&Value> {
|
||||
match path.split_first() {
|
||||
Some((current, rest)) => match self {
|
||||
Value::Map(map) => match map.get(¤t.to_string()) {
|
||||
Some(value) => value.get(rest),
|
||||
None => None,
|
||||
},
|
||||
_ => None,
|
||||
},
|
||||
None => Some(self),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user