use core::fmt;
use crate::{err::CommandError, NetFn};
#[derive(Clone)]
pub enum Command {
Unknown(u8),
GetChannelAuthCapabilities,
SetSessionPrivilegeLevel,
GetChannelCipherSuites,
}
impl fmt::Display for Command {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Command::Unknown(x) => write!(f, "Unknown: {}", x),
Command::GetChannelAuthCapabilities => write!(f, "Get Channel Auth Capabilities"),
Command::SetSessionPrivilegeLevel => write!(f, "Set Session Privilege Level"),
Command::GetChannelCipherSuites => write!(f, "Get Channel Cipher Suites"),
}
}
}
type CommandAndNetfn = (u8, NetFn);
impl TryFrom<CommandAndNetfn> for Command {
type Error = CommandError;
fn try_from(value: CommandAndNetfn) -> Result<Self, CommandError> {
let command_code = value.0;
let netfn = value.1;
match netfn {
NetFn::App => match command_code {
0x38 => Ok(Command::GetChannelAuthCapabilities),
0x54 => Ok(Command::GetChannelCipherSuites),
0x3b => Ok(Command::SetSessionPrivilegeLevel),
_ => Ok(Command::Unknown(command_code)), },
_ => Ok(Command::Unknown(command_code)),
}
}
}
impl Into<u8> for Command {
fn into(self) -> u8 {
match self {
Command::GetChannelAuthCapabilities => 0x38,
Command::GetChannelCipherSuites => 0x54,
Command::SetSessionPrivilegeLevel => 0x3b,
Command::Unknown(x) => x,
}
}
}
impl Into<CommandAndNetfn> for Command {
fn into(self) -> CommandAndNetfn {
match self {
Command::GetChannelAuthCapabilities => (0x38, NetFn::App),
Command::GetChannelCipherSuites => (0x54, NetFn::App),
Command::SetSessionPrivilegeLevel => (0x3b, NetFn::App),
Command::Unknown(x) => (x, NetFn::Unknown(0)),
}
}
}