use super::Error;
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
pub enum MethodType {
Request,
Response,
Error,
}
#[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
pub enum Method {
Binding(MethodType),
Allocate(MethodType),
CreatePermission(MethodType),
ChannelBind(MethodType),
Refresh(MethodType),
SendIndication,
DataIndication,
}
pub const BINDING_REQUEST: Method = Method::Binding(MethodType::Request);
pub const BINDING_RESPONSE: Method = Method::Binding(MethodType::Response);
pub const BINDING_ERROR: Method = Method::Binding(MethodType::Error);
pub const ALLOCATE_REQUEST: Method = Method::Allocate(MethodType::Request);
pub const ALLOCATE_RESPONSE: Method = Method::Allocate(MethodType::Response);
pub const ALLOCATE_ERROR: Method = Method::Allocate(MethodType::Error);
pub const CREATE_PERMISSION_REQUEST: Method = Method::CreatePermission(MethodType::Request);
pub const CREATE_PERMISSION_RESPONSE: Method = Method::CreatePermission(MethodType::Response);
pub const CREATE_PERMISSION_ERROR: Method = Method::CreatePermission(MethodType::Error);
pub const CHANNEL_BIND_REQUEST: Method = Method::ChannelBind(MethodType::Request);
pub const CHANNEL_BIND_RESPONSE: Method = Method::ChannelBind(MethodType::Response);
pub const CHANNEL_BIND_ERROR: Method = Method::ChannelBind(MethodType::Error);
pub const REFRESH_REQUEST: Method = Method::Refresh(MethodType::Request);
pub const REFRESH_RESPONSE: Method = Method::Refresh(MethodType::Response);
pub const REFRESH_ERROR: Method = Method::Refresh(MethodType::Error);
pub const SEND_INDICATION: Method = Method::SendIndication;
pub const DATA_INDICATION: Method = Method::DataIndication;
impl Method {
pub fn is_error(&self) -> bool {
matches!(
self,
Method::Binding(MethodType::Error)
| Method::Refresh(MethodType::Error)
| Method::Allocate(MethodType::Error)
| Method::CreatePermission(MethodType::Error)
| Method::ChannelBind(MethodType::Error)
)
}
pub fn error(&self) -> Option<Method> {
match self {
Method::Binding(_) => Some(BINDING_ERROR),
Method::Allocate(_) => Some(ALLOCATE_ERROR),
Method::CreatePermission(_) => Some(CREATE_PERMISSION_ERROR),
Method::ChannelBind(_) => Some(CHANNEL_BIND_ERROR),
Method::Refresh(_) => Some(REFRESH_ERROR),
_ => None,
}
}
}
impl TryFrom<u16> for Method {
type Error = Error;
fn try_from(value: u16) -> Result<Self, Self::Error> {
Ok(match value {
0x0001 => Self::Binding(MethodType::Request),
0x0101 => Self::Binding(MethodType::Response),
0x0111 => Self::Binding(MethodType::Error),
0x0003 => Self::Allocate(MethodType::Request),
0x0103 => Self::Allocate(MethodType::Response),
0x0113 => Self::Allocate(MethodType::Error),
0x0008 => Self::CreatePermission(MethodType::Request),
0x0108 => Self::CreatePermission(MethodType::Response),
0x0118 => Self::CreatePermission(MethodType::Error),
0x0009 => Self::ChannelBind(MethodType::Request),
0x0109 => Self::ChannelBind(MethodType::Response),
0x0119 => Self::ChannelBind(MethodType::Error),
0x0004 => Self::Refresh(MethodType::Request),
0x0104 => Self::Refresh(MethodType::Response),
0x0114 => Self::Refresh(MethodType::Error),
0x0016 => Self::SendIndication,
0x0017 => Self::DataIndication,
_ => return Err(Error::UnknownMethod),
})
}
}
impl From<Method> for u16 {
fn from(val: Method) -> Self {
match val {
Method::Binding(MethodType::Request) => 0x0001,
Method::Binding(MethodType::Response) => 0x0101,
Method::Binding(MethodType::Error) => 0x0111,
Method::Allocate(MethodType::Request) => 0x0003,
Method::Allocate(MethodType::Response) => 0x0103,
Method::Allocate(MethodType::Error) => 0x0113,
Method::CreatePermission(MethodType::Request) => 0x0008,
Method::CreatePermission(MethodType::Response) => 0x0108,
Method::CreatePermission(MethodType::Error) => 0x0118,
Method::ChannelBind(MethodType::Request) => 0x0009,
Method::ChannelBind(MethodType::Response) => 0x0109,
Method::ChannelBind(MethodType::Error) => 0x0119,
Method::Refresh(MethodType::Request) => 0x0004,
Method::Refresh(MethodType::Response) => 0x0104,
Method::Refresh(MethodType::Error) => 0x0114,
Method::SendIndication => 0x0016,
Method::DataIndication => 0x0017,
}
}
}