use crate::parser::{ByteOrder, Parser};
use crate::{Error, Head};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Signature {
None,
Mbr([u8; 4]),
Gpt([u8; 16]),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Format {
Mbr,
Gpt,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct HardDrive {
pub number: u32,
pub start: u64,
pub size: u64,
pub format: Format,
pub signature: Signature,
}
impl<'a> TryFrom<Head<'a>> for HardDrive {
type Error = Error;
fn try_from(mut node: Head<'a>) -> Result<Self, Self::Error> {
let number = node.data.parse(ByteOrder::Little)?;
let start = node.data.parse(ByteOrder::Little)?;
let size = node.data.parse(ByteOrder::Little)?;
let guid: [u8; 16] = node.data.parse(())?;
let format = match node.data.parse(())? {
1u8 => Format::Mbr,
2u8 => Format::Gpt,
_ => return Err(Error::Invalid),
};
let signature = match node.data.finish(())? {
0u8 => Signature::None,
1u8 => Signature::Mbr([guid[0], guid[1], guid[2], guid[3]]),
2u8 => Signature::Gpt(guid),
_ => return Err(Error::Invalid),
};
Ok(Self {
number,
start,
size,
format,
signature,
})
}
}