devpath 1.0.1

UEFI Device Path parsing library
Documentation
use crate::{Error, Head};

/// UEFI Device Path node types as defined in the UEFI specification
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum Type {
    /// Hardware device path type
    Hardware,

    /// ACPI device path type
    Acpi,

    /// Messaging device path type
    Messaging,

    /// Media device path type
    Media,

    /// BIOS device path type
    Bios,
}

impl Type {
    pub(crate) const fn decode(value: u8) -> Result<Self, u8> {
        match value {
            0x01 => Ok(Self::Hardware),
            0x02 => Ok(Self::Acpi),
            0x03 => Ok(Self::Messaging),
            0x04 => Ok(Self::Media),
            0x05 => Ok(Self::Bios),
            n => Err(n),
        }
    }
}

/// Device Path types as defined in UEFI specification
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Node {
    /// Hardware device path
    Hardware(crate::hw::Hardware),

    /// ACPI device path
    Acpi(crate::acpi::Acpi),

    /// Messaging device path
    Messaging(crate::msg::Messaging),

    /// Media device path
    Media(crate::media::Media),

    /// BIOS device path
    Bios(crate::bios::Bios),
}

impl TryFrom<Head<'_>> for Node {
    type Error = Error;

    fn try_from(head: Head<'_>) -> Result<Self, Self::Error> {
        match Type::decode(head.kind) {
            Ok(Type::Hardware) => TryFrom::try_from(head).map(Node::Hardware),
            Ok(Type::Acpi) => TryFrom::try_from(head).map(Node::Acpi),
            Ok(Type::Messaging) => TryFrom::try_from(head).map(Node::Messaging),
            Ok(Type::Media) => TryFrom::try_from(head).map(Node::Media),
            Ok(Type::Bios) => TryFrom::try_from(head).map(Node::Bios),
            Err(n) => Err(Error::UnknownType(n)),
        }
    }
}