use super::{GptConfig, GptDisk};
use std::{convert::TryFrom, fmt, io, path};
pub const DEFAULT_SECTOR_SIZE: LogicalBlockSize = LogicalBlockSize::Lb512;
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum LogicalBlockSize {
Lb512,
Lb4096,
}
impl From<LogicalBlockSize> for u64 {
fn from(lb: LogicalBlockSize) -> u64 {
match lb {
LogicalBlockSize::Lb512 => 512,
LogicalBlockSize::Lb4096 => 4096,
}
}
}
impl From<LogicalBlockSize> for usize {
fn from(lb: LogicalBlockSize) -> usize {
match lb {
LogicalBlockSize::Lb512 => 512,
LogicalBlockSize::Lb4096 => 4096,
}
}
}
impl TryFrom<u64> for LogicalBlockSize {
type Error = io::Error;
fn try_from(v: u64) -> Result<Self, Self::Error> {
match v {
512 => Ok(LogicalBlockSize::Lb512),
4096 => Ok(LogicalBlockSize::Lb4096),
_ => Err(io::Error::new(
io::ErrorKind::Other,
"unsupported logical block size (must be 512 or 4096)"
)),
}
}
}
impl fmt::Display for LogicalBlockSize {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
LogicalBlockSize::Lb512 => write!(f, "512"),
LogicalBlockSize::Lb4096 => write!(f, "4096"),
}
}
}
pub fn read_disk(diskpath: impl AsRef<path::Path>) -> io::Result<GptDisk<'static>> {
let cfg = GptConfig::new();
cfg.open(diskpath)
}