//! A handle module

use {
    crate::arch::hardware_random_u64,
    core::fmt::{self, Formatter},
};
/// An operating system handle without permissions attached
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
pub struct OSHandle {
    id: u64,
}

impl OSHandle {
    /// turn a u64 into an OSHandle
    pub fn new_from_u64(id: u64) -> Self {
        Self { id }
    }
    /// Generate a new OSHandle using the HAL random function
    pub fn random_new() -> Self {
        Self {
            id: hardware_random_u64(),
        }
    }
}
/// A handle for resources
#[derive(Debug, Eq, Hash, PartialEq, Clone, Copy)]
pub struct Handle {
    id:    OSHandle,
    // TODO: Update this to be indexes into the caps
    perms: Permissions,
}

impl Handle {
    /// make a new handle
    pub fn new() -> Handle {
        Handle {
            id:    OSHandle::random_new(),
            perms: Permissions::new(),
        }
    }
    /// represent the os handle as a u64
    pub fn as_u64(&self) -> u64 {
        self.id.id
    }
}

impl fmt::Display for Handle {
    fn fmt(&self, w: &mut Formatter<'_>) -> Result<(), core::fmt::Error> {
        write!(w, "{:?}", self.id)?;
        Ok(())
    }
}

#[derive(PartialEq, Hash, Eq, Debug, Clone, Copy)]
struct Permissions {
    edit_children:   bool,
    edit_attributes: bool,
}
impl Permissions {
    fn new() -> Self {
        Self {
            edit_children:   true,
            edit_attributes: true,
        }
    }
}