use std::path::PathBuf;
pub fn restore_original_extension(encrypted_file_path: &PathBuf) -> PathBuf {
if let Some(file_name) = encrypted_file_path.file_name() {
if let Some(file_name_str) = file_name.to_str() {
if file_name_str == ".sekrit" {
if let Some(parent) = encrypted_file_path.parent() {
return parent.join(".decrypted");
}
return PathBuf::from(".decrypted");
}
if file_name_str.ends_with(".sekrit") {
let original_name = &file_name_str[..file_name_str.len() - 7]; if let Some(parent) = encrypted_file_path.parent() {
if original_name.is_empty() {
return parent.join("file.decrypted");
}
return parent.join(original_name);
}
return PathBuf::from(original_name);
}
}
}
encrypted_file_path.with_extension("decrypted")
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::Path;
#[test]
fn test_restore_extension_simple() {
let encrypted_path = PathBuf::from("document.txt.sekrit");
let restored = restore_original_extension(&encrypted_path);
assert_eq!(restored, PathBuf::from("document.txt"));
}
#[test]
fn test_restore_extension_with_directory() {
let encrypted_path = PathBuf::from("/home/user/documents/file.pdf.sekrit");
let restored = restore_original_extension(&encrypted_path);
assert_eq!(restored, PathBuf::from("/home/user/documents/file.pdf"));
}
#[test]
fn test_restore_extension_multiple_dots() {
let encrypted_path = PathBuf::from("archive.tar.gz.sekrit");
let restored = restore_original_extension(&encrypted_path);
assert_eq!(restored, PathBuf::from("archive.tar.gz"));
}
#[test]
fn test_restore_extension_no_original_extension() {
let encrypted_path = PathBuf::from("file.sekrit");
let restored = restore_original_extension(&encrypted_path);
assert_eq!(restored, PathBuf::from("file"));
}
#[test]
fn test_restore_extension_nested_path() {
let encrypted_path = PathBuf::from("folder/subfolder/image.png.sekrit");
let restored = restore_original_extension(&encrypted_path);
assert_eq!(restored, PathBuf::from("folder/subfolder/image.png"));
}
#[test]
fn test_restore_extension_relative_path() {
let encrypted_path = PathBuf::from("./temp/script.py.sekrit");
let restored = restore_original_extension(&encrypted_path);
assert_eq!(restored, PathBuf::from("./temp/script.py"));
}
#[test]
fn test_restore_extension_unicode_filename() {
let encrypted_path = PathBuf::from("文档.txt.sekrit");
let restored = restore_original_extension(&encrypted_path);
assert_eq!(restored, PathBuf::from("文档.txt"));
}
#[test]
fn test_restore_extension_no_sekrit_extension() {
let encrypted_path = PathBuf::from("document.txt");
let restored = restore_original_extension(&encrypted_path);
assert_eq!(restored, PathBuf::from("document.decrypted")); }
#[test]
fn test_restore_extension_just_sekrit() {
let encrypted_path = PathBuf::from(".sekrit");
let restored = restore_original_extension(&encrypted_path);
assert_eq!(restored, PathBuf::from(".decrypted")); }
#[test]
fn test_restore_extension_double_sekrit() {
let encrypted_path = PathBuf::from("file.sekrit.sekrit");
let restored = restore_original_extension(&encrypted_path);
assert_eq!(restored, PathBuf::from("file.sekrit"));
}
}