use std::fmt;
use std::num::NonZeroU8;
use binrw::binrw;
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DeviceNumber(NonZeroU8);
impl DeviceNumber {
pub const MIN_PLAYER: u8 = 1;
pub const MAX_PLAYER: u8 = 6;
pub const MAX_BROWSABLE: u8 = 4;
pub const ONE: Self = Self(NonZeroU8::MIN);
pub const fn new(raw: u8) -> Option<Self> {
match NonZeroU8::new(raw) {
Some(number) => Some(Self(number)),
None => None,
}
}
pub const fn get(self) -> u8 {
self.0.get()
}
pub const fn is_player(self) -> bool {
self.get() >= Self::MIN_PLAYER && self.get() <= Self::MAX_PLAYER
}
pub const fn browsable(self) -> Option<BrowsableDeviceNumber> {
if self.get() <= Self::MAX_BROWSABLE {
Some(BrowsableDeviceNumber(self))
} else {
None
}
}
}
impl fmt::Debug for DeviceNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "device {}", self.get())
}
}
impl fmt::Display for DeviceNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.get())
}
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BrowsableDeviceNumber(DeviceNumber);
impl BrowsableDeviceNumber {
pub const fn new(raw: u8) -> Option<Self> {
match DeviceNumber::new(raw) {
Some(number) => number.browsable(),
None => None,
}
}
pub const fn get(self) -> u8 {
self.0.get()
}
pub const fn number(self) -> DeviceNumber {
self.0
}
pub fn descending() -> impl Iterator<Item = Self> {
(DeviceNumber::MIN_PLAYER..=DeviceNumber::MAX_BROWSABLE)
.rev()
.filter_map(Self::new)
}
}
impl From<BrowsableDeviceNumber> for DeviceNumber {
fn from(number: BrowsableDeviceNumber) -> Self {
number.0
}
}
impl fmt::Debug for BrowsableDeviceNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}
impl fmt::Display for BrowsableDeviceNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}
#[binrw]
#[brw(big)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct DeviceKind(pub u8);
impl DeviceKind {
pub const MIXER: Self = Self(0x01);
pub const CDJ: Self = Self(0x02);
pub const REKORDBOX_OR_CDJ3000: Self = Self(0x03);
pub const CDJ3000_HELLO: Self = Self(0x04);
pub fn role(self) -> u8 {
if self == Self::MIXER { 0x02 } else { 0x01 }
}
}
impl fmt::Debug for DeviceKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self {
Self::MIXER => f.write_str("mixer"),
Self::CDJ => f.write_str("cdj"),
Self::REKORDBOX_OR_CDJ3000 => f.write_str("rekordbox_or_cdj3000"),
Self::CDJ3000_HELLO => f.write_str("cdj3000_hello"),
Self(raw) => write!(f, "DeviceKind({raw:#04x})"),
}
}
}
#[binrw]
#[brw(big)]
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct MacAddress(pub [u8; 6]);
impl fmt::Display for MacAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let [o0, o1, o2, o3, o4, o5] = self.0;
write!(f, "{o0:02x}:{o1:02x}:{o2:02x}:{o3:02x}:{o4:02x}:{o5:02x}")
}
}
impl fmt::Debug for MacAddress {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self, f)
}
}
#[binrw]
#[brw(big)]
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct DeviceName(pub [u8; Self::LEN]);
impl DeviceName {
pub const LEN: usize = 20;
pub const CDJ_2000_NEXUS: &'static str = "CDJ-2000nexus";
pub fn new(name: &str) -> Self {
let mut raw = [0u8; Self::LEN];
for (slot, byte) in raw.iter_mut().zip(name.bytes()) {
*slot = byte;
}
Self(raw)
}
pub fn as_str(&self) -> String {
self.0
.iter()
.copied()
.take_while(|&byte| byte != 0)
.map(|byte| {
if byte.is_ascii() {
char::from(byte)
} else {
char::REPLACEMENT_CHARACTER
}
})
.collect()
}
}
impl Default for DeviceName {
fn default() -> Self {
Self::new(Self::CDJ_2000_NEXUS)
}
}
impl fmt::Display for DeviceName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.as_str())
}
}
impl fmt::Debug for DeviceName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn zero_is_not_a_device_number() {
assert!(DeviceNumber::new(0).is_none());
assert!(BrowsableDeviceNumber::new(0).is_none());
}
#[test]
fn five_announces_fine_and_is_never_browsed() {
let five = DeviceNumber::new(5).expect("5 is a device number");
assert!(five.is_player(), "5 is a number a real player can hold");
assert!(
five.browsable().is_none(),
"but a peer will never browse it (F45)"
);
}
#[test]
fn browsable_numbers_come_out_highest_first() {
let numbers: Vec<u8> = BrowsableDeviceNumber::descending()
.map(BrowsableDeviceNumber::get)
.collect();
assert_eq!(
numbers,
vec![4, 3, 2, 1],
"leave 1 and 2 alone as long as possible"
);
}
#[test]
fn a_name_keeps_its_padding() {
let name = DeviceName::new(DeviceName::CDJ_2000_NEXUS);
assert_eq!(name.as_str(), "CDJ-2000nexus");
assert_eq!(name.0, *b"CDJ-2000nexus\0\0\0\0\0\0\0");
}
#[test]
fn a_name_longer_than_the_field_is_truncated() {
let name = DeviceName::new("a name far longer than twenty bytes");
assert_eq!(name.as_str(), "a name far longer th");
}
}