Skip to content

Commit d796cb5

Browse files
committed
dmesg: return UResult in Record constructor instead of panic.
1 parent df3fb06 commit d796cb5

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/uu/dmesg/src/dmesg.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66
use clap::{crate_version, Arg, ArgAction, Command};
77
use regex::Regex;
88
use std::fs;
9-
use uucore::{error::FromIo, error::UResult, format_usage, help_about, help_usage};
9+
use uucore::{
10+
error::{FromIo, UResult, USimpleError},
11+
format_usage, help_about, help_usage,
12+
};
1013

1114
mod json;
1215

@@ -74,7 +77,12 @@ impl Dmesg<'_> {
7477
let lines = self.read_lines_from_kmsg_file()?;
7578
for line in lines {
7679
for (_, [pri_fac, seq, time, msg]) in re.captures_iter(&line).map(|c| c.extract()) {
77-
records.push(Record::from_str_fields(pri_fac, seq, time, msg.to_string()));
80+
records.push(Record::from_str_fields(
81+
pri_fac,
82+
seq,
83+
time,
84+
msg.to_string(),
85+
)?);
7886
}
7987
}
8088
self.records = Some(records);
@@ -128,18 +136,18 @@ struct Record {
128136
}
129137

130138
impl Record {
131-
fn from_str_fields(pri_fac: &str, seq: &str, time: &str, msg: String) -> Record {
139+
fn from_str_fields(pri_fac: &str, seq: &str, time: &str, msg: String) -> UResult<Record> {
132140
let pri_fac = str::parse(pri_fac);
133141
let seq = str::parse(seq);
134142
let time = str::parse(time);
135143
match (pri_fac, seq, time) {
136-
(Ok(pri_fac), Ok(seq), Ok(time)) => Record {
144+
(Ok(pri_fac), Ok(seq), Ok(time)) => Ok(Record {
137145
priority_facility: pri_fac,
138146
_sequence: seq,
139147
timestamp_us: time,
140148
message: msg,
141-
},
142-
_ => panic!("parse error."),
149+
}),
150+
_ => Err(USimpleError::new(1, "Failed to parse record field(s)")),
143151
}
144152
}
145153
}

0 commit comments

Comments
 (0)