use crate::{ExitCode, builtins::FatalError, dispatch::Resolution, limits::LimitExceeded};
pub(crate) const WITHHELD: &str = "<withheld>";
pub(crate) const CONTROL_WORDS: &[&str] = &[
"break", "continue", "return", "exit", "local", "shift", "unset", ":",
];
pub(crate) fn is_control_word(word: &str) -> bool {
CONTROL_WORDS.contains(&word)
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) enum CommandKind {
Control,
Function,
Builtin,
Capability,
ProviderCommand,
Rejected,
NotFound,
NotGranted,
}
impl CommandKind {
pub(crate) const fn of(resolution: &Resolution) -> Self {
match resolution {
Resolution::Function => Self::Function,
Resolution::Builtin(_) => Self::Builtin,
Resolution::Capability => Self::Capability,
Resolution::ProviderCommand => Self::ProviderCommand,
Resolution::NotGranted { .. } => Self::NotGranted,
Resolution::Rejected(_) => Self::Rejected,
Resolution::NotFound => Self::NotFound,
}
}
pub(crate) const fn label(self) -> &'static str {
match self {
Self::Control => "control",
Self::Function => "function",
Self::Builtin => "builtin",
Self::Capability => "capability",
Self::ProviderCommand => "provider-command",
Self::Rejected => "rejected",
Self::NotFound => "not-found",
Self::NotGranted => "not-granted",
}
}
pub(crate) const fn name_is_fixed_vocabulary(self) -> bool {
match self {
Self::Control
| Self::Builtin
| Self::Rejected
| Self::Capability
| Self::ProviderCommand => true,
Self::Function | Self::NotFound | Self::NotGranted => false,
}
}
}
pub(crate) fn traceable_name(kind: CommandKind, command: &str) -> &str {
if kind.name_is_fixed_vocabulary() || dekopon_core::telemetry_payloads() {
command
} else {
WITHHELD
}
}
pub(crate) fn outcome_label(status: ExitCode) -> &'static str {
match status {
ExitCode::SUCCESS => "succeeded",
ExitCode::SYNTAX => "usage-error",
ExitCode::TIMEOUT => "timed-out",
ExitCode::DENIED => "denied",
ExitCode::NOT_FOUND => "not-found",
_ => "failed",
}
}
pub(crate) fn fatal_exit_code(fatal: &FatalError) -> ExitCode {
match fatal {
FatalError::Limit(LimitExceeded::Deadline { .. }) => ExitCode::TIMEOUT,
FatalError::Limit(_) | FatalError::Unsupported(_) => ExitCode::SYNTAX,
}
}
pub(crate) fn fatal_outcome(fatal: &FatalError) -> &'static str {
match fatal {
FatalError::Limit(LimitExceeded::Deadline { .. }) => "timed-out",
FatalError::Limit(_) => "limit-exceeded",
FatalError::Unsupported(_) => "rejected",
}
}
#[cfg(test)]
mod tests;