minimal config file parsing

This commit is contained in:
sigil-03 2025-01-12 22:40:08 -07:00
parent b4e5967845
commit 8fda9c2c3e
8 changed files with 153 additions and 0 deletions

View file

@ -0,0 +1,2 @@
/target
Cargo.lock

View file

@ -0,0 +1,9 @@
[package]
name = "recipe-loader"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "^4.5.26", features = ["derive"] }
recipe = {path="../recipe"}
toml = "0.8.19"

View file

@ -0,0 +1,58 @@
use clap::Parser;
use recipe::TomlRecipe;
use std::fs;
use std::path::Path;
#[derive(Parser, Debug)]
struct Cli {
#[arg(long)]
/// the recipe you'd like to make (needs to be in the repo)
recipe: String,
#[arg(long)]
/// the repository of your recipes
repo: String,
}
pub struct LoadedEnvironment {
recipes: Vec<TomlRecipe>,
}
impl Default for LoadedEnvironment {
fn default() -> Self {
Self {
recipes: Vec::new(),
}
}
}
impl LoadedEnvironment {
pub fn new(repo: &Path) -> Self {
let paths = fs::read_dir(repo).unwrap();
let mut env = LoadedEnvironment::default();
for path in paths {
let path = path.expect("Failed to unwrap path");
env.recipes.push(TomlRecipe::new(&path.path()))
}
env
}
pub fn print(&self) {
for recipe in &self.recipes {
recipe.print();
println!();
}
}
}
pub fn main() {
let cli = Cli::parse();
let repo = Path::new(&cli.repo);
let env = LoadedEnvironment::new(repo);
env.print();
// let recipe = TomlRecipe::default();
// recipe.print();
println!("{}", repo.display());
}

View file

@ -0,0 +1,2 @@
/target
Cargo.lock

View file

@ -0,0 +1,8 @@
[package]
name = "recipe"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0.215", features = ["derive"] }
toml = "0.8.19"

View file

@ -0,0 +1,8 @@
mod recipe;
pub use crate::recipe::TomlRecipe;
#[cfg(test)]
mod tests {
use super::*;
}

View file

@ -0,0 +1,64 @@
use serde::{Deserialize, Serialize};
use std::fs;
use std::path::Path;
use std::collections::BTreeMap;
#[derive(Hash, PartialEq, Eq, Deserialize, Serialize, Debug)]
pub struct Input {}
#[derive(Hash, PartialEq, Eq, Deserialize, Serialize, Debug)]
pub struct Output {}
#[derive(Hash, PartialEq, Eq, Deserialize, Serialize, Debug)]
pub struct Dependency {}
#[derive(Hash, PartialEq, Eq, Deserialize, Serialize, Debug)]
pub struct Config {}
// Recipe which can get read out of a toml file
#[derive(Deserialize, Serialize, Debug)]
pub struct TomlRecipe {
name: String,
inputs: BTreeMap<String, Input>,
outputs: BTreeMap<String, Output>,
dependencies: BTreeMap<String, Dependency>,
config: Config,
}
impl Default for TomlRecipe {
fn default() -> Self {
Self {
name: String::new(),
inputs: BTreeMap::new(),
outputs: BTreeMap::new(),
dependencies: BTreeMap::new(),
config: Config {},
}
}
}
impl TomlRecipe {
pub fn new(config_file: &Path) -> Self {
let file: String = fs::read_to_string(&config_file).expect("Could not read input file");
let recipe: TomlRecipe = toml::from_str(&file).expect("Could not parse TOML file");
recipe
}
pub fn print(&self) {
println!("{} ---------------", self.name);
println!(" INPUTS");
for (key, _) in &self.inputs {
println!(" * {}", key);
}
println!();
println!(" OUTPUTS");
for (key, _) in &self.outputs {
println!(" * {}", key);
}
println!();
println!(" DEPENDENCIES");
for (key, _) in &self.dependencies {
println!(" * {}", key);
}
}
}

View file

@ -0,0 +1,2 @@
1. recipe visualizer using mermaid or some other charting library