use serde::Deserialize;
use serde::Serialize;
use crate::models::context::ContextModel;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum ActionRun {
Cmd,
Value,
Tiny,
Small,
Medium,
Large,
Vision,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum ActionValue {
Simple(String),
Switch(Vec<SwitchCase>),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SwitchCase {
pub when: String,
pub then: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ActionModel {
pub tag: String,
pub run: ActionRun,
pub expect: Option<ContextModel>,
#[serde(default)]
pub check: Option<String>,
#[serde(default)]
pub confirm: bool,
pub action: ActionValue,
}
impl ActionModel {
pub fn actions(&self) -> Vec<&str> {
let mut texts = Vec::new();
match &self.action {
ActionValue::Simple(s) => {
if !s.is_empty() {
texts.push(s.as_str());
}
}
ActionValue::Switch(cases) => {
for case in cases {
texts.push(case.when.as_str());
texts.push(case.then.as_str());
}
}
}
texts
}
}
impl std::fmt::Display for ActionRun {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
ActionRun::Cmd => write!(f, "cmd"),
ActionRun::Value => write!(f, "val"),
ActionRun::Tiny => write!(f, "tiny"),
ActionRun::Small => write!(f, "small"),
ActionRun::Medium => write!(f, "medium"),
ActionRun::Large => write!(f, "large"),
ActionRun::Vision => write!(f, "vision"),
}
}
}