use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ErrorCode {
ActionTimeout,
ActionFailed,
InputState,
UnsupportedInput,
InvalidInputValue,
TargetUnavailable,
TargetStale,
AmbiguousTarget,
ActionabilityChanged,
HistoryUnavailable,
ExpectationRequired,
ExpectationAlreadySatisfied,
InvalidExpectation,
InvalidTimeout,
InvalidArgument,
AmbiguousDestination,
NavigationFailed,
NavigationAborted,
ValueMismatch,
DownloadBusy,
DownloadUnsupported,
Unsupported,
PreconditionFailed,
Busy,
InvalidState,
CleanupUncertain,
}
impl ErrorCode {
pub const fn as_str(self) -> &'static str {
match self {
Self::ActionTimeout => "action_timeout",
Self::ActionFailed => "action_failed",
Self::InputState => "input_state",
Self::UnsupportedInput => "unsupported_input",
Self::InvalidInputValue => "invalid_input_value",
Self::TargetUnavailable => "target_unavailable",
Self::TargetStale => "target_stale",
Self::AmbiguousTarget => "ambiguous_target",
Self::ActionabilityChanged => "actionability_changed",
Self::HistoryUnavailable => "history_unavailable",
Self::ExpectationRequired => "expectation_required",
Self::ExpectationAlreadySatisfied => "expectation_already_satisfied",
Self::InvalidExpectation => "invalid_expectation",
Self::InvalidTimeout => "invalid_timeout",
Self::InvalidArgument => "invalid_argument",
Self::AmbiguousDestination => "ambiguous_destination",
Self::NavigationFailed => "navigation_failed",
Self::NavigationAborted => "navigation_aborted",
Self::ValueMismatch => "value_mismatch",
Self::DownloadBusy => "download_busy",
Self::DownloadUnsupported => "download_unsupported",
Self::Unsupported => "unsupported",
Self::PreconditionFailed => "precondition_failed",
Self::Busy => "busy",
Self::InvalidState => "invalid_state",
Self::CleanupUncertain => "cleanup_uncertain",
}
}
pub const fn is_transient(self) -> bool {
matches!(
self,
Self::ActionTimeout
| Self::TargetUnavailable
| Self::ActionabilityChanged
| Self::NavigationFailed
| Self::NavigationAborted
| Self::DownloadBusy
| Self::Busy
| Self::PreconditionFailed
| Self::ActionFailed
)
}
}
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl From<ErrorCode> for String {
fn from(code: ErrorCode) -> Self {
code.as_str().to_owned()
}
}
impl PartialEq<str> for ErrorCode {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}
impl PartialEq<&str> for ErrorCode {
fn eq(&self, other: &&str) -> bool {
self.as_str() == *other
}
}
impl PartialEq<ErrorCode> for &str {
fn eq(&self, other: &ErrorCode) -> bool {
*self == other.as_str()
}
}
impl serde::Serialize for ErrorCode {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.as_str())
}
}