use std::{io, path::PathBuf};
pub const GUID: &str = "5E702FA07C2FB332B76B";
pub const DEBUG_MODE: bool = cfg!(debug_assertions);
#[derive(Default, Debug, Clone)]
pub struct MetadataCollection {
pub name: String,
pub ext: String,
pub parent_dir: PathBuf,
}
#[derive(Default, Debug, Clone)]
pub struct PrepareName {
pub original_path: PathBuf,
pub new_path: PathBuf,
pub pre_path: PathBuf,
}
#[derive(Default, Debug, Clone)]
pub struct FileInfos {
pub is_exist: bool,
pub is_file: bool,
pub packed_info: MetadataCollection,
pub exchange: PrepareName,
}
#[derive(Default, Debug, Clone)]
pub struct GetPathInfo {
pub path1: PathBuf,
pub path2: PathBuf,
}
#[derive(Default, Debug, Clone)]
pub struct NameExchange {
pub f1: FileInfos,
pub f2: FileInfos,
}
#[derive(Debug, Clone)]
pub enum RenameError {
PermissionDenied,
AlreadyExists,
NotExists,
SamePath,
InvalidPath(String),
Unknown(String),
}
impl RenameError {
pub fn to_code(&self) -> i32 {
match self {
Self::NotExists => 1,
Self::PermissionDenied => 2,
Self::AlreadyExists => 3,
Self::SamePath => 4,
Self::InvalidPath(_) => 5,
Self::Unknown(_) => 255,
}
}
}
impl std::fmt::Display for RenameError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::PermissionDenied => write!(f, "Permission denied"),
Self::AlreadyExists => write!(f, "File already exists"),
Self::NotExists => write!(f, "File does not exist"),
Self::SamePath => write!(f, "Two paths refer to the same file"),
Self::InvalidPath(msg) => write!(f, "Invalid path: {}", msg),
Self::Unknown(msg) => write!(f, "Unknown error: {}", msg),
}
}
}
impl From<io::Error> for RenameError {
fn from(value: io::Error) -> Self {
match value.kind() {
io::ErrorKind::NotFound => RenameError::NotExists,
io::ErrorKind::PermissionDenied => RenameError::PermissionDenied,
io::ErrorKind::AlreadyExists => RenameError::AlreadyExists,
_ => RenameError::Unknown(value.to_string()),
}
}
}