use std::fmt;
use std::sync::Arc;
use axum::http::Method;
use serde::{Deserialize, Serialize};
use super::DeviceScope;
use crate::error::{KernelError, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub enum DeviceKind {
Phone,
Terminal,
Tray,
}
impl DeviceKind {
pub fn as_str(self) -> &'static str {
match self {
Self::Phone => "phone",
Self::Terminal => "terminal",
Self::Tray => "tray",
}
}
}
impl fmt::Display for DeviceKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl std::str::FromStr for DeviceKind {
type Err = KernelError;
fn from_str(text: &str) -> Result<Self> {
match text.trim().to_ascii_lowercase().as_str() {
"phone" => Ok(Self::Phone),
"terminal" => Ok(Self::Terminal),
"tray" => Ok(Self::Tray),
other => Err(KernelError::Config {
reason: format!("unknown device kind `{other}`: use phone, terminal or tray"),
}),
}
}
}
pub type DeviceRoutes = Arc<dyn Fn(&Method, &str) -> Option<DeviceScope> + Send + Sync>;