use std::path::{PathBuf, Path}; use std::collections::HashMap; use serde::{Serialize, Deserialize}; #[derive(Serialize, Deserialize, Clone, Debug)] pub struct Declaration { pub name: String, pub base: Option, pub variants: HashMap, } #[derive(Serialize, Deserialize, Clone, Debug)] pub struct Variant { pub base: Option, pub src: Option, // Loads every asset from an SVG file #[serde(default)] pub assets: Vec, // Loads individual assets #[serde(default)] pub overwrites: Vec, // Operations on assets } #[derive(Serialize, Deserialize, Clone, Debug)] pub struct AssetDecl { pub name: String, pub src: Option, } #[derive(Serialize, Deserialize, Clone, Debug)] pub struct Overwrite { pub id: String, // ID of the element to modify pub fill: Option, pub stroke: Option, #[serde(default)] pub remove: bool, } impl Declaration { pub fn join(self, parent: Self) -> Self { let mut variants = self.variants; for (name, parent_variant) in parent.variants { if let Some(variant) = variants.get_mut(&name) { variant.join(parent_variant); } else { variants.insert(name, parent_variant); } } Self { name: self.name, base: parent.base, variants } } /// Replaces every path relative to the yaml file to paths relative to the cwd pub fn canonicalize(&mut self, path: impl AsRef) { let path = path.as_ref().parent().unwrap_or(&PathBuf::from(".")).to_path_buf(); for variant in self.variants.values_mut() { variant.canonicalize(path.as_ref()); } } } impl Variant { pub fn join(&mut self, mut parent: Self) { self.assets.append(&mut parent.assets); self.overwrites.append(&mut parent.overwrites); if self.base.is_none() { self.base = parent.base; } if self.src.is_none() { self.src = parent.src; // TODO: handle relative paths } } /// Replaces every path relative to the yaml file to paths relative to the cwd pub fn canonicalize(&mut self, path: &Path) { match &mut self.src { Some(src_path) => *src_path = path.join(&*src_path), None => {} } for asset in &mut self.assets { asset.canonicalize(path); } // for overwrite in &mut self.overwrites { // overwrite.canonicalize(path); // } } } impl AssetDecl { /// Replaces every path relative to the yaml file to paths relative to the cwd pub fn canonicalize(&mut self, path: &Path) { match &mut self.src { Some(src_path) => *src_path = path.join(&*src_path), None => {} } } }