move Repo and Recipe into the library

This commit is contained in:
Sebastian Kuzminsky 2025-01-17 18:45:39 -07:00
parent 20c49c62a0
commit 57b8dae357
4 changed files with 84 additions and 62 deletions

View file

@ -1,79 +1,28 @@
use clap::Parser;
use tools::Recipe;
use tools::Repo;
#[derive(Debug, clap::Parser)]
#[command(version, about, long_about = None)]
struct Args {
// The name of the recipe to build.
target: String,
}
// #[derive(Debug, serde::Deserialize)]
// enum Quantity {
// Count(usize),
// Amount(String),
// }
#[derive(Debug, serde::Deserialize)]
struct Input {
/// Each input has either a `quantity` (for discrete things, like
/// apples or bearings) or an `amount` (for continuous things,
/// like rope or flour).
///
/// FIXME: Is there a good way to handle the "units" of the
/// quantity/amount? Maybe the units could default to "count", but
/// be optionally overridden by the recipe to things like "meters"
/// (of tubing) or "grams" (of chromic acid). Then this struct
/// would be `quantity: usize, units: Option<Unit>`.
quantity: Option<usize>,
amount: Option<String>,
}
#[derive(Debug, serde::Deserialize)]
struct Operator {
skills: Vec<String>,
}
#[derive(Debug, serde::Deserialize)]
struct Dependencies {
tools: Vec<String>,
operator: Operator,
}
#[derive(Debug, serde::Deserialize)]
struct Action {
process: String,
}
#[derive(Debug, serde::Deserialize)]
struct Recipe {
/// `[inputs]` is a Table where each key the the name (unique id)
/// of a recipe in the repo, and the value is an Input object.
inputs: std::collections::HashMap<String, Input>,
dependencies: Dependencies,
action: Action,
/// If a recipe has no `[outputs]`, we assume it produces 1x of the
/// thing identified by the name of the recipe.
///
/// FIXME: Or is that always the case, and we should have no outputs
/// section? None of the recipes we've been doodling around with
/// have anything like byproducts or waste streams...
outputs: Option<Vec<String>>,
}
fn validate_recipe(recipe: &Recipe) -> anyhow::Result<()> {
// if recipe.inputs.len() == 0 {
// Err("recipe has no inputs!");
// }
Ok(())
/// Directory containing the repo of all repositories.
#[arg(short, long)]
repo: String,
}
fn main() -> anyhow::Result<()> {
let args = Args::parse();
let repo_contents = std::fs::read_dir(args.repo).unwrap();
let target_str = std::fs::read_to_string(args.target)?;
let recipe: Recipe = toml::from_str(&target_str)?;
println!("{recipe:#?}");
let r = validate_recipe(&recipe);
let r = tools::recipe::validate_recipe(&recipe);
println!("valid? {r:#?}");
Ok(())
}

5
tools/src/lib.rs Normal file
View file

@ -0,0 +1,5 @@
pub mod recipe;
pub use recipe::Recipe;
pub mod repo;
pub use repo::Repo;

62
tools/src/recipe.rs Normal file
View file

@ -0,0 +1,62 @@
// #[derive(Debug, serde::Deserialize)]
// enum Quantity {
// Count(usize),
// Amount(String),
// }
#[derive(Debug, serde::Deserialize)]
pub struct Input {
/// Each input has either a `quantity` (for discrete things, like
/// apples or bearings) or an `amount` (for continuous things,
/// like rope or flour).
///
/// FIXME: Is there a good way to handle the "units" of the
/// quantity/amount? Maybe the units could default to "count", but
/// be optionally overridden by the recipe to things like "meters"
/// (of tubing) or "grams" (of chromic acid). Then this struct
/// would be `quantity: usize, units: Option<Unit>`.
quantity: Option<usize>,
amount: Option<String>,
}
#[derive(Debug, serde::Deserialize)]
pub struct Operator {
skills: Vec<String>,
}
#[derive(Debug, serde::Deserialize)]
pub struct Dependencies {
tools: Vec<String>,
operator: Operator,
}
#[derive(Debug, serde::Deserialize)]
pub struct Action {
process: String,
}
#[derive(Debug, serde::Deserialize)]
pub struct Recipe {
/// `[inputs]` is a Table where each key the the name (unique id)
/// of a recipe in the repo, and the value is an Input object.
inputs: std::collections::HashMap<String, Input>,
dependencies: Dependencies,
action: Action,
/// If a recipe has no `[outputs]`, we assume it produces 1x of the
/// thing identified by the name of the recipe.
///
/// FIXME: Or is that always the case, and we should have no outputs
/// section? None of the recipes we've been doodling around with
/// have anything like byproducts or waste streams...
outputs: Option<Vec<String>>,
}
pub fn validate_recipe(recipe: &Recipe) -> anyhow::Result<()> {
// if recipe.inputs.len() == 0 {
// Err("recipe has no inputs!");
// }
Ok(())
}

6
tools/src/repo.rs Normal file
View file

@ -0,0 +1,6 @@
use crate::recipe::Recipe;
pub struct Repo {
path: String,
recipes: std::collections::HashMap<String, Recipe>,
}