use clap::ArgMatches;
use crate::InputError;
pub trait InputCollector<T>: Send + Sync {
fn name(&self) -> &'static str;
fn is_available(&self, matches: &ArgMatches) -> bool;
fn collect(&self, matches: &ArgMatches) -> Result<Option<T>, InputError>;
fn validate(&self, _value: &T) -> Result<(), String> {
Ok(())
}
fn can_retry(&self) -> bool {
false
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedInput<T> {
pub value: T,
pub source: InputSourceKind,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InputSourceKind {
Arg,
Flag,
Stdin,
Env,
Clipboard,
Editor,
Prompt,
Default,
}
impl std::fmt::Display for InputSourceKind {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Arg => write!(f, "argument"),
Self::Flag => write!(f, "flag"),
Self::Stdin => write!(f, "stdin"),
Self::Env => write!(f, "environment variable"),
Self::Clipboard => write!(f, "clipboard"),
Self::Editor => write!(f, "editor"),
Self::Prompt => write!(f, "prompt"),
Self::Default => write!(f, "default"),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn source_kind_display() {
assert_eq!(InputSourceKind::Arg.to_string(), "argument");
assert_eq!(InputSourceKind::Stdin.to_string(), "stdin");
assert_eq!(InputSourceKind::Editor.to_string(), "editor");
}
}