diff --git a/ci/cargo-clippy.yml b/ci/cargo-clippy.yml index 76680ceb7..154acfe26 100644 --- a/ci/cargo-clippy.yml +++ b/ci/cargo-clippy.yml @@ -9,5 +9,5 @@ jobs: steps: - template: install-rust.yml - - script: cargo clippy -- -D warnings -D clippy::all -D clippy::nursery -D clippy::pedantic + - script: cargo clippy -- -D clippy::all displayName: Run clippy diff --git a/src/config.rs b/src/config.rs index 92073b5f6..ba339468b 100644 --- a/src/config.rs +++ b/src/config.rs @@ -21,30 +21,27 @@ pub trait Config { impl Config for Table { /// Initialize the Config struct fn initialize() -> Table { - if let Some(file_data) = Table::config_from_file() { + if let Some(file_data) = Self::config_from_file() { return file_data; } - Table::new() + Self::new() } /// Create a config from a starship configuration file fn config_from_file() -> Option { - let file_path = match env::var("STARSHIP_CONFIG") { - Ok(path) => { - // Use $STARSHIP_CONFIG as the config path if available - log::debug!("STARSHIP_CONFIG is set: \n{}", &path); - path - } - Err(_) => { - // Default to using ~/.config/starhip.toml - log::debug!("STARSHIP_CONFIG is not set"); - let config_path = home_dir()?.join(".config/starship.toml"); - let config_path_str = config_path.to_str()?.to_owned(); + let file_path = if let Ok(path) = env::var("STARSHIP_CONFIG") { + // Use $STARSHIP_CONFIG as the config path if available + log::debug!("STARSHIP_CONFIG is set: \n{}", &path); + path + } else { + // Default to using ~/.config/starhip.toml + log::debug!("STARSHIP_CONFIG is not set"); + let config_path = home_dir()?.join(".config/starship.toml"); + let config_path_str = config_path.to_str()?.to_owned(); - log::debug!("Using default config path: {}", config_path_str); - config_path_str - } + log::debug!("Using default config path: {}", config_path_str); + config_path_str }; let toml_content = match utils::read_file(&file_path) { @@ -65,10 +62,7 @@ impl Config for Table { /// Get the subset of the table for a module by its name fn get_module_config(&self, module_name: &str) -> Option<&toml::value::Table> { - let module_config = self - .get(module_name) - .map(toml::Value::as_table) - .unwrap_or(None); + let module_config = self.get(module_name).and_then(toml::Value::as_table); if module_config.is_some() { log::debug!( @@ -149,7 +143,7 @@ impl Config for Table { } } - +#[cfg(test)] mod tests { use super::*; diff --git a/src/context.rs b/src/context.rs index 3e3640ace..1cfbaa3af 100644 --- a/src/context.rs +++ b/src/context.rs @@ -70,10 +70,10 @@ impl<'a> Context<'a> { let repository = Repository::discover(¤t_dir).ok(); let repo_root = repository .as_ref() - .and_then(|repo| repo.workdir().map(|repo| repo.to_path_buf())); + .and_then(|repo| repo.workdir().map(std::path::Path::to_path_buf)); let branch_name = repository .as_ref() - .and_then(|repo| get_current_branch(&repo)); + .and_then(|repo| get_current_branch(repo)); Context { config, @@ -102,9 +102,7 @@ impl<'a> Context<'a> { let config = self.config.get_module_config(name); // If the segment has "disabled" set to "true", don't show it - let disabled = config - .map(|table| table.get_as_bool("disabled")) - .unwrap_or(None); + let disabled = config.and_then(|table| table.get_as_bool("disabled")); if disabled == Some(true) { return None; @@ -135,17 +133,17 @@ pub struct ScanDir<'a> { } impl<'a> ScanDir<'a> { - pub fn set_files(mut self, files: &'a [&'a str]) -> Self { + pub const fn set_files(mut self, files: &'a [&'a str]) -> Self { self.files = files; self } - pub fn set_extensions(mut self, extensions: &'a [&'a str]) -> Self { + pub const fn set_extensions(mut self, extensions: &'a [&'a str]) -> Self { self.extensions = extensions; self } - pub fn set_folders(mut self, folders: &'a [&'a str]) -> Self { + pub const fn set_folders(mut self, folders: &'a [&'a str]) -> Self { self.folders = folders; self } @@ -155,9 +153,9 @@ impl<'a> ScanDir<'a> { pub fn scan(&mut self) -> bool { self.dir_files.iter().any(|path| { if path.is_dir() { - return path_has_name(&path, &self.folders); + path_has_name(path, self.folders) } else { - return path_has_name(&path, &self.files) || has_extension(&path, &self.extensions); + path_has_name(path, self.files) || has_extension(path, self.extensions) } }) } @@ -199,7 +197,7 @@ fn get_current_branch(repository: &Repository) -> Option { let head = repository.head().ok()?; let shorthand = head.shorthand(); - shorthand.map(|branch| branch.to_string()) + shorthand.map(std::string::ToString::to_string) } #[cfg(test)] diff --git a/src/init.rs b/src/init.rs index 5db23c2b4..396ba72d0 100644 --- a/src/init.rs +++ b/src/init.rs @@ -54,8 +54,7 @@ pub fn init(shell_name: &str) { } }; - if setup_script.is_some() { - let script = setup_script.unwrap(); + if let Some(script) = setup_script { print!("{}", script); } } diff --git a/src/module.rs b/src/module.rs index 526345b95..ade5d9e5d 100644 --- a/src/module.rs +++ b/src/module.rs @@ -17,13 +17,13 @@ pub struct Module<'a> { style: Style, /// The prefix used to separate the current module from the previous one. - prefix: ModuleAffix, + prefix: Affix, /// The collection of segments that compose this module. segments: Vec, /// The suffix used to separate the current module from the next one. - suffix: ModuleAffix, + suffix: Affix, } impl<'a> Module<'a> { @@ -33,9 +33,9 @@ impl<'a> Module<'a> { config, name: name.to_string(), style: Style::default(), - prefix: ModuleAffix::default_prefix(name.to_string()), + prefix: Affix::default_prefix(name), segments: Vec::new(), - suffix: ModuleAffix::default_suffix(name.to_string()), + suffix: Affix::default_suffix(name), } } @@ -56,12 +56,12 @@ impl<'a> Module<'a> { } /// Get the module's prefix - pub fn get_prefix(&mut self) -> &mut ModuleAffix { + pub fn get_prefix(&mut self) -> &mut Affix { &mut self.prefix } /// Get the module's suffix - pub fn get_suffix(&mut self) -> &mut ModuleAffix { + pub fn get_suffix(&mut self) -> &mut Affix { &mut self.suffix } @@ -82,7 +82,7 @@ impl<'a> Module<'a> { let mut ansi_strings = self .segments .iter() - .map(|s| s.ansi_string()) + .map(Segment::ansi_string) .collect::>(); ansi_strings.insert(0, self.prefix.ansi_string()); @@ -119,7 +119,7 @@ impl<'a> fmt::Display for Module<'a> { } /// Module affixes are to be used for the prefix or suffix of a module. -pub struct ModuleAffix { +pub struct Affix { /// The affix's name, to be used in configuration and logging. name: String, @@ -130,17 +130,17 @@ pub struct ModuleAffix { value: String, } -impl ModuleAffix { - pub fn default_prefix(name: String) -> ModuleAffix { - ModuleAffix { +impl Affix { + pub fn default_prefix(name: &str) -> Self { + Self { name: format!("{}_prefix", name), style: Style::default(), value: "via ".to_string(), } } - pub fn default_suffix(name: String) -> ModuleAffix { - ModuleAffix { + pub fn default_suffix(name: &str) -> Self { + Self { name: format!("{}_suffix", name), style: Style::default(), value: " ".to_string(), @@ -150,7 +150,7 @@ impl ModuleAffix { /// Sets the style of the module. /// /// Accepts either `Color` or `Style`. - pub fn set_style(&mut self, style: T) -> &mut ModuleAffix + pub fn set_style(&mut self, style: T) -> &mut Self where T: Into