use std::fmt::Display;

const ID_BYTES: usize = 6;

#[derive(Debug, Clone, Ord, Eq, PartialEq, PartialOrd)]
pub struct HandleID(String);

impl HandleID {
    pub(crate) fn new_random() -> Self {
        Self(generate_short_id())
    }
}

impl Display for HandleID {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.0)
    }
}

impl From<&str> for HandleID {
    fn from(value: &str) -> Self {
        Self(value.to_string())
    }
}

impl From<String> for HandleID {
    fn from(value: String) -> Self {
        Self(value)
    }
}

pub(crate) fn generate_short_id() -> String {
    let bytes: [u8; ID_BYTES] = rand::random();
    bytes.iter().map(|b| format!("{b:02x}")).collect()
}
