1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-30 12:37:49 +00:00

basenc: use UResult

This commit is contained in:
Thomas Queiroz 2021-10-02 23:17:16 -03:00
parent f85ccf8e46
commit b924774c8a
No known key found for this signature in database
GPG key ID: 229D2DDF7ECA5F8F

View file

@ -11,10 +11,14 @@
#[macro_use] #[macro_use]
extern crate uucore; extern crate uucore;
use clap::{crate_version, App, Arg}; use clap::{App, Arg};
use uu_base32::base_common::{self, Config}; use uu_base32::base_common::{self, Config, BASE_CMD_PARSE_ERROR};
use uucore::{encoding::Format, InvalidEncodingHandling}; use uucore::{
encoding::Format,
error::{UResult, UUsageError},
InvalidEncodingHandling,
};
use std::io::{stdin, Read}; use std::io::{stdin, Read};
@ -26,8 +30,6 @@ static ABOUT: &str = "
from any other non-alphabet bytes in the encoded stream. from any other non-alphabet bytes in the encoded stream.
"; ";
static BASE_CMD_PARSE_ERROR: i32 = 1;
const ENCODINGS: &[(&str, Format)] = &[ const ENCODINGS: &[(&str, Format)] = &[
("base64", Format::Base64), ("base64", Format::Base64),
("base64url", Format::Base64Url), ("base64url", Format::Base64Url),
@ -47,14 +49,14 @@ fn usage() -> String {
} }
pub fn uu_app() -> App<'static, 'static> { pub fn uu_app() -> App<'static, 'static> {
let mut app = base_common::base_app(uucore::util_name(), crate_version!(), ABOUT); let mut app = base_common::base_app(ABOUT);
for encoding in ENCODINGS { for encoding in ENCODINGS {
app = app.arg(Arg::with_name(encoding.0).long(encoding.0)); app = app.arg(Arg::with_name(encoding.0).long(encoding.0));
} }
app app
} }
fn parse_cmd_args(args: impl uucore::Args) -> (Config, Format) { fn parse_cmd_args(args: impl uucore::Args) -> UResult<(Config, Format)> {
let usage = usage(); let usage = usage();
let matches = uu_app().usage(&usage[..]).get_matches_from( let matches = uu_app().usage(&usage[..]).get_matches_from(
args.collect_str(InvalidEncodingHandling::ConvertLossy) args.collect_str(InvalidEncodingHandling::ConvertLossy)
@ -63,24 +65,19 @@ fn parse_cmd_args(args: impl uucore::Args) -> (Config, Format) {
let format = ENCODINGS let format = ENCODINGS
.iter() .iter()
.find(|encoding| matches.is_present(encoding.0)) .find(|encoding| matches.is_present(encoding.0))
.unwrap_or_else(|| { .ok_or_else(|| UUsageError::new(BASE_CMD_PARSE_ERROR, "missing encoding type"))?
show_usage_error!("missing encoding type");
std::process::exit(1)
})
.1; .1;
( let config = Config::from(&matches)?;
Config::from("basenc", &matches).unwrap_or_else(|s| crash!(BASE_CMD_PARSE_ERROR, "{}", s)), Ok((config, format))
format,
)
} }
pub fn uumain(args: impl uucore::Args) -> i32 { #[uucore_procs::gen_uumain]
let name = uucore::util_name(); pub fn uumain(args: impl uucore::Args) -> UResult<()> {
let (config, format) = parse_cmd_args(args); let (config, format) = parse_cmd_args(args)?;
// Create a reference to stdin so we can return a locked stdin from // Create a reference to stdin so we can return a locked stdin from
// parse_base_cmd_args // parse_base_cmd_args
let stdin_raw = stdin(); let stdin_raw = stdin();
let mut input: Box<dyn Read> = base_common::get_input(&config, &stdin_raw); let mut input: Box<dyn Read> = base_common::get_input(&config, &stdin_raw)?;
base_common::handle_input( base_common::handle_input(
&mut input, &mut input,
@ -88,8 +85,5 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
config.wrap_cols, config.wrap_cols,
config.ignore_garbage, config.ignore_garbage,
config.decode, config.decode,
name, )
);
0
} }