Skip to content

Commit

Permalink
error reporting and testing
Browse files Browse the repository at this point in the history
  • Loading branch information
roylaurie committed May 3, 2024
1 parent f59070d commit e383c6a
Show file tree
Hide file tree
Showing 5 changed files with 482 additions and 82 deletions.
13 changes: 10 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bak9"
version = "0.0.2"
version = "0.1.1"
edition = "2021"
description = "Creates a backup .bak copy of a file"
authors = ["Asmov LLC <[email protected]>"]
Expand All @@ -10,6 +10,13 @@ keywords = ["backup", "copy"]
categories = ["command-line-utilities", "filesystem"]

[dependencies]
clap = { version = "^4", features = ["derive"] }
file_diff = "^1"
clap = { version = "4", features = ["derive"] }
colored = "2"
file_diff = "1"
strum = { version = "0", features = ["derive"] }
thiserror = "1"

[dev-dependencies]
function_name = "0"
file_diff = "1"

46 changes: 36 additions & 10 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::path::PathBuf;
use clap::Parser;

use crate::PathExt;

#[derive(Parser)]
#[command(version, about)]
pub struct Cli {
Expand All @@ -13,33 +15,57 @@ pub struct Cli {
#[arg(short, default_value_t = false, help = "Delete all backups of FILE")]
pub delete: bool,

#[arg(short, default_value_t = 10, help = "Number of backups to keep before pruning")]
#[arg(short, value_parser = clap::value_parser!(u8).range(1..),
default_value_t = 10, help = "Number of backups to keep before pruning")]
pub num: u8,
}

impl Cli {
pub fn dir(&self) -> PathBuf {
match &self.dir {
Some(dir) => dir.clone(),
None => self.file.parent().unwrap().to_path_buf().clone(),
None => self.file.parent().expect("Expected parent directory").to_path_buf().clone(),
}
}
}

fn validate_path(path: &str, filetype: &'static str) -> Result<PathBuf, String> {
let path = PathBuf::from(path)
.canonicalize()
.map_err(|_| format!("{filetype} not found: {:?}", path))?;

if !path.exists() {
return Err(format!("{filetype} not found: {:?}", path))
}

Ok(path)
}

fn validate_file(path: &str) -> Result<PathBuf, String> {
let path = PathBuf::from(path);
if path.exists() {
Ok(path)
let path = validate_path(path, "File")?;
if !path.is_file() {
Err(format!("Source path is not a file: {:?}", path))
} else if path.filename_str().is_none() {
return Err(format!("Invalid source file: {:?}", path))
} else {
Err(format!("File not found: {:?}", path))
Ok(path)
}
}

fn validate_dir(path: &str) -> Result<PathBuf, String> {
let path = PathBuf::from(path);
if path.exists() {
Ok(path)
let path = validate_path(path, "Directory")?;
if !path.is_dir() {
return Err(format!("Destination path is not a directory: {:?}", path))
} else {
Err(format!("Directory not found: {:?}", path))
Ok(path)
}
}

#[cfg(test)]
mod tests {
#[test]
fn test_verify_cli() {
use clap::CommandFactory;
super::Cli::command().debug_assert()
}
}
Loading

0 comments on commit e383c6a

Please sign in to comment.