t_rex/
fix.rs

1use std::{fs::DirEntry, path::Path};
2
3use anyhow::{bail, Result};
4use console::style;
5
6use crate::utils::{get_padding_digits, get_sorted_files_in_dir, rename_files_from_index};
7
8pub fn process_fix(dir_path: String) -> Result<()> {
9    let dir_path = Path::new(&dir_path);
10    let dir_path_string = dir_path.to_string_lossy().to_string();
11    if !dir_path.is_dir() {
12        bail!(
13            "Couldn't find the directory with the given path '{}'!",
14            dir_path_string
15        );
16    }
17    let sorted_files: Vec<DirEntry> = get_sorted_files_in_dir(dir_path)?;
18    let (_, digits_needed) = get_padding_digits(&sorted_files);
19    let fix_count = rename_files_from_index(sorted_files, None, dir_path, digits_needed)?;
20
21    println!(
22        "{}",
23        style(format!("Successfully fixed {} files!", fix_count))
24            .green()
25            .bold()
26            .dim()
27    );
28
29    Ok(())
30}