use crate::{
command_attr::CommandAttrs, error::compile_error_at, fields_parse::ParserType,
rename_rules::RenameRule, Result,
};
pub(crate) struct CommandEnum {
pub prefix: String,
pub description: Option<String>,
pub command_separator: String,
pub rename_rule: RenameRule,
pub parser_type: ParserType,
}
impl CommandEnum {
pub fn from_attributes(attributes: &[syn::Attribute]) -> Result<Self> {
let attrs = CommandAttrs::from_attributes(attributes)?;
let CommandAttrs {
prefix,
description,
rename_rule,
rename,
parser,
separator,
command_separator,
hide,
} = attrs;
if let Some((_, sp)) = rename {
return Err(compile_error_at(
"`rename` can only be applied to enum variants, not the enum itself",
sp,
));
}
if let Some(sp) = hide {
return Err(compile_error_at(
"`hide` can only be applied to enum variants, not the enum itself",
sp,
));
}
let mut parser = parser.map(|(p, _)| p).unwrap_or(ParserType::Default);
if let (
ParserType::Split {
separator: ref mut sep,
},
Some((s, _)),
) = (&mut parser, &separator)
{
*sep = Some(s.clone());
}
Ok(Self {
prefix: prefix.map(|(p, _)| p).unwrap_or_else(|| "/".to_owned()),
description: description.map(|(d, _)| d),
command_separator: command_separator
.map(|(s, _)| s)
.unwrap_or_else(|| " ".to_owned()),
rename_rule: rename_rule
.map(|(rr, _)| rr)
.unwrap_or(RenameRule::Identity),
parser_type: parser,
})
}
}