2022-10-25 16:32:36 -07:00
|
|
|
use super::*;
|
|
|
|
|
2024-01-12 18:44:13 -08:00
|
|
|
#[derive(EnumString, PartialEq, Debug, Clone, Serialize, Ord, PartialOrd, Eq, IntoStaticStr)]
|
2022-10-31 00:52:03 -07:00
|
|
|
#[strum(serialize_all = "kebab-case")]
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
2024-01-12 18:44:13 -08:00
|
|
|
pub(crate) enum Attribute<'src> {
|
|
|
|
Confirm(Option<StringLiteral<'src>>),
|
2022-10-31 00:52:03 -07:00
|
|
|
Linux,
|
|
|
|
Macos,
|
2022-11-02 23:37:35 -07:00
|
|
|
NoCd,
|
2022-10-25 16:32:36 -07:00
|
|
|
NoExitMessage,
|
2022-11-22 16:25:57 -08:00
|
|
|
Private,
|
2024-01-12 12:38:23 -08:00
|
|
|
NoQuiet,
|
2022-10-31 00:52:03 -07:00
|
|
|
Unix,
|
|
|
|
Windows,
|
2022-10-25 16:32:36 -07:00
|
|
|
}
|
|
|
|
|
2024-01-12 18:44:13 -08:00
|
|
|
impl<'src> Attribute<'src> {
|
|
|
|
pub(crate) fn from_name(name: Name) -> Option<Self> {
|
2022-10-25 16:32:36 -07:00
|
|
|
name.lexeme().parse().ok()
|
|
|
|
}
|
2022-10-31 00:52:03 -07:00
|
|
|
|
2024-01-12 18:44:13 -08:00
|
|
|
pub(crate) fn name(&self) -> &'static str {
|
2022-10-31 00:52:03 -07:00
|
|
|
self.into()
|
|
|
|
}
|
2024-01-12 18:44:13 -08:00
|
|
|
|
|
|
|
pub(crate) fn with_argument(
|
|
|
|
self,
|
|
|
|
name: Name<'src>,
|
|
|
|
argument: StringLiteral<'src>,
|
|
|
|
) -> CompileResult<'src, Self> {
|
|
|
|
match self {
|
|
|
|
Self::Confirm(_) => Ok(Self::Confirm(Some(argument))),
|
|
|
|
_ => Err(name.error(CompileErrorKind::UnexpectedAttributeArgument { attribute: self })),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn argument(&self) -> Option<&StringLiteral> {
|
|
|
|
if let Self::Confirm(prompt) = self {
|
|
|
|
prompt.as_ref()
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'src> Display for Attribute<'src> {
|
|
|
|
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
|
|
|
|
write!(f, "{}", self.name())?;
|
|
|
|
|
|
|
|
if let Some(argument) = self.argument() {
|
|
|
|
write!(f, "({argument})")?;
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2022-10-31 00:52:03 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
2024-01-12 18:44:13 -08:00
|
|
|
fn name() {
|
|
|
|
assert_eq!(Attribute::NoExitMessage.name(), "no-exit-message");
|
2022-10-31 00:52:03 -07:00
|
|
|
}
|
2022-10-25 16:32:36 -07:00
|
|
|
}
|