diff --git a/src/main.rs b/src/main.rs index 1055f85..fd2abb1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,12 @@ use anyhow::{anyhow, Context, Result}; use cli::{Cli, Commands}; use colored::{ColoredString, Colorize}; use util::sk_cfg_path; -use std::{fs::create_dir_all, path::PathBuf}; +use std::{path::PathBuf}; use std::fs; -use clap::{Parser, Subcommand}; +use clap::{Parser}; -use crate::util::{copy_recursively, path_buf_to_string}; +use crate::util::{copy_recursively, path_buf_filename, path_buf_to_string}; mod cli; mod util; @@ -55,7 +55,7 @@ fn add(source: PathBuf, id: Option) -> Result<()> { Ok(()) } -fn new(id: String, path: Option, name: Option) -> Result<()> { +fn new(id: String, path: Option, _name: Option) -> Result<()> { let mut dest_path = path.unwrap_or(PathBuf::from(&id)); let skeleton_path = sk_cfg_path()?.join("skeletons").join(&id); @@ -73,14 +73,14 @@ fn new(id: String, path: Option, name: Option) -> Result<()> { } // FIXME: Hacky - if !dest_path.exists() && path_buf_to_string(&dest_path)?.ends_with("/") { + if !dest_path.exists() && path_buf_to_string(&dest_path)?.ends_with('/') { return Err(anyhow!("Target directory does not exist")); } fs::File::create(&dest_path)?; fs::copy(&skeleton_path, &dest_path)?; } else if skeleton_path.is_dir() { - if (dest_path.is_dir() && !dest_path.read_dir()?.next().is_none()) || (dest_path.is_file()) { + if (dest_path.is_dir() && dest_path.read_dir()?.next().is_some()) || (dest_path.is_file()) { return Err(anyhow!("Target directory already exists and is not an empty directory")); } @@ -93,23 +93,18 @@ fn new(id: String, path: Option, name: Option) -> Result<()> { } fn list() -> Result<()> { - for entry in fs::read_dir(sk_cfg_path()?.join("skeletons"))? { - match entry { - Ok(n) => { - let path = n.path(); - // lmao nice "refactor" - let mut id: ColoredString = path.file_name().unwrap().to_str().unwrap().to_string().into(); - - if path.is_dir() { - id = id.blue().bold(); - } else { - id = id.white(); - } - - print!("{} ", &id); - }, - Err(_) => (), + for entry in fs::read_dir(sk_cfg_path()?.join("skeletons"))?.flatten() { + let path = entry.path(); + // lmao nice "refactor" + let mut id: ColoredString = path_buf_filename(&path)?.into(); + + if path.is_dir() { + id = id.blue().bold(); + } else { + id = id.white(); } + + print!("{} ", &id); } println!(); @@ -117,8 +112,8 @@ fn list() -> Result<()> { Ok(()) } -fn remove(id: String, no_confirm: bool) -> Result<()> { - let skeleton_path = sk_cfg_path()?.join("skeletons").join(&id); +fn remove(id: String, _no_confirm: bool) -> Result<()> { + let skeleton_path = sk_cfg_path()?.join("skeletons").join(id); if !skeleton_path.exists() { return Err(anyhow!("Could not find skeleton")); diff --git a/src/util.rs b/src/util.rs index a2e3365..4c586ee 100644 --- a/src/util.rs +++ b/src/util.rs @@ -2,7 +2,7 @@ use std::path::PathBuf; use std::fs; use std::path::Path; -use anyhow::{anyhow, Result, Error}; +use anyhow::{anyhow, Result}; use home::home_dir; pub fn sk_cfg_path() -> Result { @@ -12,11 +12,11 @@ pub fn sk_cfg_path() -> Result { } } -pub fn path_buf_filename(path: &PathBuf) -> Result { +pub fn path_buf_filename(path: &Path) -> Result { Ok(path.file_name().unwrap().to_str().unwrap().to_string()) } -pub fn path_buf_to_string(p: &PathBuf) -> Result { +pub fn path_buf_to_string(p: &Path) -> Result { match p.to_str() { Some(s) => Ok(s.to_string()), None => Err(anyhow!("Could not convert string to PathBuf")),