Skip to content

Commit

Permalink
Apply clippy linting suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
sunsided committed Dec 30, 2023
1 parent 7c58496 commit c587b19
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/banner.rs
Expand Up @@ -4,7 +4,7 @@

use std::fmt::{Display, Formatter};

static BANNER: &'static str = indoc::indoc!(
static BANNER: &str = indoc::indoc!(
r#"
██╗░░██╗░█████╗░░██████╗░░░░░███████╗██╗░░░░░░░██╗██████╗
██║░██╔╝██╔══██╗██╔════╝░██╗░██╔════╝██║░░██╗░░██║██╔══██╗
Expand Down
8 changes: 4 additions & 4 deletions src/cli.rs
Expand Up @@ -37,7 +37,7 @@ pub struct Cli {

fn config_file_exists(s: &str) -> Result<PathBuf, String> {
let path = PathBuf::from(s);
if let Ok(_) = File::open(&path) {
if File::open(&path).is_ok() {
Ok(path)
} else {
Err(format!(
Expand All @@ -63,9 +63,9 @@ impl Deref for KubectlPathBuf {
}
}

impl Into<PathBuf> for KubectlPathBuf {
fn into(self) -> PathBuf {
self.0
impl From<KubectlPathBuf> for PathBuf {
fn from(val: KubectlPathBuf) -> Self {
val.0
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/config.rs
Expand Up @@ -34,7 +34,7 @@ lazy_static! {
pub static ref HIGHEST_SUPPORTED_VERSION: Version = Version::new(0, 3, 0);
}

pub static DEFAULT_CONFIG_FILE: &'static str = ".k8sfwd";
pub static DEFAULT_CONFIG_FILE: &str = ".k8sfwd";

/// Describes the source and handling of a configuration.
#[derive(Debug)]
Expand Down Expand Up @@ -73,27 +73,27 @@ pub fn sanitize_config(
fn autofill_context_and_cluster(
config: &mut PortForwardConfig,
kubectl: &Kubectl,
current_context: &String,
current_context: &str,
current_cluster: &Option<String>,
) {
match (&mut config.context, &mut config.cluster) {
(Some(_context), Some(_cluster)) => { /* nothing to do */ }
(Some(context), None) => match kubectl.cluster_from_context(Some(&context)) {
(Some(context), None) => match kubectl.cluster_from_context(Some(context)) {
Ok(Some(cluster)) => {
config.cluster = Some(cluster);
}
Ok(None) => {}
Err(_) => {}
},
(None, Some(cluster)) => match kubectl.context_from_cluster(Some(&cluster)) {
(None, Some(cluster)) => match kubectl.context_from_cluster(Some(cluster)) {
Ok(Some(context)) => {
config.context = Some(context);
}
Ok(None) => {}
Err(_) => {}
},
(None, None) => {
config.context = Some(current_context.clone());
config.context = Some(current_context.to_owned());
config.cluster = current_cluster.clone();
}
}
Expand Down Expand Up @@ -211,7 +211,7 @@ fn handle_special_path(
};

if !visited_paths.track_directory(&path)? {
let path = path.join(&config);
let path = path.join(config);
if let Ok(file) = File::open(&path) {
files.push((
ConfigMeta {
Expand Down
2 changes: 1 addition & 1 deletion src/config/port.rs
Expand Up @@ -24,7 +24,7 @@ impl MergeWith for Vec<Port> {

let set: HashSet<Port> = HashSet::from_iter(self.iter().cloned());
let other_set = HashSet::from_iter(other.iter().cloned());
*self = Vec::from_iter(&mut set.union(&other_set).into_iter().cloned());
*self = Vec::from_iter(&mut set.union(&other_set).cloned());
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/config/port_forward_config.rs
Expand Up @@ -56,7 +56,7 @@ impl MergeWith for PortForwardConfig {
self.cluster.merge_with(&other.cluster);
self.merge_listen_addrs(&other.listen_addrs);
self.namespace = other.namespace.clone();
self.r#type = other.r#type.clone();
self.r#type = other.r#type;
self.target = other.target.clone();
self.ports.merge_with(&other.ports);
}
Expand All @@ -77,11 +77,11 @@ impl MergeWith for Vec<PortForwardConfig> {

for cfg in other {
map.entry(cfg.target.clone())
.and_modify(|current| current.merge_with(&cfg))
.and_modify(|current| current.merge_with(cfg))
.or_insert(cfg.clone());
}

*self = Vec::from_iter(map.into_iter().map(|(_k, v)| v));
*self = Vec::from_iter(map.into_values());
}
}

Expand All @@ -93,7 +93,7 @@ impl PortForwardConfig {
fn merge_listen_addrs(&mut self, other: &[String]) {
let set: HashSet<String> = HashSet::from_iter(self.listen_addrs.drain(0..));
let other_set = HashSet::from_iter(other.iter().cloned());
self.listen_addrs = Vec::from_iter(&mut set.union(&other_set).into_iter().cloned());
self.listen_addrs = Vec::from_iter(&mut set.union(&other_set).cloned());
}
}

Expand Down
6 changes: 1 addition & 5 deletions src/config/port_forward_configs.rs
Expand Up @@ -90,11 +90,7 @@ impl IntoIterator for PortForwardConfigs {
impl PortForwardConfigs {
pub fn is_supported_version(&self) -> bool {
#[allow(clippy::absurd_extreme_comparisons)]
if self.version < *LOWEST_SUPPORTED_VERSION || self.version > *HIGHEST_SUPPORTED_VERSION {
false
} else {
true
}
!(self.version < *LOWEST_SUPPORTED_VERSION || self.version > *HIGHEST_SUPPORTED_VERSION)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/config/resource_type.rs
Expand Up @@ -22,7 +22,7 @@ impl Default for ResourceType {
}

impl ResourceType {
pub fn to_arg(&self) -> &'static str {
pub fn as_arg(&self) -> &'static str {
match self {
ResourceType::Service => "service",
ResourceType::Deployment => "deployment",
Expand Down
6 changes: 3 additions & 3 deletions src/config/retry_delay.rs
Expand Up @@ -23,9 +23,9 @@ impl Default for RetryDelay {
}
}

impl Into<Duration> for RetryDelay {
fn into(self) -> Duration {
Duration::from_secs_f64(self.0)
impl From<RetryDelay> for Duration {
fn from(val: RetryDelay) -> Self {
Duration::from_secs_f64(val.0)
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/config/visit_tracker.rs
Expand Up @@ -3,7 +3,7 @@
// SPDX-FileType: SOURCE

use same_file::is_same_file;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

/// Tracks directories that were visited during a configuration scan.
#[derive(Debug, Default)]
Expand All @@ -16,7 +16,7 @@ impl VisitTracker {
///
/// This differs from [`track_directory`](Self::track_directory) in that
/// it canonicalizes the file path and registers the owning directory.
pub fn track_file_path(&mut self, file: &PathBuf) -> Result<bool, std::io::Error> {
pub fn track_file_path(&mut self, file: &Path) -> Result<bool, std::io::Error> {
if let Some(directory) = file.canonicalize()?.parent() {
let path_buf = directory.to_path_buf();
return self.track_directory(&path_buf);
Expand All @@ -39,7 +39,7 @@ impl VisitTracker {
/// Tests whether a path was already visited before.
fn path_already_visited(&self, test_path: &PathBuf) -> Result<bool, std::io::Error> {
for path in &self.visited {
match is_same_file(path, &test_path) {
match is_same_file(path, test_path) {
Ok(true) => return Ok(true),
Ok(false) => continue,
Err(e) => return Err(e),
Expand Down
8 changes: 4 additions & 4 deletions src/kubectl.rs
Expand Up @@ -7,7 +7,7 @@ use crate::config::{ConfigId, OperationalConfig, PortForwardConfig, RetryDelay};
use serde::Deserialize;
use std::env::current_dir;
use std::io::{BufRead, Read};
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
use std::sync::mpsc::Sender;
use std::thread::JoinHandle;
Expand All @@ -26,7 +26,7 @@ pub struct Kubectl {

impl Kubectl {
pub fn new(kubectl: Option<KubectlPathBuf>) -> Result<Self, ShellError> {
let kubectl: PathBuf = kubectl.unwrap_or(KubectlPathBuf::default()).into();
let kubectl: PathBuf = kubectl.unwrap_or_default().into();
let path = kubectl
.parent()
.map(|p| p.to_path_buf())
Expand Down Expand Up @@ -162,7 +162,7 @@ impl Kubectl {
) -> Result<JoinHandle<Result<(), anyhow::Error>>, VersionError> {
let target = format!(
"{resource}/{name}",
resource = fwd_config.r#type.to_arg(),
resource = fwd_config.r#type.as_arg(),
name = fwd_config.target
);

Expand Down Expand Up @@ -269,7 +269,7 @@ impl Kubectl {
Ok(child_thread)
}

fn get_env_path(current_dir: &PathBuf) -> String {
fn get_env_path(current_dir: &Path) -> String {
let mut path = std::env::var("PATH").unwrap_or_else(|_| String::new());
if !path.is_empty() {
path.push(ENV_PATH_SEPARATOR);
Expand Down
9 changes: 4 additions & 5 deletions src/main.rs
Expand Up @@ -191,14 +191,14 @@ fn map_and_print_config(
println!("{id} {name}");
println!(
"{padding} target: {resource}/{name}.{namespace}",
resource = config.r#type.to_arg(),
resource = config.r#type.as_arg(),
name = config.target,
namespace = config.namespace
);
} else {
println!(
"{id} target: {resource}/{name}.{namespace}",
resource = config.r#type.to_arg(),
resource = config.r#type.as_arg(),
name = config.target,
namespace = config.namespace
);
Expand Down Expand Up @@ -232,7 +232,7 @@ fn map_and_print_config(
}

fn start_output_loop_thread(out_rx: Receiver<ChildEvent>) -> JoinHandle<()> {
let print_thread = thread::spawn(move || {
thread::spawn(move || {
while let Ok(event) = out_rx.recv() {
match event {
ChildEvent::Output(id, channel, message) => {
Expand Down Expand Up @@ -266,8 +266,7 @@ fn start_output_loop_thread(out_rx: Receiver<ChildEvent>) -> JoinHandle<()> {
}
}
}
});
print_thread
})
}

fn exitcode(code: exitcode::ExitCode) -> Result<ExitCode, anyhow::Error> {
Expand Down
2 changes: 1 addition & 1 deletion src/target_filter.rs
Expand Up @@ -58,7 +58,7 @@ where
}

for filter in this {
if filter.matches(&config) {
if filter.matches(config) {
return true;
}
}
Expand Down

0 comments on commit c587b19

Please sign in to comment.