use std::fs;
use std::path::Path;
use ssri::Integrity;
use crate::content::rm;
use crate::errors::{Internal, Result};
use crate::index;
pub fn remove<P, K>(cache: P, key: K) -> Result<()>
where
P: AsRef<Path>,
K: AsRef<str>,
{
index::delete(cache.as_ref(), key.as_ref())
}
pub fn remove_hash<P: AsRef<Path>>(cache: P, sri: &Integrity) -> Result<()> {
rm::rm(cache.as_ref(), sri)
}
pub fn clear<P: AsRef<Path>>(cache: P) -> Result<()> {
for entry in (cache.as_ref().read_dir().to_internal()?).flatten() {
fs::remove_dir_all(entry.path()).to_internal()?;
}
Ok(())
}
#[cfg(test)]
mod tests {
#[test]
fn test_remove() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().to_owned();
let sri = crate::write(&dir, "key", b"my-data").unwrap();
crate::remove(&dir, "key").unwrap();
let new_entry = crate::metadata(&dir, "key").unwrap();
assert!(new_entry.is_none());
let data_exists = crate::exists(&dir, &sri);
assert!(data_exists);
}
#[test]
fn test_remove_data() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().to_owned();
let sri = crate::write(&dir, "key", b"my-data").unwrap();
crate::remove_hash(&dir, &sri).unwrap();
let entry = crate::metadata(&dir, "key").unwrap();
assert!(entry.is_some());
let data_exists = crate::exists(&dir, &sri);
assert!(!data_exists);
}
#[test]
fn test_clear() {
let tmp = tempfile::tempdir().unwrap();
let dir = tmp.path().to_owned();
let sri = crate::write(&dir, "key", b"my-data").unwrap();
crate::clear(&dir).unwrap();
let entry = crate::metadata(&dir, "key").unwrap();
assert_eq!(entry, None);
let data_exists = crate::exists(&dir, &sri);
assert!(!data_exists);
}
}