Skip to content

Commit 21dc55c

Browse files
committed
tsort: refactor error handling
1 parent 753f869 commit 21dc55c

3 files changed

Lines changed: 15 additions & 5 deletions

File tree

src/uu/tsort/locales/en-US.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ tsort-error-loop = input contains a loop:
99
tsort-error-extra-operand = extra operand { $operand }
1010
Try '{ $util } --help' for more information.
1111
tsort-error-at-least-one-input = at least one input
12+
tsort-error-read = read error: { $error }
13+
tsort-error-write = write error: { $error }

src/uu/tsort/locales/fr-FR.ftl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ tsort-error-loop = l'entrée contient une boucle :
99
tsort-error-extra-operand = opérande supplémentaire { $operand }
1010
Essayez '{ $util } --help' pour plus d'informations.
1111
tsort-error-at-least-one-input = au moins une entrée
12+
tsort-error-read = erreur de lecture : { $error }
13+
tsort-error-write = erreur d'écriture : { $error }

src/uu/tsort/src/tsort.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use string_interner::StringInterner;
1515
use string_interner::backend::BucketBackend;
1616
use thiserror::Error;
1717
use uucore::display::Quotable;
18-
use uucore::error::{UError, UResult, USimpleError};
18+
use uucore::error::{UError, UResult, USimpleError, strip_errno};
1919
use uucore::{format_usage, show, translate};
2020

2121
// short types for switching interning behavior on the fly.
@@ -102,7 +102,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {
102102
process_input(reader, &mut g)?;
103103
}
104104

105-
g.run_tsort().map_err(|e| USimpleError::new(1, format!("write error: {e}")))?;
105+
g.run_tsort()?;
106106
Ok(())
107107
}
108108

@@ -151,6 +151,12 @@ enum TsortError {
151151
/// Wrapper for bubbling up IO errors
152152
#[error("{0}")]
153153
IO(#[from] io::Error),
154+
/// Read error.
155+
#[error("{}", translate!("tsort-error-read", "error" => strip_errno(.0)))]
156+
Read(io::Error),
157+
/// Write error.
158+
#[error("{}", translate!("tsort-error-write", "error" => strip_errno(.0)))]
159+
Write(io::Error),
154160
}
155161

156162
// Auxiliary struct, just for printing loop nodes via show! macro
@@ -173,7 +179,7 @@ fn process_input<R: BufRead>(reader: R, graph: &mut Graph) -> Result<(), TsortEr
173179
if e.kind() == io::ErrorKind::IsADirectory {
174180
TsortError::IsDir(graph.name())
175181
} else {
176-
e.into()
182+
TsortError::Read(e)
177183
}
178184
})?;
179185
for token in line.split_whitespace() {
@@ -276,7 +282,7 @@ impl Graph {
276282
}
277283

278284
/// Implementation of algorithm T from TAOCP (Don. Knuth), vol. 1.
279-
fn run_tsort(&mut self) -> io::Result<()> {
285+
fn run_tsort(&mut self) -> Result<(), TsortError> {
280286
let mut independent_nodes_queue: VecDeque<Sym> = self
281287
.nodes
282288
.iter()
@@ -321,7 +327,7 @@ impl Graph {
321327
}
322328
}
323329

324-
res
330+
res.map_err(TsortError::Write)
325331
}
326332
pub fn indegree(&self, sym: Sym) -> Option<usize> {
327333
self.nodes.get(&sym).map(|data| data.predecessor_count)

0 commit comments

Comments
 (0)