mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-29 12:07:46 +00:00
Merge pull request #2703 from thomasqueirozb/base_uresult
base32 + base64 + basenc: use UResult
This commit is contained in:
commit
368fa54520
6 changed files with 85 additions and 96 deletions
|
@ -11,7 +11,7 @@ extern crate uucore;
|
|||
use std::io::{stdin, Read};
|
||||
|
||||
use clap::App;
|
||||
use uucore::encoding::Format;
|
||||
use uucore::{encoding::Format, error::UResult};
|
||||
|
||||
pub mod base_common;
|
||||
|
||||
|
@ -24,27 +24,22 @@ static ABOUT: &str = "
|
|||
to attempt to recover from any other non-alphabet bytes in the
|
||||
encoded stream.
|
||||
";
|
||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
static BASE_CMD_PARSE_ERROR: i32 = 1;
|
||||
|
||||
fn usage() -> String {
|
||||
format!("{0} [OPTION]... [FILE]", uucore::execution_phrase())
|
||||
}
|
||||
|
||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||
#[uucore_procs::gen_uumain]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let format = Format::Base32;
|
||||
let usage = usage();
|
||||
let name = uucore::util_name();
|
||||
|
||||
let config_result: Result<base_common::Config, String> =
|
||||
base_common::parse_base_cmd_args(args, name, VERSION, ABOUT, &usage);
|
||||
let config = config_result.unwrap_or_else(|s| crash!(BASE_CMD_PARSE_ERROR, "{}", s));
|
||||
let config: base_common::Config = base_common::parse_base_cmd_args(args, ABOUT, &usage)?;
|
||||
|
||||
// Create a reference to stdin so we can return a locked stdin from
|
||||
// parse_base_cmd_args
|
||||
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(
|
||||
&mut input,
|
||||
|
@ -52,12 +47,9 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
config.wrap_cols,
|
||||
config.ignore_garbage,
|
||||
config.decode,
|
||||
name,
|
||||
);
|
||||
|
||||
0
|
||||
)
|
||||
}
|
||||
|
||||
pub fn uu_app() -> App<'static, 'static> {
|
||||
base_common::base_app(uucore::util_name(), VERSION, ABOUT)
|
||||
base_common::base_app(ABOUT)
|
||||
}
|
||||
|
|
|
@ -11,13 +11,16 @@ use std::io::{stdout, Read, Write};
|
|||
|
||||
use uucore::display::Quotable;
|
||||
use uucore::encoding::{wrap_print, Data, Format};
|
||||
use uucore::error::{FromIo, UResult, USimpleError, UUsageError};
|
||||
use uucore::InvalidEncodingHandling;
|
||||
|
||||
use std::fs::File;
|
||||
use std::io::{BufReader, Stdin};
|
||||
use std::path::Path;
|
||||
|
||||
use clap::{App, Arg};
|
||||
use clap::{crate_version, App, Arg};
|
||||
|
||||
pub static BASE_CMD_PARSE_ERROR: i32 = 1;
|
||||
|
||||
// Config.
|
||||
pub struct Config {
|
||||
|
@ -35,15 +38,14 @@ pub mod options {
|
|||
}
|
||||
|
||||
impl Config {
|
||||
pub fn from(app_name: &str, options: &clap::ArgMatches) -> Result<Config, String> {
|
||||
pub fn from(options: &clap::ArgMatches) -> UResult<Config> {
|
||||
let file: Option<String> = match options.values_of(options::FILE) {
|
||||
Some(mut values) => {
|
||||
let name = values.next().unwrap();
|
||||
if let Some(extra_op) = values.next() {
|
||||
return Err(format!(
|
||||
"extra operand {}\nTry '{} --help' for more information.",
|
||||
extra_op.quote(),
|
||||
app_name
|
||||
return Err(UUsageError::new(
|
||||
BASE_CMD_PARSE_ERROR,
|
||||
format!("extra operand {}", extra_op.quote(),),
|
||||
));
|
||||
}
|
||||
|
||||
|
@ -51,7 +53,10 @@ impl Config {
|
|||
None
|
||||
} else {
|
||||
if !Path::exists(Path::new(name)) {
|
||||
return Err(format!("{}: No such file or directory", name.maybe_quote()));
|
||||
return Err(USimpleError::new(
|
||||
BASE_CMD_PARSE_ERROR,
|
||||
format!("{}: No such file or directory", name.maybe_quote()),
|
||||
));
|
||||
}
|
||||
Some(name.to_owned())
|
||||
}
|
||||
|
@ -62,8 +67,12 @@ impl Config {
|
|||
let cols = options
|
||||
.value_of(options::WRAP)
|
||||
.map(|num| {
|
||||
num.parse::<usize>()
|
||||
.map_err(|_| format!("invalid wrap size: {}", num.quote()))
|
||||
num.parse::<usize>().map_err(|_| {
|
||||
USimpleError::new(
|
||||
BASE_CMD_PARSE_ERROR,
|
||||
format!("invalid wrap size: {}", num.quote()),
|
||||
)
|
||||
})
|
||||
})
|
||||
.transpose()?;
|
||||
|
||||
|
@ -76,23 +85,17 @@ impl Config {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn parse_base_cmd_args(
|
||||
args: impl uucore::Args,
|
||||
name: &str,
|
||||
version: &str,
|
||||
about: &str,
|
||||
usage: &str,
|
||||
) -> Result<Config, String> {
|
||||
let app = base_app(name, version, about).usage(usage);
|
||||
pub fn parse_base_cmd_args(args: impl uucore::Args, about: &str, usage: &str) -> UResult<Config> {
|
||||
let app = base_app(about).usage(usage);
|
||||
let arg_list = args
|
||||
.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
.accept_any();
|
||||
Config::from(name, &app.get_matches_from(arg_list))
|
||||
Config::from(&app.get_matches_from(arg_list))
|
||||
}
|
||||
|
||||
pub fn base_app<'a>(name: &str, version: &'a str, about: &'a str) -> App<'static, 'a> {
|
||||
App::new(name)
|
||||
.version(version)
|
||||
pub fn base_app<'a>(about: &'a str) -> App<'static, 'a> {
|
||||
App::new(uucore::util_name())
|
||||
.version(crate_version!())
|
||||
.about(about)
|
||||
// Format arguments.
|
||||
.arg(
|
||||
|
@ -121,14 +124,15 @@ pub fn base_app<'a>(name: &str, version: &'a str, about: &'a str) -> App<'static
|
|||
.arg(Arg::with_name(options::FILE).index(1).multiple(true))
|
||||
}
|
||||
|
||||
pub fn get_input<'a>(config: &Config, stdin_ref: &'a Stdin) -> Box<dyn Read + 'a> {
|
||||
pub fn get_input<'a>(config: &Config, stdin_ref: &'a Stdin) -> UResult<Box<dyn Read + 'a>> {
|
||||
match &config.to_read {
|
||||
Some(name) => {
|
||||
let file_buf = crash_if_err!(1, File::open(Path::new(name)));
|
||||
Box::new(BufReader::new(file_buf)) // as Box<dyn Read>
|
||||
let file_buf =
|
||||
File::open(Path::new(name)).map_err_context(|| name.maybe_quote().to_string())?;
|
||||
Ok(Box::new(BufReader::new(file_buf))) // as Box<dyn Read>
|
||||
}
|
||||
None => {
|
||||
Box::new(stdin_ref.lock()) // as Box<dyn Read>
|
||||
Ok(Box::new(stdin_ref.lock())) // as Box<dyn Read>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -139,8 +143,7 @@ pub fn handle_input<R: Read>(
|
|||
line_wrap: Option<usize>,
|
||||
ignore_garbage: bool,
|
||||
decode: bool,
|
||||
name: &str,
|
||||
) {
|
||||
) -> UResult<()> {
|
||||
let mut data = Data::new(input, format).ignore_garbage(ignore_garbage);
|
||||
if let Some(wrap) = line_wrap {
|
||||
data = data.line_wrap(wrap);
|
||||
|
@ -150,28 +153,23 @@ pub fn handle_input<R: Read>(
|
|||
match data.encode() {
|
||||
Ok(s) => {
|
||||
wrap_print(&data, s);
|
||||
Ok(())
|
||||
}
|
||||
Err(_) => {
|
||||
eprintln!(
|
||||
"{}: error: invalid input (length must be multiple of 4 characters)",
|
||||
name
|
||||
);
|
||||
exit!(1)
|
||||
}
|
||||
Err(_) => Err(USimpleError::new(
|
||||
1,
|
||||
"error: invalid input (length must be multiple of 4 characters)",
|
||||
)),
|
||||
}
|
||||
} else {
|
||||
match data.decode() {
|
||||
Ok(s) => {
|
||||
if stdout().write_all(&s).is_err() {
|
||||
// on windows console, writing invalid utf8 returns an error
|
||||
eprintln!("{}: error: Cannot write non-utf8 data", name);
|
||||
exit!(1)
|
||||
return Err(USimpleError::new(1, "error: cannot write non-utf8 data"));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Err(_) => {
|
||||
eprintln!("{}: error: invalid input", name);
|
||||
exit!(1)
|
||||
}
|
||||
Err(_) => Err(USimpleError::new(1, "error: invalid input")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,7 +12,7 @@ extern crate uucore;
|
|||
use uu_base32::base_common;
|
||||
pub use uu_base32::uu_app;
|
||||
|
||||
use uucore::encoding::Format;
|
||||
use uucore::{encoding::Format, error::UResult};
|
||||
|
||||
use std::io::{stdin, Read};
|
||||
|
||||
|
@ -25,26 +25,22 @@ static ABOUT: &str = "
|
|||
to attempt to recover from any other non-alphabet bytes in the
|
||||
encoded stream.
|
||||
";
|
||||
static VERSION: &str = env!("CARGO_PKG_VERSION");
|
||||
|
||||
static BASE_CMD_PARSE_ERROR: i32 = 1;
|
||||
|
||||
fn usage() -> String {
|
||||
format!("{0} [OPTION]... [FILE]", uucore::execution_phrase())
|
||||
}
|
||||
|
||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||
#[uucore_procs::gen_uumain]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let format = Format::Base64;
|
||||
let usage = usage();
|
||||
let name = uucore::util_name();
|
||||
let config_result: Result<base_common::Config, String> =
|
||||
base_common::parse_base_cmd_args(args, name, VERSION, ABOUT, &usage);
|
||||
let config = config_result.unwrap_or_else(|s| crash!(BASE_CMD_PARSE_ERROR, "{}", s));
|
||||
|
||||
let config: base_common::Config = base_common::parse_base_cmd_args(args, ABOUT, &usage)?;
|
||||
|
||||
// Create a reference to stdin so we can return a locked stdin from
|
||||
// parse_base_cmd_args
|
||||
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(
|
||||
&mut input,
|
||||
|
@ -52,8 +48,5 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
config.wrap_cols,
|
||||
config.ignore_garbage,
|
||||
config.decode,
|
||||
name,
|
||||
);
|
||||
|
||||
0
|
||||
)
|
||||
}
|
||||
|
|
|
@ -11,10 +11,14 @@
|
|||
#[macro_use]
|
||||
extern crate uucore;
|
||||
|
||||
use clap::{crate_version, App, Arg};
|
||||
use uu_base32::base_common::{self, Config};
|
||||
use clap::{App, Arg};
|
||||
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};
|
||||
|
||||
|
@ -26,8 +30,6 @@ static ABOUT: &str = "
|
|||
from any other non-alphabet bytes in the encoded stream.
|
||||
";
|
||||
|
||||
static BASE_CMD_PARSE_ERROR: i32 = 1;
|
||||
|
||||
const ENCODINGS: &[(&str, Format)] = &[
|
||||
("base64", Format::Base64),
|
||||
("base64url", Format::Base64Url),
|
||||
|
@ -47,14 +49,14 @@ fn usage() -> String {
|
|||
}
|
||||
|
||||
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 {
|
||||
app = app.arg(Arg::with_name(encoding.0).long(encoding.0));
|
||||
}
|
||||
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 matches = uu_app().usage(&usage[..]).get_matches_from(
|
||||
args.collect_str(InvalidEncodingHandling::ConvertLossy)
|
||||
|
@ -63,24 +65,19 @@ fn parse_cmd_args(args: impl uucore::Args) -> (Config, Format) {
|
|||
let format = ENCODINGS
|
||||
.iter()
|
||||
.find(|encoding| matches.is_present(encoding.0))
|
||||
.unwrap_or_else(|| {
|
||||
show_usage_error!("missing encoding type");
|
||||
std::process::exit(1)
|
||||
})
|
||||
.ok_or_else(|| UUsageError::new(BASE_CMD_PARSE_ERROR, "missing encoding type"))?
|
||||
.1;
|
||||
(
|
||||
Config::from("basenc", &matches).unwrap_or_else(|s| crash!(BASE_CMD_PARSE_ERROR, "{}", s)),
|
||||
format,
|
||||
)
|
||||
let config = Config::from(&matches)?;
|
||||
Ok((config, format))
|
||||
}
|
||||
|
||||
pub fn uumain(args: impl uucore::Args) -> i32 {
|
||||
let name = uucore::util_name();
|
||||
let (config, format) = parse_cmd_args(args);
|
||||
#[uucore_procs::gen_uumain]
|
||||
pub fn uumain(args: impl uucore::Args) -> UResult<()> {
|
||||
let (config, format) = parse_cmd_args(args)?;
|
||||
// Create a reference to stdin so we can return a locked stdin from
|
||||
// parse_base_cmd_args
|
||||
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(
|
||||
&mut input,
|
||||
|
@ -88,8 +85,5 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
config.wrap_cols,
|
||||
config.ignore_garbage,
|
||||
config.decode,
|
||||
name,
|
||||
);
|
||||
|
||||
0
|
||||
)
|
||||
}
|
||||
|
|
|
@ -113,12 +113,18 @@ fn test_wrap_bad_arg() {
|
|||
|
||||
#[test]
|
||||
fn test_base32_extra_operand() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
|
||||
// Expect a failure when multiple files are specified.
|
||||
new_ucmd!()
|
||||
ts.ucmd()
|
||||
.arg("a.txt")
|
||||
.arg("b.txt")
|
||||
.fails()
|
||||
.stderr_only("base32: extra operand 'b.txt'\nTry 'base32 --help' for more information.");
|
||||
.stderr_only(format!(
|
||||
"{0}: extra operand 'b.txt'\nTry '{1} {0} --help' for more information.",
|
||||
ts.util_name,
|
||||
ts.bin_path.to_string_lossy()
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -95,12 +95,18 @@ fn test_wrap_bad_arg() {
|
|||
|
||||
#[test]
|
||||
fn test_base64_extra_operand() {
|
||||
let ts = TestScenario::new(util_name!());
|
||||
|
||||
// Expect a failure when multiple files are specified.
|
||||
new_ucmd!()
|
||||
ts.ucmd()
|
||||
.arg("a.txt")
|
||||
.arg("b.txt")
|
||||
.fails()
|
||||
.stderr_only("base64: extra operand 'b.txt'\nTry 'base64 --help' for more information.");
|
||||
.stderr_only(format!(
|
||||
"{0}: extra operand 'b.txt'\nTry '{1} {0} --help' for more information.",
|
||||
ts.util_name,
|
||||
ts.bin_path.to_string_lossy()
|
||||
));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue