//! Parameters for the [`Configuration::set_policy`](crate::Configuration::set_policy) command.

use num_traits::FromPrimitive;

use crate::Error;
use crate::error::ValueError;
use crate::ezsp::{Status, decision, policy};
crate::frame::parameters::frame!(
    0x0056,
    { policy_id: u8 },
    impl {
        impl Command {
            /// Creates command parameters.
            #[must_use]
            pub fn new(policy_id: policy::Id) -> Self {
                Self {
                    policy_id: policy_id.into(),
                }
            }
        }
    },
    { status: u8, decision_id: u8 } => Configuration(configuration)::GetPolicy,
    impl {
        /// Converts the response into a [`decision::Id`] or an appropriate [`Error`] depending on its status.
        impl TryFrom<Response> for decision::Id {
            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) => Self::from_u8(response.decision_id)
                        .ok_or_else(|| ValueError::DecisionId(response.decision_id).into()),
                    other => Err(other.into()),
                }
            }
        }
    }
);
