#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct LinuxKernelVersion
{
pub release: Box<[u8]>,
pub timestamp: Box<[u8]>,
pub full: Box<[u8]>,
}
impl LinuxKernelVersion
{
#[inline(always)]
pub fn major_minor_revision(&self) -> LinuxKernelVersionNumber
{
let left = self.release.split_bytes_n(2, b'-').next().unwrap();
let mut parts = left.split_bytes_n(3, b'.');
let major = u16::parse_decimal_number(parts.next().unwrap()).unwrap();
let minor = u16::parse_decimal_number(parts.next().unwrap()).unwrap();
let revision = u16::parse_decimal_number(parts.next().unwrap()).unwrap();
LinuxKernelVersionNumber
{
major,
minor,
revision,
}
}
#[inline(always)]
fn verify_ostype(proc_path: &ProcPath) -> io::Result<()>
{
let type_ = proc_path.sys_kernel_file_path("ostype").read_raw_without_line_feed()?;
if &type_[..] == b"Linux"
{
Ok(())
}
else
{
Err(io::Error::new(ErrorKind::InvalidData, format!("This is not Linux but {:?}", type_)))
}
}
#[inline(always)]
pub fn parse(proc_path: &ProcPath) -> io::Result<Self>
{
Self::verify_ostype(proc_path)?;
Ok
(
Self
{
release: proc_path.sys_kernel_file_path("osrelease").read_raw_without_line_feed()?,
timestamp: proc_path.sys_kernel_file_path("version").read_raw_without_line_feed()?,
full: proc_path.file_path("version").read_raw_without_line_feed()?
}
)
}
}