xacli_core/command/ext.rs
1use crate::{Arg, Context, Result};
2
3// ============================================================================
4// Extension Traits
5// ============================================================================
6
7/// Validates an argument value
8///
9/// Implement this trait to create custom validators:
10/// ```ignore
11/// use xacli_core::{Validator, ArgValue, Context, Result, Error};
12///
13/// struct NonEmpty;
14/// impl Validator for NonEmpty {
15/// fn validate(&self, ctx: &dyn Context, value: &ArgValue) -> Result<()> {
16/// match value.as_string() {
17/// Ok(s) if !s.is_empty() => Ok(()),
18/// _ => Err(Error::InvalidArgumentValue("Value cannot be empty".into())),
19/// }
20/// }
21/// }
22/// ```
23pub trait Validator {
24 fn validate(&self, ctx: &dyn Context, arg: &Arg) -> Result<()>;
25}
26
27/// Provides dynamic completion suggestions
28///
29/// Implement this trait to provide completions for an argument:
30/// ```ignore
31/// use xacli_core::{Completer, Context};
32///
33/// struct FileCompleter;
34/// impl Completer for FileCompleter {
35/// fn complete(&self, ctx: &dyn Context, prefix: &str) -> Vec<String> {
36/// // Return files matching prefix
37/// vec![]
38/// }
39/// }
40/// ```
41pub trait Completer {
42 fn complete(&self, ctx: &dyn Context, prefix: &str) -> Vec<String>;
43}
44
45/// Prompts user for input interactively
46///
47/// Implement this trait to create custom prompt components:
48/// ```ignore
49/// use xacli_core::{Prompter, Arg, ArgValue, Context};
50/// use std::io;
51///
52/// struct InputPrompter { message: String }
53/// impl Prompter for InputPrompter {
54/// fn prompt(&self, ctx: &mut dyn Context, arg: &Arg) -> io::Result<ArgValue> {
55/// // Interactive prompt logic using ctx.read_event(), ctx.write_command(), etc.
56/// Ok(ArgValue::String("user input".into()))
57/// }
58/// }
59/// ```
60pub trait Prompter {
61 fn prompt(&self, ctx: &mut dyn Context, arg: &Arg) -> Result<String>;
62}