Skip to content

Commit

Permalink
clippy, fmt and error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Jan 7, 2024
1 parent c32a6ba commit 7d54232
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 23 deletions.
30 changes: 24 additions & 6 deletions src/uu/env/src/parse_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,30 @@ use crate::raw_string_parser;
/// An error returned when string arg splitting fails.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ParseError {
MissingClosingQuote { pos: usize, c: char },
InvalidBackslashAtEndOfStringInMinusS { pos: usize, quoting: String },
BackslashCNotAllowedInDoubleQuotes { pos: usize },
InvalidSequenceBackslashXInMinusS { pos: usize, c: char },
ParsingOfVariableNameFailed { pos: usize, sub_err: subst::Error },
InternalError { pos: usize, message: String, sub_err: raw_string_parser::Error },
MissingClosingQuote {
pos: usize,
c: char,
},
InvalidBackslashAtEndOfStringInMinusS {
pos: usize,
quoting: String,
},
BackslashCNotAllowedInDoubleQuotes {
pos: usize,
},
InvalidSequenceBackslashXInMinusS {
pos: usize,
c: char,
},
ParsingOfVariableNameFailed {
pos: usize,
sub_err: subst::Error,
},
InternalError {
pos: usize,
message: String,
sub_err: raw_string_parser::Error,
},
}

