use anyhow::Result;
use clap::ArgMatches;
use serde::{Deserialize, Serialize};
use std::{fs, path::PathBuf};
use crate::{
models::{action::ActionModel, arg::ArgActionModel},
validate::ValidateTrait,
};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FlowModel {
pub name: String,
pub about: String,
#[serde(default)]
pub check: Option<String>,
#[serde(default)]
pub clipboard: bool,
#[serde(default)]
pub args: Vec<ArgActionModel>,
#[serde(default)]
pub actions: Vec<ActionModel>,
}
impl FlowModel {
pub fn load(path: &PathBuf) -> Result<Self> {
let content = fs::read_to_string(path)?;
let flow: Self = yaml_serde::from_str(&content)
.map_err(|e| anyhow::anyhow!("Failed to parse {}: {}", path.display(), e))?;
flow.validate()?;
Ok(flow)
}
pub fn apply_args(mut self, matches: &ArgMatches) -> Self {
for arg in &mut self.args {
arg.resolve_values(matches);
}
for action in &mut self.actions {
for arg in &self.args {
for v in &arg.values {
action.action = action.action.replace(&format!("{{{}}}", v.name), &v.value);
}
}
}
self
}
}