ctaphid-types 0.2.0

Data types for the CTAPHID protocol
Documentation
// Copyright (C) 2021-2022 Robin Krahl <robin.krahl@ireas.org>
// SPDX-License-Identifier: Apache-2.0 or MIT

use core::fmt;

/// A CTAPHID channel.
///
/// See [ยง 11.2.3 of the CTAP specification][spec].
///
/// [spec]: https://fidoalliance.org/specs/fido-v2.1-ps-20210615/fido-client-to-authenticator-protocol-v2.1-ps-20210615.html#usb-channels
#[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)]
pub struct Channel(u32);

impl Channel {
    /// The broadcast channel (`0xffff_ffff`).
    pub const BROADCAST: Channel = Channel(0xffff_ffff);

    /// Encodes the channel as a byte array.
    pub fn to_bytes(self) -> [u8; 4] {
        self.into()
    }
}

impl From<u32> for Channel {
    fn from(id: u32) -> Self {
        Self(id)
    }
}

impl From<Channel> for u32 {
    fn from(channel: Channel) -> u32 {
        channel.0
    }
}

impl From<[u8; 4]> for Channel {
    fn from(data: [u8; 4]) -> Self {
        Self::from(u32::from_be_bytes(data))
    }
}

impl From<&[u8; 4]> for Channel {
    fn from(data: &[u8; 4]) -> Self {
        Self::from(*data)
    }
}

impl From<Channel> for [u8; 4] {
    fn from(channel: Channel) -> Self {
        channel.0.to_be_bytes()
    }
}

impl fmt::Display for Channel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "0x{:x}", self.0)
    }
}

#[cfg(test)]
mod test {
    use super::Channel;

    impl quickcheck::Arbitrary for Channel {
        fn arbitrary(g: &mut quickcheck::Gen) -> Self {
            Self(u32::arbitrary(g))
        }

        fn shrink(&self) -> Box<dyn Iterator<Item = Self>> {
            Box::new(self.0.shrink().map(Self))
        }
    }

    quickcheck::quickcheck! {
        fn from_u32(value: u32) -> bool {
            u32::from(Channel::from(value)) == value
        }
    }
}