Skip to content

Commit 371ab5e

Browse files
committed
faster release parsing
Around 20% faster when you think about it, but results vary becaus of hardware race conditions.
1 parent 8f5bfcb commit 371ab5e

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/release.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use color_eyre::Result;
22
use nix::sys::utsname::UtsName;
33
use std::{
44
fs::File,
5-
io::{self, Read},
5+
io::{self, BufRead, BufReader},
66
};
77

88
pub fn get_system_info(utsname: &UtsName) -> nix::Result<String> {
@@ -15,13 +15,15 @@ pub fn get_system_info(utsname: &UtsName) -> nix::Result<String> {
1515
}
1616

1717
pub fn get_os_pretty_name() -> Result<String, io::Error> {
18-
let mut os_release_content = String::with_capacity(1024);
19-
File::open("/etc/os-release")?.read_to_string(&mut os_release_content)?;
18+
let file = File::open("/etc/os-release")?;
19+
let reader = BufReader::new(file);
2020

21-
let pretty_name = os_release_content
22-
.lines()
23-
.find(|line| line.starts_with("PRETTY_NAME="))
24-
.map(|line| line.trim_start_matches("PRETTY_NAME=").trim_matches('"'));
21+
for line in reader.lines() {
22+
let line = line?;
23+
if let Some(pretty_name) = line.strip_prefix("PRETTY_NAME=") {
24+
return Ok(pretty_name.trim_matches('"').to_string());
25+
}
26+
}
2527

26-
Ok(pretty_name.unwrap_or("Unknown").to_string())
28+
Ok("Unknown".to_string())
2729
}

0 commit comments

Comments
 (0)