recipe & repo compile warnings

This commit is contained in:
Sebastian Kuzminsky 2025-03-13 07:55:19 -06:00
parent 8d7e95186e
commit 24a6f61e13
2 changed files with 21 additions and 25 deletions

View file

@ -102,7 +102,7 @@ impl Recipe {
pub fn from_file(file: &std::path::PathBuf) -> Result<Self, RecipeLoadError> { pub fn from_file(file: &std::path::PathBuf) -> Result<Self, RecipeLoadError> {
let recipe_contents = std::fs::read_to_string(file)?; let recipe_contents = std::fs::read_to_string(file)?;
let mut recipe: Recipe = toml::from_str(&recipe_contents)?; let mut recipe: Recipe = toml::from_str(&recipe_contents)?;
if recipe.outputs == None { if recipe.outputs.is_none() {
if let Some(recipe_name) = file.file_stem() { if let Some(recipe_name) = file.file_stem() {
let mut outputs = std::collections::HashMap::<String, Output>::new(); let mut outputs = std::collections::HashMap::<String, Output>::new();
let key = recipe_name.to_string_lossy().into_owned(); let key = recipe_name.to_string_lossy().into_owned();
@ -113,7 +113,7 @@ impl Recipe {
recipe.outputs = Some(outputs); recipe.outputs = Some(outputs);
} }
} }
// let r = recipe.validate_recipe(); recipe.validate_recipe()?;
Ok(recipe) Ok(recipe)
} }
@ -122,17 +122,17 @@ impl Recipe {
if self.inputs.len() != 1 { if self.inputs.len() != 1 {
return false; return false;
} }
if let Some(input_name) = self.inputs.keys().into_iter().next() { if let Some(input_name) = self.inputs.keys().next() {
if input_name == "capital" { if input_name == "capital" {
return true; return true;
} }
} }
return false; false
} }
} }
impl Recipe { impl Recipe {
fn validate_recipe(self: &Self) -> anyhow::Result<()> { fn validate_recipe(&self) -> Result<(), RecipeLoadError> {
// if recipe.inputs.len() == 0 { // if recipe.inputs.len() == 0 {
// Err("recipe has no inputs!"); // Err("recipe has no inputs!");
// } // }
@ -141,8 +141,6 @@ impl Recipe {
} }
mod test { mod test {
use super::*;
#[test] #[test]
fn is_vitamin() { fn is_vitamin() {
let recipes = vec![ let recipes = vec![
@ -160,7 +158,7 @@ mod test {
for (recipe_filename, is_leaf) in recipes.iter() { for (recipe_filename, is_leaf) in recipes.iter() {
let recipe_path = std::path::PathBuf::from(recipe_filename); let recipe_path = std::path::PathBuf::from(recipe_filename);
let recipe = Recipe::from_file(&recipe_path).unwrap(); let recipe = super::Recipe::from_file(&recipe_path).unwrap();
let result = recipe.is_vitamin(); let result = recipe.is_vitamin();
assert_eq!(result, *is_leaf); assert_eq!(result, *is_leaf);
} }

View file

@ -5,7 +5,7 @@ use crate::recipe::Recipe;
#[derive(Debug)] #[derive(Debug)]
pub struct Repo { pub struct Repo {
path: String, _path: String,
recipes: std::collections::HashMap<String, Recipe>, recipes: std::collections::HashMap<String, Recipe>,
} }
@ -18,24 +18,22 @@ pub enum RepoLoadError {
impl Repo { impl Repo {
pub fn new(path: &str) -> Result<Self, RepoLoadError> { pub fn new(path: &str) -> Result<Self, RepoLoadError> {
let mut repo = Self { let mut repo = Self {
path: std::string::String::from(path), _path: std::string::String::from(path),
recipes: std::collections::HashMap::<String, Recipe>::new(), recipes: std::collections::HashMap::<String, Recipe>::new(),
}; };
repo.add_dir(path)?; repo.add_dir(path)?;
Ok(repo) Ok(repo)
} }
pub fn get_recipe(self: &Self, recipe_name: &str) -> Option<&Recipe> { pub fn get_recipe(&self, recipe_name: &str) -> Option<&Recipe> {
self.recipes.get(recipe_name) self.recipes.get(recipe_name)
} }
pub fn compile(self: &Self, target: &str) -> anyhow::Result<()> { pub fn compile(&self, target: &str) -> anyhow::Result<()> {
let recipe = self.get_recipe(target); let recipe = self.get_recipe(target);
match recipe { match recipe {
None => { None => Err(anyhow::Error::msg(format!("recipe for {target} not found"))),
return Err(anyhow::Error::msg(format!("recipe for {target} not found")));
}
Some(recipe) => { Some(recipe) => {
let puml_filename = format!("{target}.puml"); let puml_filename = format!("{target}.puml");
let mut puml_file = std::fs::File::create(&puml_filename)?; let mut puml_file = std::fs::File::create(&puml_filename)?;
@ -57,7 +55,7 @@ impl Repo {
} }
impl Repo { impl Repo {
fn add_dir(self: &mut Self, path: &str) -> Result<(), RepoLoadError> { fn add_dir(&mut self, path: &str) -> Result<(), RepoLoadError> {
// println!("reading Recipes from {path}"); // println!("reading Recipes from {path}");
let dir_entries = std::fs::read_dir(path)?; let dir_entries = std::fs::read_dir(path)?;
for dir_entry in dir_entries { for dir_entry in dir_entries {
@ -86,11 +84,11 @@ impl Repo {
Ok(()) Ok(())
} }
fn add_file(self: &mut Self, path: &std::path::PathBuf) -> anyhow::Result<()> { fn add_file(&mut self, path: &std::path::PathBuf) -> anyhow::Result<()> {
// println!("reading Recipe from {:?}", path); // println!("reading Recipe from {:?}", path);
if let Some(recipe_name) = path.file_stem() { if let Some(recipe_name) = path.file_stem() {
let key = recipe_name.to_string_lossy().into_owned(); let key = recipe_name.to_string_lossy().into_owned();
let value = match crate::Recipe::from_file(path) { let value = match Recipe::from_file(path) {
Ok(recipe) => recipe, Ok(recipe) => recipe,
Err(e) => { Err(e) => {
return Err(anyhow::Error::msg(format!( return Err(anyhow::Error::msg(format!(
@ -105,7 +103,7 @@ impl Repo {
} }
fn compile_inner( fn compile_inner(
self: &Self, &self,
puml_file: &mut std::fs::File, puml_file: &mut std::fs::File,
recipe_name: &str, // the name of the thing we're making recipe_name: &str, // the name of the thing we're making
recipe: &Recipe, // the recipe for the thing we're making recipe: &Recipe, // the recipe for the thing we're making
@ -192,7 +190,7 @@ impl Repo {
false => (input_name.clone(), input_name.clone()), false => (input_name.clone(), input_name.clone()),
}; };
writeln!(puml_file, "object {}", input_object_declaration); writeln!(puml_file, "object {}", input_object_declaration)?;
match &input_recipe.action { match &input_recipe.action {
crate::recipe::Action::process(process) => { crate::recipe::Action::process(process) => {
writeln!(puml_file, "{} : {:?}", input_object_name, process)?; writeln!(puml_file, "{} : {:?}", input_object_name, process)?;
@ -206,14 +204,14 @@ impl Repo {
} }
if let Some(cost_str) = cost { if let Some(cost_str) = cost {
writeln!(puml_file, "{} : {:?}", input_object_name, cost_str); writeln!(puml_file, "{} : {:?}", input_object_name, cost_str)?;
} }
write!(puml_file, "{} <|-- {}", recipe_name, input_object_name); write!(puml_file, "{} <|-- {}", recipe_name, input_object_name)?;
if input_info.quantity.unit != None || input_info.quantity.amount != 1.0 { if input_info.quantity.unit.is_some() || input_info.quantity.amount != 1.0 {
write!(puml_file, " : quantity={:?}", input_info.quantity); write!(puml_file, " : quantity={:?}", input_info.quantity)?;
} }
writeln!(puml_file, ""); writeln!(puml_file)?;
if !input_recipe.is_vitamin() { if !input_recipe.is_vitamin() {
self.compile_inner(puml_file, input_name, input_recipe, indent + 4)?; self.compile_inner(puml_file, input_name, input_recipe, indent + 4)?;