Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
cre4ture committed Feb 23, 2024
1 parent 7b69cf1 commit b788ce9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 14 deletions.
7 changes: 5 additions & 2 deletions src/uu/env/src/split_iterator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@

#![forbid(unsafe_code)]

use std::borrow::Cow;
use std::ffi::OsStr;
use std::ffi::OsString;

use crate::native_int_str::from_native_int_representation;
use crate::native_int_str::from_native_int_representation_owned;
use crate::native_int_str::to_native_int_representation;
use crate::native_int_str::NativeCharInt;
Expand Down Expand Up @@ -103,14 +105,15 @@ impl<'a> SplitIterator<'a> {

let (name, default) = var_parse.parse_variable()?;

let value = std::env::var_os(name);
let varname_os_str_cow = from_native_int_representation(Cow::Borrowed(name));
let value = std::env::var_os(varname_os_str_cow);
match (&value, default) {
(None, None) => {} // do nothing, just replace it with ""
(Some(value), _) => {
self.expander.put_string(value);
}
(None, Some(default)) => {
self.expander.put_string(default);
self.expander.put_native_string(default);
}
};

Expand Down
4 changes: 4 additions & 0 deletions src/uu/env/src/string_expander.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ impl<'a> StringExpander<'a> {
self.output.extend(native.deref());
}

pub fn put_native_string(&mut self, n_str: &NativeIntStr) {
self.output.extend(n_str);
}

pub fn take_collected_output(&mut self) -> Vec<NativeCharInt> {
mem::take(&mut self.output)
}
Expand Down
20 changes: 8 additions & 12 deletions src/uu/env/src/variable_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.

Check failure on line 5 in src/uu/env/src/variable_parser.rs

View workflow job for this annotation

GitHub Actions / Style/format (ubuntu-latest, feat_os_unix)

ERROR: `cargo fmt`: style violation (file:'src/uu/env/src/variable_parser.rs', line:5; use `cargo fmt -- "src/uu/env/src/variable_parser.rs"`)
use std::{borrow::Cow, ffi::OsStr, ops::Range};
use std::ops::Range;

use crate::{
native_int_str::from_native_int_representation, parse_error::ParseError,
native_int_str::NativeIntStr, parse_error::ParseError,
string_parser::StringParser,
};

Expand Down Expand Up @@ -37,7 +37,7 @@ impl<'a, 'b> VariableParser<'a, 'b> {

fn parse_braced_variable_name(
&mut self,
) -> Result<(Cow<'a, OsStr>, Option<Cow<'a, OsStr>>), ParseError> {
) -> Result<(&'a NativeIntStr, Option<&'a NativeIntStr>), ParseError> {
let pos_start = self.parser.get_peek_position();

self.check_variable_name_start()?;
Expand Down Expand Up @@ -88,7 +88,7 @@ impl<'a, 'b> VariableParser<'a, 'b> {
};
}

let default = if let Some(default_end) = default_end {
let default_opt = if let Some(default_end) = default_end {
Some(self.parser.substring(&Range {
start: varname_end + 1,
end: default_end,
Expand All @@ -102,13 +102,10 @@ impl<'a, 'b> VariableParser<'a, 'b> {
end: varname_end,
});

let varname_cow = from_native_int_representation(Cow::Borrowed(varname));
let default_opt_cow = default.map(|x| from_native_int_representation(Cow::Borrowed(x)));

Ok((varname_cow, default_opt_cow))
Ok((varname, default_opt))
}

fn parse_unbraced_variable_name(&mut self) -> Result<Cow<'a, OsStr>, ParseError> {
fn parse_unbraced_variable_name(&mut self) -> Result<&'a NativeIntStr, ParseError> {
let pos_start = self.parser.get_peek_position();

self.check_variable_name_start()?;
Expand Down Expand Up @@ -136,14 +133,13 @@ impl<'a, 'b> VariableParser<'a, 'b> {
start: pos_start,
end: pos_end,
});
let varname_cow = Cow::Borrowed(varname);

Ok(from_native_int_representation(varname_cow))
Ok(varname)
}

pub fn parse_variable(
&mut self,
) -> Result<(Cow<'a, OsStr>, Option<Cow<'a, OsStr>>), ParseError> {
) -> Result<(&'a NativeIntStr, Option<&'a NativeIntStr>), ParseError> {
self.skip_one()?;

let (name, default) = match self.get_current_char() {
Expand Down

0 comments on commit b788ce9

Please sign in to comment.