use serde::Serialize;
use serde_json::Value;
#[derive(Debug, Serialize, Clone)]
pub struct Maskfile {
pub title: String,
pub description: String,
pub commands: Vec<Command>,
}
impl Maskfile {
pub fn to_json(&self) -> Result<Value, serde_json::Error> {
serde_json::to_value(&self)
}
}
#[derive(Debug, Serialize, Clone)]
pub struct Command {
pub level: u8,
pub name: String,
pub description: String,
pub script: Option<Script>,
pub subcommands: Vec<Command>,
pub required_args: Vec<RequiredArg>,
pub optional_args: Vec<OptionalArg>,
pub named_flags: Vec<NamedFlag>,
}
impl Command {
pub fn new(level: u8) -> Self {
Self {
level,
name: "".to_string(),
description: "".to_string(),
script: Some(Script::new()),
subcommands: vec![],
required_args: vec![],
optional_args: vec![],
named_flags: vec![],
}
}
pub fn build(mut self) -> Self {
if let Some(s) = &mut self.script {
if s.source.is_empty() && s.executor.is_empty() {
self.script = None;
}
}
if self.script.is_some() {
self.named_flags.push(NamedFlag {
name: "verbose".to_string(),
description: "Sets the level of verbosity".to_string(),
short: "v".to_string(),
long: "verbose".to_string(),
multiple: false,
takes_value: false,
required: false,
validate_as_number: false,
choices: vec![],
val: "".to_string(),
});
}
self
}
}
#[derive(Debug, Serialize, Clone)]
pub struct Script {
pub executor: String, pub source: String,
}
impl Script {
pub fn new() -> Self {
Self {
executor: "".to_string(),
source: "".to_string(),
}
}
}
#[derive(Debug, Serialize, Clone)]
pub struct RequiredArg {
pub name: String,
#[serde(skip)]
pub val: String,
}
impl RequiredArg {
pub fn new(name: String) -> Self {
Self {
name,
val: "".to_string(),
}
}
}
#[derive(Debug, Serialize, Clone)]
pub struct OptionalArg {
pub name: String,
#[serde(skip)]
pub val: String,
}
impl OptionalArg {
pub fn new(name: String) -> Self {
Self {
name,
val: "".to_string(),
}
}
}
#[derive(Debug, Serialize, Clone)]
pub struct NamedFlag {
pub name: String,
pub description: String,
pub short: String, pub long: String, pub multiple: bool, pub takes_value: bool, pub validate_as_number: bool, pub choices: Vec<String>, pub required: bool,
#[serde(skip)]
pub val: String,
}
impl NamedFlag {
pub fn new() -> Self {
Self {
name: "".to_string(),
description: "".to_string(),
short: "".to_string(),
long: "".to_string(),
multiple: false,
takes_value: false,
required: false,
validate_as_number: false,
choices: vec![],
val: "".to_string(),
}
}
}