1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

Merge pull request #2835 from jfinkels/tsort-uresult

tsort: return UResult from uumain() function
This commit is contained in:
Sylvestre Ledru 2022-01-07 21:51:25 +01:00 committed by GitHub
commit 480bd3c36a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -5,16 +5,13 @@
// * // *
// * For the full copyright and license information, please view the LICENSE // * For the full copyright and license information, please view the LICENSE
// * file that was distributed with this source code. // * file that was distributed with this source code.
#[macro_use]
extern crate uucore;
use clap::{crate_version, App, Arg}; use clap::{crate_version, App, Arg};
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::fs::File; use std::fs::File;
use std::io::{stdin, BufRead, BufReader, Read}; use std::io::{stdin, BufRead, BufReader, Read};
use std::path::Path; use std::path::Path;
use uucore::display::Quotable; use uucore::display::Quotable;
use uucore::error::{FromIo, UResult, USimpleError};
use uucore::InvalidEncodingHandling; use uucore::InvalidEncodingHandling;
static SUMMARY: &str = "Topological sort the strings in FILE. static SUMMARY: &str = "Topological sort the strings in FILE.
@ -26,7 +23,8 @@ mod options {
pub const FILE: &str = "file"; pub const FILE: &str = "file";
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let args = args let args = args
.collect_str(InvalidEncodingHandling::ConvertLossy) .collect_str(InvalidEncodingHandling::ConvertLossy)
.accept_any(); .accept_any();
@ -43,13 +41,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
stdin_buf = stdin(); stdin_buf = stdin();
&mut stdin_buf as &mut dyn Read &mut stdin_buf as &mut dyn Read
} else { } else {
file_buf = match File::open(Path::new(&input)) { file_buf = File::open(Path::new(&input)).map_err_context(|| input.to_string())?;
Ok(a) => a,
_ => {
show_error!("{}: No such file or directory", input.maybe_quote());
return 1;
}
};
&mut file_buf as &mut dyn Read &mut file_buf as &mut dyn Read
}); });
@ -69,11 +61,15 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
for ab in tokens.chunks(2) { for ab in tokens.chunks(2) {
match ab.len() { match ab.len() {
2 => g.add_edge(&ab[0], &ab[1]), 2 => g.add_edge(&ab[0], &ab[1]),
_ => crash!( _ => {
1, return Err(USimpleError::new(
"{}: input contains an odd number of tokens", 1,
input.maybe_quote() format!(
), "{}: input contains an odd number of tokens",
input.maybe_quote()
),
))
}
} }
} }
} }
@ -84,14 +80,17 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
g.run_tsort(); g.run_tsort();
if !g.is_acyclic() { if !g.is_acyclic() {
crash!(1, "{}, input contains a loop:", input); return Err(USimpleError::new(
1,
format!("{}, input contains a loop:", input),
));
} }
for x in &g.result { for x in &g.result {
println!("{}", x); println!("{}", x);
} }
0 Ok(())
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {