1
0
mirror of https://github.com/Llewellynvdm/starship.git synced 2024-11-28 15:56:28 +00:00

refactor: simplify iterator usage (#3017)

This commit is contained in:
Marcin Puc 2021-09-02 17:48:29 +02:00 committed by GitHub
parent 3933553486
commit 6598275e15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 21 additions and 26 deletions

View File

@ -141,7 +141,7 @@ where
fn from_config(config: &'a Value) -> Option<Self> { fn from_config(config: &'a Value) -> Option<Self> {
let mut hm = Self::default(); let mut hm = Self::default();
for (x, y) in config.as_table()?.iter() { for (x, y) in config.as_table()? {
hm.insert(x.clone(), T::from_config(y)?); hm.insert(x.clone(), T::from_config(y)?);
} }
@ -157,7 +157,7 @@ where
fn from_config(config: &'a Value) -> Option<Self> { fn from_config(config: &'a Value) -> Option<Self> {
let mut im = Self::default(); let mut im = Self::default();
for (x, y) in config.as_table()?.iter() { for (x, y) in config.as_table()? {
im.insert(x.clone(), T::from_config(y)?); im.insert(x.clone(), T::from_config(y)?);
} }

View File

@ -122,7 +122,7 @@ impl<'a> ModuleConfig<'a> for StarshipRootConfig<'a> {
"env_var", "env_var",
] ]
.iter() .iter()
.chain(ALL_MODULES.iter()) .chain(ALL_MODULES)
.filter_map(|field| { .filter_map(|field| {
let score = strsim::jaro_winkler(unknown, field); let score = strsim::jaro_winkler(unknown, field);
(score > 0.8).then(|| (score, field)) (score > 0.8).then(|| (score, field))

View File

@ -253,7 +253,7 @@ fn real_path<P: AsRef<Path>>(path: P) -> PathBuf {
/// substitutions, in order, on the path. Any non-pair of strings is ignored. /// substitutions, in order, on the path. Any non-pair of strings is ignored.
fn substitute_path(dir_string: String, substitutions: &IndexMap<String, &str>) -> String { fn substitute_path(dir_string: String, substitutions: &IndexMap<String, &str>) -> String {
let mut substituted_dir = dir_string; let mut substituted_dir = dir_string;
for substitution_pair in substitutions.iter() { for substitution_pair in substitutions {
substituted_dir = substituted_dir.replace(substitution_pair.0, substitution_pair.1); substituted_dir = substituted_dir.replace(substitution_pair.0, substitution_pair.1);
} }
substituted_dir substituted_dir

View File

@ -186,7 +186,7 @@ fn try_find_nearby_global_json(current_dir: &Path, repo_root: Option<&Path>) ->
// Check the parent directory, or otherwise the repository root, for a global.json // Check the parent directory, or otherwise the repository root, for a global.json
let mut check_dirs = parent_dir let mut check_dirs = parent_dir
.iter() .iter()
.chain(repo_root.iter()) .chain(&repo_root)
.copied() // Copies the reference, not the Path itself .copied() // Copies the reference, not the Path itself
.collect::<Vec<&Path>>(); .collect::<Vec<&Path>>();

View File

@ -429,7 +429,7 @@ users: []
.path(dir.path()) .path(dir.path())
.env( .env(
"KUBECONFIG", "KUBECONFIG",
env::join_paths([&filename_cc, &filename_ctx].iter()) env::join_paths([&filename_cc, &filename_ctx])
.unwrap() .unwrap()
.to_string_lossy(), .to_string_lossy(),
) )
@ -444,7 +444,7 @@ users: []
.path(dir.path()) .path(dir.path())
.env( .env(
"KUBECONFIG", "KUBECONFIG",
env::join_paths([&filename_ctx, &filename_cc].iter()) env::join_paths([&filename_ctx, &filename_cc])
.unwrap() .unwrap()
.to_string_lossy(), .to_string_lossy(),
) )

View File

@ -287,7 +287,7 @@ mod tests {
None, None,
]; ];
for (status, name) in exit_values.iter().zip(exit_values_name.iter()) { for (status, name) in exit_values.iter().zip(&exit_values_name) {
let expected = name.map(std::string::ToString::to_string); let expected = name.map(std::string::ToString::to_string);
let actual = ModuleRenderer::new("status") let actual = ModuleRenderer::new("status")
.config(toml::toml! { .config(toml::toml! {
@ -314,7 +314,7 @@ mod tests {
None, None,
]; ];
for (status, name) in exit_values.iter().zip(exit_values_name.iter()) { for (status, name) in exit_values.iter().zip(&exit_values_name) {
let expected = name.map(std::string::ToString::to_string); let expected = name.map(std::string::ToString::to_string);
let actual = ModuleRenderer::new("status") let actual = ModuleRenderer::new("status")
.config(toml::toml! { .config(toml::toml! {
@ -343,7 +343,7 @@ mod tests {
Some("-3"), Some("-3"),
]; ];
for (status, name) in exit_values.iter().zip(exit_values_name.iter()) { for (status, name) in exit_values.iter().zip(&exit_values_name) {
let expected = name.map(std::string::ToString::to_string); let expected = name.map(std::string::ToString::to_string);
let actual = ModuleRenderer::new("status") let actual = ModuleRenderer::new("status")
.config(toml::toml! { .config(toml::toml! {
@ -362,7 +362,7 @@ mod tests {
let exit_values = [1, 126, 127, 130, 131]; let exit_values = [1, 126, 127, 130, 131];
let exit_values_name = ["🔴", "🚫", "🔍", "🧱", ""]; let exit_values_name = ["🔴", "🚫", "🔍", "🧱", ""];
for (status, name) in exit_values.iter().zip(exit_values_name.iter()) { for (status, name) in exit_values.iter().zip(&exit_values_name) {
let expected = Some((*name).to_string()); let expected = Some((*name).to_string());
let actual = ModuleRenderer::new("status") let actual = ModuleRenderer::new("status")
.config(toml::toml! { .config(toml::toml! {
@ -388,7 +388,7 @@ mod tests {
let exit_values = [1, 126, 127, 130, 131]; let exit_values = [1, 126, 127, 130, 131];
let exit_values_name = ["🔴", "🚫", "🔍", "🔴", "🔴"]; let exit_values_name = ["🔴", "🚫", "🔍", "🔴", "🔴"];
for (status, name) in exit_values.iter().zip(exit_values_name.iter()) { for (status, name) in exit_values.iter().zip(&exit_values_name) {
let expected = Some((*name).to_string()); let expected = Some((*name).to_string());
let actual = ModuleRenderer::new("status") let actual = ModuleRenderer::new("status")
.config(toml::toml! { .config(toml::toml! {
@ -424,7 +424,7 @@ mod tests {
"PSF 🔴=🔴 🔴 🔴", "PSF 🔴=🔴 🔴 🔴",
]; ];
for (status, rendered) in exit_values.iter().zip(exit_values_rendered.iter()) { for (status, rendered) in exit_values.iter().zip(&exit_values_rendered) {
let main_exit_code = status[0]; let main_exit_code = status[0];
let pipe_exit_code = &status[1..]; let pipe_exit_code = &status[1..];
@ -468,7 +468,7 @@ mod tests {
"PSF 🔴=🔴1 🔴1 🔴1", "PSF 🔴=🔴1 🔴1 🔴1",
]; ];
for (status, rendered) in exit_values.iter().zip(exit_values_rendered.iter()) { for (status, rendered) in exit_values.iter().zip(&exit_values_rendered) {
let main_exit_code = status[0]; let main_exit_code = status[0];
let pipe_exit_code = &status[1..]; let pipe_exit_code = &status[1..];
@ -521,7 +521,7 @@ mod tests {
let exit_values = [[130, 126, 131, 127], [1, 1, 1, 1]]; let exit_values = [[130, 126, 131, 127], [1, 1, 1, 1]];
let exit_values_rendered = ["F 🧱", "F 🔴"]; let exit_values_rendered = ["F 🧱", "F 🔴"];
for (status, rendered) in exit_values.iter().zip(exit_values_rendered.iter()) { for (status, rendered) in exit_values.iter().zip(&exit_values_rendered) {
let main_exit_code = status[0]; let main_exit_code = status[0];
let pipe_exit_code = &status[1..]; let pipe_exit_code = &status[1..];
@ -561,7 +561,7 @@ mod tests {
"PSF 1ERROR=🟢|🟢|🟢|🔴30|🔍|🚫|🔴3|⚡|🟢|⚡|🟢|🔴", "PSF 1ERROR=🟢|🟢|🟢|🔴30|🔍|🚫|🔴3|⚡|🟢|⚡|🟢|🔴",
]; ];
for (status, rendered) in exit_values.iter().zip(exit_values_rendered.iter()) { for (status, rendered) in exit_values.iter().zip(&exit_values_rendered) {
let main_exit_code = status[0]; let main_exit_code = status[0];
let pipe_exit_code = &status[1..]; let pipe_exit_code = &status[1..];

View File

@ -1,6 +1,5 @@
extern crate winapi; extern crate winapi;
use std::iter;
use std::mem; use std::mem;
use std::os::windows::ffi::OsStrExt; use std::os::windows::ffi::OsStrExt;
use std::path::Path; use std::path::Path;
@ -22,11 +21,7 @@ use winapi::um::winnt::{
/// the current process access token and directory's security descriptor. /// the current process access token and directory's security descriptor.
/// Does not work for network drives and always returns true /// Does not work for network drives and always returns true
pub fn is_write_allowed(folder_path: &Path) -> std::result::Result<bool, &'static str> { pub fn is_write_allowed(folder_path: &Path) -> std::result::Result<bool, &'static str> {
let folder_name: Vec<u16> = folder_path let folder_name: Vec<u16> = folder_path.as_os_str().encode_wide().chain([0]).collect();
.as_os_str()
.encode_wide()
.chain(iter::once(0))
.collect();
if is_network_path(&folder_name) { if is_network_path(&folder_name) {
log::info!( log::info!(

View File

@ -300,13 +300,13 @@ fn compute_modules<'a>(context: &'a Context) -> Vec<Module<'a>> {
for module in &modules { for module in &modules {
// Manually add all modules if `$all` is encountered // Manually add all modules if `$all` is encountered
if module == "all" { if module == "all" {
for module in PROMPT_ORDER.iter() { for module in PROMPT_ORDER {
let modules = handle_module(module, context, &modules); let modules = handle_module(module, context, &modules);
prompt_order.extend(modules.into_iter()); prompt_order.extend(modules);
} }
} else { } else {
let modules = handle_module(module, context, &modules); let modules = handle_module(module, context, &modules);
prompt_order.extend(modules.into_iter()); prompt_order.extend(modules);
} }
} }

View File

@ -20,7 +20,7 @@ fn impl_module_config(dinput: DeriveInput) -> proc_macro::TokenStream {
let mut load_tokens = quote! {}; let mut load_tokens = quote! {};
let mut fields = quote! {}; let mut fields = quote! {};
for field in fields_named.named.iter() { for field in &fields_named.named {
let ident = field.ident.as_ref().unwrap(); let ident = field.ident.as_ref().unwrap();
let new_load_tokens = quote! { let new_load_tokens = quote! {