use le_stream::{FromLeStream, ToLeStream};
use num_traits::FromPrimitive;
use crate::Error;
use crate::ember::Status;
use crate::ember::network::Parameters;
use crate::ember::node::Type;
use crate::error::ValueError;
use crate::frame::Parameter;
use crate::frame::responds_with::RespondsWith;
const ID: u16 = 0x0028;
#[derive(Clone, Debug, Eq, PartialEq, ToLeStream)]
pub(crate) struct Command;
impl Parameter for Command {
const ID: u16 = ID;
}
impl RespondsWith for Command {
type Response = Response;
}
#[derive(Clone, Debug, Eq, PartialEq, FromLeStream)]
pub struct Response {
status: u8,
node_type: u8,
parameters: Parameters,
}
impl Response {
pub fn status(&self) -> Result<Status, u8> {
Status::from_u8(self.status).ok_or(self.status)
}
pub fn node_type(&self) -> Result<Type, u8> {
Type::from_u8(self.node_type).ok_or(self.node_type)
}
#[must_use]
pub const fn parameters(&self) -> &Parameters {
&self.parameters
}
#[must_use]
pub const fn into_parameters(self) -> Parameters {
self.parameters
}
}
impl Parameter for Response {
const ID: u16 = ID;
}
impl TryFrom<Response> for (Type, Parameters) {
type Error = Error;
fn try_from(response: Response) -> Result<Self, Self::Error> {
match Status::from_u8(response.status).ok_or(response.status) {
Ok(Status::Success) => Type::from_u8(response.node_type)
.ok_or_else(|| ValueError::EmberNodeType(response.node_type).into())
.map(|node_type| (node_type, response.parameters)),
other => Err(other.into()),
}
}
}