impl fmt::Display for ParseError {
Expand Down
3 changes: 2 additions & 1 deletion src/uu/env/src/raw_string_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub enum Error {
NoAsciiBoundary,
NoAsciiChar,
NoAsciiCharInput,
EndOfInput,
InternalError,
}

Expand Down Expand Up @@ -62,7 +63,7 @@ impl<'a> RawStringParser<'a> {
if let Some(c) = c {
Ok(*c)
} else {
Err(Error::NoAsciiChar)
Err(Error::EndOfInput)
}
}

Expand Down
15 changes: 10 additions & 5 deletions src/uu/env/src/split_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,11 @@ impl<'a> SplitIterator<'a> {
Ok(())
}

fn map_internal_error<T, S>(&self, result: Result<T, raw_string_parser::Error>, msg: S) -> Result<T, ParseError>
fn map_internal_error<T, S>(
&self,
result: Result<T, raw_string_parser::Error>,
msg: S,
) -> Result<T, ParseError>
where
std::string::String: From<S>,
{
Expand Down Expand Up @@ -213,9 +217,10 @@ impl<'a> SplitIterator<'a> {
},
DelimiterBackslash => match c {
None => {
self.push_ascii_char_to_word(b'\\')?;
self.push_word_to_words()?;
break;
return Err(ParseError::InvalidBackslashAtEndOfStringInMinusS {
pos: self.raw_parser.get_look_at_pos(),
quoting: "Delimiter".into(),
})
}
Some(b'_') => {
self.skip_one()?;
Expand Down Expand Up @@ -269,7 +274,7 @@ impl<'a> SplitIterator<'a> {
None => {
return Err(ParseError::InvalidBackslashAtEndOfStringInMinusS {
pos: self.raw_parser.get_look_at_pos(),
quoting: "None".into(),
quoting: "Unquoted".into(),
})
}
Some(b'\n') => {
Expand Down
46 changes: 38 additions & 8 deletions tests/by-util/test_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,27 @@ mod tests_split_iterator {

#[test]
fn split_trailing_backslash() {
split_ok(&[("\\", &["\\"]), (" \\", &["\\"]), ("a\\", &["a\\"])]);
assert_eq!(
split("\\"),
Err(ParseError::InvalidBackslashAtEndOfStringInMinusS {
pos: 1,
quoting: "Delimiter".into()
})
);
assert_eq!(
split(" \\"),
Err(ParseError::InvalidBackslashAtEndOfStringInMinusS {
pos: 2,
quoting: "Delimiter".into()
})
);
assert_eq!(
split("a\\"),
Err(ParseError::InvalidBackslashAtEndOfStringInMinusS {
pos: 2,
quoting: "Unquoted".into()
})
);
}

#[test]
Expand Down Expand Up @@ -795,8 +815,8 @@ mod tests_split_iterator {

#[test]
fn test_join() {
assert_eq!(join(&["a", "b", "c"]), "a b c");
assert_eq!(join(&[" ", "$", "\n"]), "' ' '$' '\n'");
assert_eq!(join(["a", "b", "c"]), "a b c");
assert_eq!(join([" ", "$", "\n"]), "' ' '$' '\n'");
}

#[test]
Expand All @@ -819,6 +839,7 @@ mod tests_split_iterator {
}

mod test_raw_string_parser {

#[test]
fn test_ascii_only_take_one_look_at_correct_data_and_end_behavior() {
let input = "hello";
Expand All @@ -827,7 +848,10 @@ mod test_raw_string_parser {
assert_eq!(input.as_bytes().get(i).unwrap(), &uut.look_at().unwrap());
uut.take_one().unwrap();
}
assert_eq!(uut.look_at(), Err(()));
assert_eq!(
uut.look_at(),
Err(env::raw_string_parser::Error::EndOfInput)
);
uut.take_one().unwrap_err();
uut.take_one().unwrap_err();
assert_eq!(uut.take_collected_output().unwrap(), input);
Expand All @@ -838,7 +862,7 @@ mod test_raw_string_parser {
#[test]
fn test_multi_byte_codes_take_one_look_at_correct_data_and_end_behavior() {
let input = "🦉🦉🦉x🦉🦉x🦉x🦉🦉🦉🦉";
let first_byte_of_owl = "🦉".as_bytes().get(0).unwrap().clone();
let first_byte_of_owl = *"🦉".as_bytes().first().unwrap();
let mut uut = env::raw_string_parser::RawStringParser::new(input);
for _i in 0..3 {
assert_eq!(uut.look_at().unwrap(), first_byte_of_owl);
Expand All @@ -848,7 +872,10 @@ mod test_raw_string_parser {
}
assert_eq!(uut.look_at().unwrap(), first_byte_of_owl);
uut.take_one().unwrap();
assert_eq!(uut.look_at(), Err(()));
assert_eq!(
uut.look_at(),
Err(env::raw_string_parser::Error::EndOfInput)
);
uut.take_one().unwrap_err();
uut.take_one().unwrap_err();
assert_eq!(uut.take_collected_output().unwrap(), input);
Expand All @@ -859,7 +886,7 @@ mod test_raw_string_parser {
#[test]
fn test_multi_byte_codes_put_one_ascii_start_middle_end_try_invalid_ascii() {
let input = "🦉🦉🦉x🦉🦉x🦉x🦉🦉🦉🦉";
let first_byte_of_owl = "🦉".as_bytes().get(0).unwrap().clone();
let first_byte_of_owl = *"🦉".as_bytes().first().unwrap();
let mut uut = env::raw_string_parser::RawStringParser::new(input);
uut.put_one_ascii(b'a').unwrap();
for _i in 0..3 {
Expand All @@ -874,7 +901,10 @@ mod test_raw_string_parser {
uut.take_one().unwrap();
uut.put_one_ascii(first_byte_of_owl).unwrap_err();
uut.put_one_ascii(b'a').unwrap();
assert_eq!(uut.look_at(), Err(()));
assert_eq!(
uut.look_at(),
Err(env::raw_string_parser::Error::EndOfInput)
);
uut.take_one().unwrap_err();
uut.put_one_ascii(b'a').unwrap();
uut.take_one().unwrap_err();
Expand Down
6 changes: 3 additions & 3 deletions tests/by-util/test_head.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ fn test_read_backwards_bytes_proc_fs_version() {

let args = ["-c", "-1", "/proc/version"];
let result = ts.ucmd().args(&args).succeeds();
assert!(result.stdout().len() > 0);
assert!(!result.stdout().is_empty());
}

#[cfg(all(
Expand All @@ -404,7 +404,7 @@ fn test_read_backwards_bytes_proc_fs_modules() {

let args = ["-c", "-1", "/proc/modules"];
let result = ts.ucmd().args(&args).succeeds();
assert!(result.stdout().len() > 0);
assert!(!result.stdout().is_empty());
}

#[cfg(all(
Expand All @@ -418,7 +418,7 @@ fn test_read_backwards_lines_proc_fs_modules() {

let args = ["--lines", "-1", "/proc/modules"];
let result = ts.ucmd().args(&args).succeeds();
assert!(result.stdout().len() > 0);
assert!(!result.stdout().is_empty());
}

#[cfg(all(
Expand Down

0 comments on commit 7d54232

Please sign in to comment.