use std::collections::BTreeMap;
use zenith_core::{Diagnostic, Document, RecipeDef};
use super::record_affected;
#[derive(Clone, Copy)]
pub(super) struct RecipeScalars<'a> {
pub(super) id: &'a str,
pub(super) kind: &'a str,
pub(super) seed: Option<i64>,
pub(super) generator: Option<&'a str>,
pub(super) bounds: Option<&'a str>,
pub(super) detached: Option<bool>,
}
pub(super) fn apply_create_recipe(
s: RecipeScalars<'_>,
doc: &mut Document,
diagnostics: &mut Vec<Diagnostic>,
affected: &mut Vec<String>,
) {
if doc.recipes.iter().any(|r| r.id == s.id) {
diagnostics.push(Diagnostic::error(
"tx.duplicate_id",
format!("create_recipe: a recipe with id {:?} already exists", s.id),
None,
Some(s.id.to_owned()),
));
return;
}
doc.recipes.push(RecipeDef {
id: s.id.to_owned(),
kind: s.kind.to_owned(),
seed: s.seed,
generator: s.generator.map(str::to_owned),
bounds: s.bounds.map(str::to_owned),
detached: s.detached,
params: Vec::new(),
palette: Vec::new(),
expanded: Vec::new(),
source_span: None,
unknown_props: BTreeMap::new(),
});
record_affected(s.id, affected);
}
pub(super) fn apply_update_recipe(
s: RecipeScalars<'_>,
doc: &mut Document,
diagnostics: &mut Vec<Diagnostic>,
affected: &mut Vec<String>,
) {
let Some(idx) = doc.recipes.iter().position(|r| r.id == s.id) else {
diagnostics.push(Diagnostic::error(
"tx.unknown_recipe",
format!("update_recipe: no recipe with id {:?} exists", s.id),
None,
Some(s.id.to_owned()),
));
return;
};
if let Some(recipe) = doc.recipes.get_mut(idx) {
recipe.kind = s.kind.to_owned();
recipe.seed = s.seed;
recipe.generator = s.generator.map(str::to_owned);
recipe.bounds = s.bounds.map(str::to_owned);
recipe.detached = s.detached;
}
record_affected(s.id, affected);
}
pub(super) fn apply_delete_recipe(
id: &str,
doc: &mut Document,
diagnostics: &mut Vec<Diagnostic>,
affected: &mut Vec<String>,
) {
let Some(idx) = doc.recipes.iter().position(|r| r.id == id) else {
diagnostics.push(Diagnostic::error(
"tx.unknown_recipe",
format!("delete_recipe: no recipe with id {:?} exists", id),
None,
Some(id.to_owned()),
));
return;
};
doc.recipes.remove(idx);
record_affected(id, affected);
}