mirror of
https://github.com/RGBCube/uutils-coreutils
synced 2025-07-30 12:37:49 +00:00
uucore(memo): refactor error propogation with new SubError enum
This commit is contained in:
parent
1e12c46c24
commit
f04f22b012
1 changed files with 33 additions and 43 deletions
|
@ -5,8 +5,10 @@
|
||||||
//! it is created by Sub's implementation of the Tokenizer trait
|
//! it is created by Sub's implementation of the Tokenizer trait
|
||||||
//! Subs which have numeric field chars make use of the num_format
|
//! Subs which have numeric field chars make use of the num_format
|
||||||
//! submodule
|
//! submodule
|
||||||
use crate::error::{UResult, UUsageError};
|
use crate::error::{UError, UResult};
|
||||||
use itertools::{put_back_n, PutBackN};
|
use itertools::{put_back_n, PutBackN};
|
||||||
|
use std::error::Error;
|
||||||
|
use std::fmt::Display;
|
||||||
use std::iter::Peekable;
|
use std::iter::Peekable;
|
||||||
use std::process::exit;
|
use std::process::exit;
|
||||||
use std::slice::Iter;
|
use std::slice::Iter;
|
||||||
|
@ -20,6 +22,23 @@ use super::unescaped_text::UnescapedText;
|
||||||
|
|
||||||
const EXIT_ERR: i32 = 1;
|
const EXIT_ERR: i32 = 1;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum SubError {
|
||||||
|
InvalidSpec(String),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Display for SubError {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
|
||||||
|
match self {
|
||||||
|
Self::InvalidSpec(s) => write!(f, "%{}: invalid conversion specification", s),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Error for SubError {}
|
||||||
|
|
||||||
|
impl UError for SubError {}
|
||||||
|
|
||||||
fn convert_asterisk_arg_int(asterisk_arg: &str) -> isize {
|
fn convert_asterisk_arg_int(asterisk_arg: &str) -> isize {
|
||||||
// this is a costly way to parse the
|
// this is a costly way to parse the
|
||||||
// args used for asterisk values into integers
|
// args used for asterisk values into integers
|
||||||
|
@ -172,10 +191,7 @@ impl SubParser {
|
||||||
'-' | '*' | '0'..='9' => {
|
'-' | '*' | '0'..='9' => {
|
||||||
if !self.past_decimal {
|
if !self.past_decimal {
|
||||||
if self.min_width_is_asterisk || self.specifiers_found {
|
if self.min_width_is_asterisk || self.specifiers_found {
|
||||||
return Err(UUsageError::new(
|
return Err(SubError::InvalidSpec(self.text_so_far.clone()).into());
|
||||||
1,
|
|
||||||
format!("%{}: invalid conversion specification", &self.text_so_far),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
if self.min_width_tmp.is_none() {
|
if self.min_width_tmp.is_none() {
|
||||||
self.min_width_tmp = Some(String::new());
|
self.min_width_tmp = Some(String::new());
|
||||||
|
@ -183,13 +199,9 @@ impl SubParser {
|
||||||
match self.min_width_tmp.as_mut() {
|
match self.min_width_tmp.as_mut() {
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
if (ch == '-' || ch == '*') && !x.is_empty() {
|
if (ch == '-' || ch == '*') && !x.is_empty() {
|
||||||
return Err(UUsageError::new(
|
return Err(
|
||||||
1,
|
SubError::InvalidSpec(self.text_so_far.clone()).into()
|
||||||
format!(
|
);
|
||||||
"%{}: invalid conversion specification",
|
|
||||||
&self.text_so_far
|
|
||||||
),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
if ch == '*' {
|
if ch == '*' {
|
||||||
self.min_width_is_asterisk = true;
|
self.min_width_is_asterisk = true;
|
||||||
|
@ -204,10 +216,7 @@ impl SubParser {
|
||||||
// second field should never have a
|
// second field should never have a
|
||||||
// negative value
|
// negative value
|
||||||
if self.second_field_is_asterisk || ch == '-' || self.specifiers_found {
|
if self.second_field_is_asterisk || ch == '-' || self.specifiers_found {
|
||||||
return Err(UUsageError::new(
|
return Err(SubError::InvalidSpec(self.text_so_far.clone()).into());
|
||||||
1,
|
|
||||||
format!("%{}: invalid conversion specification", &self.text_so_far),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
if self.second_field_tmp.is_none() {
|
if self.second_field_tmp.is_none() {
|
||||||
self.second_field_tmp = Some(String::new());
|
self.second_field_tmp = Some(String::new());
|
||||||
|
@ -215,13 +224,9 @@ impl SubParser {
|
||||||
match self.second_field_tmp.as_mut() {
|
match self.second_field_tmp.as_mut() {
|
||||||
Some(x) => {
|
Some(x) => {
|
||||||
if ch == '*' && !x.is_empty() {
|
if ch == '*' && !x.is_empty() {
|
||||||
return Err(UUsageError::new(
|
return Err(
|
||||||
1,
|
SubError::InvalidSpec(self.text_so_far.clone()).into()
|
||||||
format!(
|
);
|
||||||
"%{}: invalid conversion specification",
|
|
||||||
&self.text_so_far
|
|
||||||
),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
if ch == '*' {
|
if ch == '*' {
|
||||||
self.second_field_is_asterisk = true;
|
self.second_field_is_asterisk = true;
|
||||||
|
@ -238,10 +243,7 @@ impl SubParser {
|
||||||
if !self.past_decimal {
|
if !self.past_decimal {
|
||||||
self.past_decimal = true;
|
self.past_decimal = true;
|
||||||
} else {
|
} else {
|
||||||
return Err(UUsageError::new(
|
return Err(SubError::InvalidSpec(self.text_so_far.clone()).into());
|
||||||
1,
|
|
||||||
format!("%{}: invalid conversion specification", &self.text_so_far),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
x if legal_fields.binary_search(&x).is_ok() => {
|
x if legal_fields.binary_search(&x).is_ok() => {
|
||||||
|
@ -258,18 +260,12 @@ impl SubParser {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
return Err(UUsageError::new(
|
return Err(SubError::InvalidSpec(self.text_so_far.clone()).into());
|
||||||
1,
|
|
||||||
format!("%{}: invalid conversion specification", &self.text_so_far),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.field_char.is_none() {
|
if self.field_char.is_none() {
|
||||||
return Err(UUsageError::new(
|
return Err(SubError::InvalidSpec(self.text_so_far.clone()).into());
|
||||||
1,
|
|
||||||
format!("%{}: invalid conversion specification", &self.text_so_far),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
let field_char_retrieved = self.field_char.unwrap();
|
let field_char_retrieved = self.field_char.unwrap();
|
||||||
if self.past_decimal && self.second_field_tmp.is_none() {
|
if self.past_decimal && self.second_field_tmp.is_none() {
|
||||||
|
@ -303,10 +299,7 @@ impl SubParser {
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
text_so_far.push('%');
|
text_so_far.push('%');
|
||||||
return Err(UUsageError::new(
|
Err(SubError::InvalidSpec(text_so_far.clone()).into())
|
||||||
1,
|
|
||||||
format!("%{}: invalid conversion specification", &text_so_far[..]),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -334,10 +327,7 @@ impl SubParser {
|
||||||
// invalid string substitution
|
// invalid string substitution
|
||||||
// to do: include information about an invalid
|
// to do: include information about an invalid
|
||||||
// string substitution
|
// string substitution
|
||||||
return Err(UUsageError::new(
|
return Err(SubError::InvalidSpec(self.text_so_far.clone()).into());
|
||||||
1,
|
|
||||||
format!("%{}: invalid conversion specification", &self.text_so_far),
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue