use crate::error::{VBError, VBResult};
use crate::state::file;
use crate::value::VBVariant;
use vb6core::error::err_number;
pub fn rmdir(path: &VBVariant) -> VBResult<()> {
let path_str = match path {
VBVariant::String(s) => s.as_str().to_string(),
_ => {
return Err(VBError::with_description(
13, "Type mismatch in RmDir",
));
}
};
file::remove_dir(std::path::Path::new(&path_str)).map_err(|e| {
VBError::with_description(
match e.kind() {
std::io::ErrorKind::NotFound => err_number::PATH_NOT_FOUND, std::io::ErrorKind::PermissionDenied => err_number::PERMISSION_DENIED, _ => err_number::PATH_FILE_ACCESS_ERROR, },
e.to_string(),
)
})?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
use crate::state::file::{self};
use vb6core::error::err_number;
#[test]
fn rmdir_removes_directory() {
let _guard = crate::state::test_support::lock_test();
let dir = tempfile::tempdir().unwrap();
file::set_root(dir.path());
std::fs::create_dir(dir.path().join("toremove")).unwrap();
assert!(dir.path().join("toremove").exists());
rmdir(&VBVariant::from_string("toremove")).unwrap();
assert!(!dir.path().join("toremove").exists());
}
#[test]
fn rmdir_rejects_nonexistent_directory() {
let _guard = crate::state::test_support::lock_test();
let dir = tempfile::tempdir().unwrap();
file::set_root(dir.path());
let result = rmdir(&VBVariant::from_string("nonexistent"));
assert!(result.is_err());
assert_eq!(result.unwrap_err().number, err_number::PATH_NOT_FOUND);
}
#[test]
fn rmdir_rejects_non_string() {
let _guard = crate::state::test_support::lock_test();
let result = rmdir(&VBVariant::Long(42));
assert!(result.is_err());
assert_eq!(result.unwrap_err().number, err_number::TYPE_MISMATCH);
}
}