faster release parsing
Some checks are pending
Rust / build (push) Waiting to run

Around 20% faster when you think about it, but results vary because of hardware race conditions.
This commit is contained in:
raf 2024-12-09 17:29:32 +03:00
parent 8f5bfcbd05
commit 3960b37089
Signed by: NotAShelf
GPG key ID: AF26552424E53993

View file

@ -2,7 +2,7 @@ use color_eyre::Result;
use nix::sys::utsname::UtsName; use nix::sys::utsname::UtsName;
use std::{ use std::{
fs::File, fs::File,
io::{self, Read}, io::{self, BufRead, BufReader},
}; };
pub fn get_system_info(utsname: &UtsName) -> nix::Result<String> { pub fn get_system_info(utsname: &UtsName) -> nix::Result<String> {
@ -15,13 +15,15 @@ pub fn get_system_info(utsname: &UtsName) -> nix::Result<String> {
} }
pub fn get_os_pretty_name() -> Result<String, io::Error> { pub fn get_os_pretty_name() -> Result<String, io::Error> {
let mut os_release_content = String::with_capacity(1024); let file = File::open("/etc/os-release")?;
File::open("/etc/os-release")?.read_to_string(&mut os_release_content)?; let reader = BufReader::new(file);
let pretty_name = os_release_content for line in reader.lines() {
.lines() let line = line?;
.find(|line| line.starts_with("PRETTY_NAME=")) if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") {
.map(|line| line.trim_start_matches("PRETTY_NAME=").trim_matches('"')); return Ok(pretty_name.trim_matches('"').to_string());
}
}
Ok(pretty_name.unwrap_or("Unknown").to_string()) Ok("Unknown".to_string())
} }