feat: add cli

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-08-24 15:40:44 +02:00
parent 61db3da695
commit 5e88ffdbc9
4 changed files with 214 additions and 142 deletions

View File

@@ -0,0 +1,76 @@
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
};
use anyhow::anyhow;
use toml::Table;
use crate::project::CUDDLE_PROJECT_FILE;
pub struct Project {
value: Value,
pub root: PathBuf,
}
impl Project {
pub fn new(value: Value, root: &Path) -> Self {
Self {
value,
root: root.to_path_buf(),
}
}
pub fn from_file(content: &str, root: &Path) -> anyhow::Result<Self> {
let table: Table = toml::from_str(content)?;
let config = Config::default();
let project = table
.get("project")
.ok_or(anyhow!("cuddle.toml doesn't provide a [project] table"))?;
let value: Value = project.into();
Ok(Self::new(value, root))
}
pub async fn from_path(path: &Path) -> anyhow::Result<Self> {
let cuddle_file = path.join(CUDDLE_PROJECT_FILE);
if !cuddle_file.exists() {
anyhow::bail!("no cuddle.toml project file found");
}
let cuddle_project_file = tokio::fs::read_to_string(cuddle_file).await?;
Self::from_file(&cuddle_project_file, path)
}
}
#[derive(Default)]
struct Config {
project: BTreeMap<String, Value>,
}
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())
}
}
}
}