use alloc::string::String;
use crate::parser::{Format, Invalid, Parser};
use crate::{Error, Head};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum NamespaceId {
Eui64([u8; 8]),
Nguid([u8; 16]),
Uuid([u8; 16]),
Csi(u8),
}
impl Parser<NamespaceId> for &[u8] {
type Arg = ();
fn parse(&mut self, _arg: Self::Arg) -> Result<NamespaceId, Invalid> {
let nidt = self.parse(())?;
let nid: [u8; 16] = self.parse(())?;
match nidt {
0x01u8 => {
let mut eui64 = [0u8; 8];
eui64.copy_from_slice(&nid[..8]);
Ok(NamespaceId::Eui64(eui64))
}
0x02u8 => Ok(NamespaceId::Nguid(nid)),
0x03u8 => Ok(NamespaceId::Uuid(nid)),
0x04u8 => Ok(NamespaceId::Csi(nid[0])),
_ => Err(Invalid),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct NvmeOfNamespace {
pub nid: NamespaceId,
pub nqn: String,
}
impl TryFrom<Head<'_>> for NvmeOfNamespace {
type Error = Error;
fn try_from(mut node: Head<'_>) -> Result<Self, Self::Error> {
Ok(Self {
nid: node.data.parse(())?,
nqn: node.data.finish(Format::Utf8(None))?,
})
}
}