From 18634c10c9d5ad39f2076a6a17794074787818ad Mon Sep 17 00:00:00 2001 From: Ulrich Hornung Date: Sun, 18 Feb 2024 17:08:51 +0100 Subject: [PATCH] cleanup --- src/uu/env/src/env.rs | 4 ++-- src/uu/env/src/split_iterator.rs | 15 +++++++-------- src/uu/env/src/string_expander.rs | 10 ++++------ src/uu/env/src/string_parser.rs | 10 +++++----- src/uu/env/src/variable_parser.rs | 6 +++--- tests/by-util/test_env.rs | 23 +++++++++++++---------- 6 files changed, 34 insertions(+), 34 deletions(-) diff --git a/src/uu/env/src/env.rs b/src/uu/env/src/env.rs index 2626c301ede..19afab1fc75 100644 --- a/src/uu/env/src/env.rs +++ b/src/uu/env/src/env.rs @@ -6,9 +6,9 @@ // spell-checker:ignore (ToDO) chdir execvp progname subcommand subcommands unsets setenv putenv spawnp SIGSEGV SIGBUS sigaction pub mod parse_error; -pub mod string_parser; -pub mod string_expander; pub mod split_iterator; +pub mod string_expander; +pub mod string_parser; pub mod variable_parser; use clap::builder::ValueParser; diff --git a/src/uu/env/src/split_iterator.rs b/src/uu/env/src/split_iterator.rs index 46929f2d34c..f444e69addb 100644 --- a/src/uu/env/src/split_iterator.rs +++ b/src/uu/env/src/split_iterator.rs @@ -22,7 +22,7 @@ use std::ffi::OsStr; use std::ffi::OsString; use crate::parse_error::ParseError; -use crate::string_parser::RawStringExpander; +use crate::string_expander::StringExpander; use crate::string_parser::StringParser; use crate::variable_parser::VariableParser; @@ -67,14 +67,14 @@ const REPLACEMENTS: [(char, char); 9] = [ const ASCII_WHITESPACE_CHARS: [char; 6] = [' ', '\t', '\r', '\n', '\x0B', '\x0C']; pub struct SplitIterator<'a> { - raw_parser: RawStringExpander<'a>, + raw_parser: StringExpander<'a>, words: Vec, } impl<'a> SplitIterator<'a> { pub fn new + ?Sized>(s: &'a S) -> Self { Self { - raw_parser: RawStringExpander::new(s.as_ref()), + raw_parser: StringExpander::new(s.as_ref()), words: Vec::::new(), } } @@ -93,7 +93,7 @@ impl<'a> SplitIterator<'a> { } fn push_char_to_word(&mut self, c: char) { - self.raw_parser.put_one_char(c) + self.raw_parser.put_one_char(c); } fn push_word_to_words(&mut self) { @@ -109,10 +109,9 @@ impl<'a> SplitIterator<'a> { self.raw_parser.get_parser_mut() } - fn substitute_variable(&mut self) -> Result<(), ParseError> - { - let mut var_parse = VariableParser::<'a, '_>{ - parser: self.get_parser_mut() + fn substitute_variable(&mut self) -> Result<(), ParseError> { + let mut var_parse = VariableParser::<'a, '_> { + parser: self.get_parser_mut(), }; let (name, default) = var_parse.parse_variable()?; diff --git a/src/uu/env/src/string_expander.rs b/src/uu/env/src/string_expander.rs index d96c014dcca..42984dfea0b 100644 --- a/src/uu/env/src/string_expander.rs +++ b/src/uu/env/src/string_expander.rs @@ -3,13 +3,13 @@ // For the full copyright and license information, please view the LICENSE // file that was distributed with this source code. -use std::{ffi::{OsStr, OsString}, mem}; +use std::{ + ffi::{OsStr, OsString}, + mem, +}; use crate::string_parser::{Chunk, Error, StringParser}; - - - /// This class makes parsing and word collection more convenient. /// /// It manages an "output" buffer that is automatically filled. @@ -21,8 +21,6 @@ pub struct StringExpander<'a> { output: OsString, } - - impl<'a> StringExpander<'a> { pub fn new + ?Sized>(input: &'a S) -> Self { Self { diff --git a/src/uu/env/src/string_parser.rs b/src/uu/env/src/string_parser.rs index 04338963dcb..09e96378ab4 100644 --- a/src/uu/env/src/string_parser.rs +++ b/src/uu/env/src/string_parser.rs @@ -6,10 +6,7 @@ // spell-checker:ignore (words) splitted #![forbid(unsafe_code)] -use std::{ - ffi::{OsStr, OsString}, - mem, -}; +use std::ffi::OsStr; use os_str_bytes::OsStrBytesExt; @@ -109,7 +106,10 @@ impl<'a> StringParser<'a> { } pub fn look_at_chunk(&self) -> Option> { - return self.get_chunk_with_length_at(self.pointer).ok().map(|(chunk, _)| chunk); + return self + .get_chunk_with_length_at(self.pointer) + .ok() + .map(|(chunk, _)| chunk); } pub fn consume_one(&mut self) -> Result, Error> { diff --git a/src/uu/env/src/variable_parser.rs b/src/uu/env/src/variable_parser.rs index dbc4b44d225..f784e0e2382 100644 --- a/src/uu/env/src/variable_parser.rs +++ b/src/uu/env/src/variable_parser.rs @@ -8,13 +8,13 @@ use std::{ffi::OsStr, ops::Range}; use crate::{parse_error::ParseError, string_parser::StringParser}; pub struct VariableParser<'a, 'b> - where 'a : 'b +where + 'a: 'b, { - pub parser: &'b mut StringParser<'a> + pub parser: &'b mut StringParser<'a>, } impl<'a, 'b> VariableParser<'a, 'b> { - fn get_current_char(&self) -> Option { self.parser.look_at().ok() } diff --git a/tests/by-util/test_env.rs b/tests/by-util/test_env.rs index 52b3635cb10..3a370c20075 100644 --- a/tests/by-util/test_env.rs +++ b/tests/by-util/test_env.rs @@ -910,15 +910,18 @@ mod tests_split_iterator { } mod test_raw_string_parser { - use std::{ffi::{OsStr, OsString}, os::unix::ffi::OsStringExt}; + use std::{ + ffi::{OsStr, OsString}, + os::unix::ffi::OsStringExt, + }; - use env::string_parser; + use env::{string_expander::StringExpander, string_parser}; use os_str_bytes::OsStrBytesExt; #[test] fn test_ascii_only_take_one_look_at_correct_data_and_end_behavior() { let input = "hello"; - let mut uut = env::string_parser::RawStringExpander::new(&input); + let mut uut = StringExpander::new(&input); for c in input.chars() { assert_eq!(c, uut.get_parser().look_at().unwrap()); uut.take_one().unwrap(); @@ -941,7 +944,7 @@ mod test_raw_string_parser { fn test_multi_byte_codes_take_one_look_at_correct_data_and_end_behavior() { let input = OsString::from("🦉🦉🦉x🦉🦉x🦉x🦉🦉🦉🦉"); let owl: char = '🦉'; - let mut uut = env::string_parser::RawStringExpander::new(&input); + let mut uut = StringExpander::new(&input); for _i in 0..3 { assert_eq!(uut.get_parser().look_at().unwrap(), owl); uut.take_one().unwrap(); @@ -968,7 +971,7 @@ mod test_raw_string_parser { fn test_multi_byte_codes_put_one_ascii_start_middle_end_try_invalid_ascii() { let input = OsString::from("🦉🦉🦉x🦉🦉x🦉x🦉🦉🦉🦉"); let owl: char = '🦉'; - let mut uut = env::string_parser::RawStringExpander::new(&input); + let mut uut = StringExpander::new(&input); uut.put_one_char('a'); for _i in 0..3 { assert_eq!(uut.get_parser().look_at().unwrap(), owl); @@ -1003,7 +1006,7 @@ mod test_raw_string_parser { #[test] fn test_multi_byte_codes_skip_one_take_one_skip_until_ascii_char_or_end() { let input = OsString::from("🦉🦉🦉x🦉🦉x🦉x🦉🦉🦉🦉"); - let mut uut = env::string_parser::RawStringExpander::new(&input); + let mut uut = StringExpander::new(&input); uut.skip_one().unwrap(); // skip 🦉🦉🦉 assert_eq!(uut.get_look_at_pos(), 12); @@ -1028,7 +1031,7 @@ mod test_raw_string_parser { #[test] fn test_multi_byte_codes_skip_multiple_ascii_bounded_good_and_bad() { let input = OsString::from("🦉🦉🦉x🦉🦉x🦉x🦉🦉🦉🦉"); - let mut uut = env::string_parser::RawStringExpander::new(&input); + let mut uut = StringExpander::new(&input); uut.get_parser_mut().skip_multiple_ascii_bounded(0); assert_eq!(uut.get_look_at_pos(), 0); @@ -1052,7 +1055,7 @@ mod test_raw_string_parser { #[test] fn test_multi_byte_codes_put_string_utf8_start_middle_end() { let input = OsString::from("🦉🦉🦉x🦉🦉x🦉x🦉🦉🦉🦉"); - let mut uut = env::string_parser::RawStringExpander::new(&input); + let mut uut = StringExpander::new(&input); uut.put_string("🦔oo"); uut.take_one().unwrap(); // takes 🦉🦉🦉 @@ -1068,7 +1071,7 @@ mod test_raw_string_parser { #[test] fn test_multi_byte_codes_look_at_remaining_start_middle_end() { let input = "🦉🦉🦉x🦉🦉x🦉x🦉🦉🦉🦉"; - let mut uut = env::string_parser::RawStringExpander::new(&input); + let mut uut = StringExpander::new(&input); assert_eq!(uut.get_parser().look_at_remaining(), input); uut.take_one().unwrap(); // takes 🦉🦉🦉 @@ -1088,7 +1091,7 @@ mod test_raw_string_parser { let owl_b = "🦉".bytes().next().unwrap(); let input_u8 = [b'<', owl_b, b'>']; let input_str = OsString::from_vec(input_u8.to_vec()); - let mut uut = env::string_parser::RawStringExpander::new(&input_str); + let mut uut = StringExpander::new(&input_str); assert_eq!(uut.get_parser().look_at_remaining(), input_str); assert_eq!(uut.get_parser().look_at().unwrap(), '<');