#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Parse the SETUP packet of control transfers
#[derive(Clone, Copy, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct SetupPacket {
    /// bmRequestType
    pub request_type: u8,
    /// bRequest
    pub request: u8,
    /// wValue
    pub value: u16,
    /// wIndex
    pub index: u16,
    /// wLength
    pub length: u16,
}

impl SetupPacket {
    /// Parse a [SetupPacket] from raw setup packet
    pub fn parse(setup: &[u8; 8]) -> SetupPacket {
        SetupPacket {
            request_type: setup[0],
            request: setup[1],
            value: ((setup[3] as u16) << 8) | (setup[2] as u16),
            index: ((setup[5] as u16) << 8) | (setup[4] as u16),
            length: ((setup[7] as u16) << 8) | (setup[6] as u16),
        }
    }

    pub fn is_setup(&self) -> bool {
        self.request_type != 0
            || self.request != 0
            || self.value != 0
            || self.index != 0
            || self.length != 0
    }
}

// pub fn is_setup(setup: &[u8; 8]) -> bool {
//     setup.iter().any(|b| *b != 0)
// }
