use crate::logging::{LoggingKind, LoggingLevel};
use bpaf::Bpaf;
use std::str::FromStr;
#[derive(Debug, Clone, Bpaf)]
pub struct CliOptions {
#[bpaf(long("colors"), argument("off|force"))]
pub colors: Option<ColorsArg>,
#[bpaf(
long("log-level"),
argument("none|debug|info|warn|error"),
fallback(LoggingLevel::default()),
display_fallback
)]
pub log_level: LoggingLevel,
#[bpaf(
long("log-kind"),
argument("pretty|compact|json"),
fallback(LoggingKind::default()),
display_fallback
)]
pub log_kind: LoggingKind,
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum ColorsArg {
Off,
Force,
}
impl FromStr for ColorsArg {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"off" => Ok(Self::Off),
"force" => Ok(Self::Force),
_ => Err(format!(
"value {s:?} is not valid for the --colors argument"
)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn color_args_from_str() {
assert_eq!("off".parse::<ColorsArg>(), Ok(ColorsArg::Off));
assert_eq!("force".parse::<ColorsArg>(), Ok(ColorsArg::Force));
assert_eq!(
"unknown".parse::<ColorsArg>(),
Err("value \"unknown\" is not valid for the --colors argument".to_string())
);
}
}