use std::fmt;
use std::time::Duration;
use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum Fault {
Denied,
ConfirmationRequired,
Blocked,
Busy,
}
impl Fault {
pub fn from_code(code: u16) -> Option<Self> {
match code {
606 => Some(Self::Denied),
866 => Some(Self::ConfirmationRequired),
867 => Some(Self::Blocked),
868 => Some(Self::Busy),
_ => None,
}
}
pub fn of(error: &tr064::Error) -> Option<Self> {
error.upnp_code().and_then(Self::from_code)
}
pub fn code(self) -> u16 {
match self {
Self::Denied => 606,
Self::ConfirmationRequired => 866,
Self::Blocked => 867,
Self::Busy => 868,
}
}
pub fn status(self) -> &'static str {
match self {
Self::Denied => "denied",
Self::ConfirmationRequired => "confirmation_required",
Self::Blocked => "blocked",
Self::Busy => "busy",
}
}
pub fn retry_after(self) -> Option<Duration> {
match self {
Self::Blocked => Some(Duration::from_hours(1)),
Self::Busy => Some(Duration::from_mins(2)),
Self::Denied | Self::ConfirmationRequired => None,
}
}
pub fn is_temporary(self) -> bool {
self.retry_after().is_some()
}
}
impl fmt::Display for Fault {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let text = match self {
Self::Denied => "the user lacks the right for this action",
Self::ConfirmationRequired => {
"the device wants this action confirmed with a second factor, \
which fritz never does"
}
Self::Blocked => {
"the device blocks confirmations after failed ones; \
try again in up to an hour"
}
Self::Busy => {
"the device is busy with another confirmation; \
try again in a few minutes"
}
};
write!(f, "{text} (UPnP error {})", self.code())
}
}
#[cfg(test)]
mod tests {
use super::*;
const ALL: [Fault; 4] = [
Fault::Denied,
Fault::ConfirmationRequired,
Fault::Blocked,
Fault::Busy,
];
#[test]
fn maps_the_codes() {
for fault in ALL {
assert_eq!(Fault::from_code(fault.code()), Some(fault));
}
assert_eq!(Fault::from_code(713), None);
assert_eq!(Fault::from_code(401), None);
}
#[test]
fn finds_the_fault_of_an_error() {
let error = tr064::Error::Upnp {
code: 866,
description: "second factor authentication required".to_owned(),
};
assert_eq!(Fault::of(&error), Some(Fault::ConfirmationRequired));
assert_eq!(Fault::of(&tr064::Error::AuthenticationFailed), None);
}
#[test]
fn describes_the_faults() {
let names: Vec<_> = ALL.iter().map(|f| f.status()).collect();
assert_eq!(
names,
["denied", "confirmation_required", "blocked", "busy"]
);
for fault in ALL {
assert_eq!(
serde_json::to_value(fault).unwrap(),
serde_json::json!(fault.status())
);
let text = fault.to_string();
assert!(text.ends_with(&format!("(UPnP error {})", fault.code())));
}
assert!(Fault::ConfirmationRequired.to_string().contains("never"));
}
#[test]
fn says_when_to_try_again() {
let waits: Vec<_> = ALL.iter().map(|f| f.retry_after()).collect();
assert_eq!(
waits,
[
None,
None,
Some(Duration::from_hours(1)),
Some(Duration::from_mins(2))
]
);
assert!(Fault::Blocked.is_temporary());
assert!(!Fault::Denied.is_temporary());
}
}