|
6 | 6 | use clap::{crate_version, Arg, ArgAction, Command}; |
7 | 7 | use regex::Regex; |
8 | 8 | 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 | +}; |
10 | 13 |
|
11 | 14 | mod json; |
12 | 15 |
|
@@ -74,7 +77,12 @@ impl Dmesg<'_> { |
74 | 77 | let lines = self.read_lines_from_kmsg_file()?; |
75 | 78 | for line in lines { |
76 | 79 | 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 | + )?); |
78 | 86 | } |
79 | 87 | } |
80 | 88 | self.records = Some(records); |
@@ -128,18 +136,18 @@ struct Record { |
128 | 136 | } |
129 | 137 |
|
130 | 138 | 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> { |
132 | 140 | let pri_fac = str::parse(pri_fac); |
133 | 141 | let seq = str::parse(seq); |
134 | 142 | let time = str::parse(time); |
135 | 143 | match (pri_fac, seq, time) { |
136 | | - (Ok(pri_fac), Ok(seq), Ok(time)) => Record { |
| 144 | + (Ok(pri_fac), Ok(seq), Ok(time)) => Ok(Record { |
137 | 145 | priority_facility: pri_fac, |
138 | 146 | _sequence: seq, |
139 | 147 | timestamp_us: time, |
140 | 148 | message: msg, |
141 | | - }, |
142 | | - _ => panic!("parse error."), |
| 149 | + }), |
| 150 | + _ => Err(USimpleError::new(1, "Failed to parse record field(s)")), |
143 | 151 | } |
144 | 152 | } |
145 | 153 | } |
0 commit comments