mirror of https://github.com/zkat/cacache-rs.git
feat(api): AsRef all the things!
This commit is contained in:
parent
5a57d46b3f
commit
5af622eb30
24
src/get.rs
24
src/get.rs
|
|
@ -7,34 +7,34 @@ use crate::content::read;
|
|||
use crate::errors::Error;
|
||||
use crate::index::{self, Entry};
|
||||
|
||||
pub fn read(cache: &Path, key: &str) -> Result<Vec<u8>, Error> {
|
||||
if let Some(entry) = index::find(&cache, &key)? {
|
||||
pub fn read<P: AsRef<Path>, K: AsRef<str>>(cache: P, key: K) -> Result<Vec<u8>, Error> {
|
||||
if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? {
|
||||
read_hash(cache, &entry.integrity)
|
||||
} else {
|
||||
Err(Error::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn read_hash(cache: &Path, sri: &Integrity) -> Result<Vec<u8>, Error> {
|
||||
Ok(read::read(cache, sri)?)
|
||||
pub fn read_hash<P: AsRef<Path>>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error> {
|
||||
Ok(read::read(cache.as_ref(), sri)?)
|
||||
}
|
||||
|
||||
pub fn copy(cache: &Path, key: &str, to: &Path) -> Result<u64, Error> {
|
||||
if let Some(entry) = index::find(&cache, &key)? {
|
||||
pub fn copy<P: AsRef<Path>, K: AsRef<str>>(cache: P, key: K, to: P) -> Result<u64, Error> {
|
||||
if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? {
|
||||
copy_hash(cache, &entry.integrity, to)
|
||||
} else {
|
||||
Err(Error::NotFound)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn copy_hash(cache: &Path, sri: &Integrity, to: &Path) -> Result<u64, Error> {
|
||||
Ok(read::copy(cache, sri, to)?)
|
||||
pub fn copy_hash<P: AsRef<Path>>(cache: P, sri: &Integrity, to: P) -> Result<u64, Error> {
|
||||
Ok(read::copy(cache.as_ref(), sri, to.as_ref())?)
|
||||
}
|
||||
|
||||
pub fn info(cache: &Path, key: &str) -> Result<Option<Entry>, Error> {
|
||||
index::find(cache, &key)
|
||||
pub fn info<P: AsRef<Path>, K: AsRef<str>>(cache: P, key: K) -> Result<Option<Entry>, Error> {
|
||||
index::find(cache.as_ref(), key.as_ref())
|
||||
}
|
||||
|
||||
pub fn hash_exists(cache: &Path, sri: &Integrity) -> bool {
|
||||
read::has_content(&cache, &sri).is_some()
|
||||
pub fn hash_exists<P: AsRef<Path>>(cache: P, sri: &Integrity) -> bool {
|
||||
read::has_content(cache.as_ref(), &sri).is_some()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,6 +3,6 @@ use std::path::Path;
|
|||
|
||||
use crate::index;
|
||||
|
||||
pub fn all(cache: &Path) -> impl Iterator {
|
||||
index::ls(cache)
|
||||
pub fn all<P: AsRef<Path>>(cache: P) -> impl Iterator {
|
||||
index::ls(cache.as_ref())
|
||||
}
|
||||
|
|
|
|||
18
src/put.rs
18
src/put.rs
|
|
@ -9,9 +9,9 @@ use crate::content::write;
|
|||
use crate::index;
|
||||
use crate::errors::Error;
|
||||
|
||||
pub fn data(cache: &Path, key: &str, data: Vec<u8>) -> Result<Integrity, Error> {
|
||||
let sri = write::write(&cache, &data)?;
|
||||
Writer::new(cache, &key).integrity(sri).commit(data)
|
||||
pub fn data<P: AsRef<Path>, D: AsRef<[u8]>, K: AsRef<str>>(cache: P, key: K, data: D) -> Result<Integrity, Error> {
|
||||
let sri = write::write(cache.as_ref(), data.as_ref())?;
|
||||
Writer::new(cache.as_ref(), key.as_ref()).integrity(sri).commit(data)
|
||||
}
|
||||
|
||||
pub struct Writer {
|
||||
|
|
@ -26,10 +26,10 @@ pub struct Writer {
|
|||
}
|
||||
|
||||
impl Writer {
|
||||
pub fn new(cache: &Path, key: &str) -> Writer {
|
||||
pub fn new<P: AsRef<Path>, K: AsRef<str>>(cache: P, key: K) -> Writer {
|
||||
Writer {
|
||||
cache: cache.to_path_buf(),
|
||||
key: String::from(key),
|
||||
cache: cache.as_ref().to_path_buf(),
|
||||
key: String::from(key.as_ref()),
|
||||
sri: None,
|
||||
size: None,
|
||||
time: None,
|
||||
|
|
@ -65,18 +65,18 @@ impl Writer {
|
|||
self
|
||||
}
|
||||
|
||||
pub fn commit(self, data: Vec<u8>) -> Result<Integrity, Error> {
|
||||
pub fn commit<D: AsRef<[u8]>>(self, data: D) -> Result<Integrity, Error> {
|
||||
if let Some(sri) = &self.sri {
|
||||
if sri.clone().check(&data).is_none() {
|
||||
return Err(Error::IntegrityError);
|
||||
}
|
||||
}
|
||||
if let Some(size) = self.size {
|
||||
if size != data.len() {
|
||||
if size != data.as_ref().len() {
|
||||
return Err(Error::SizeError);
|
||||
}
|
||||
}
|
||||
let sri = write::write(&self.cache, &data)?;
|
||||
let sri = write::write(&self.cache, data.as_ref())?;
|
||||
index::insert(self)?;
|
||||
Ok(sri)
|
||||
}
|
||||
|
|
|
|||
12
src/rm.rs
12
src/rm.rs
|
|
@ -8,16 +8,16 @@ use crate::content::rm;
|
|||
use crate::errors::Error;
|
||||
use crate::index;
|
||||
|
||||
pub fn entry(cache: &Path, key: &str) -> Result<(), Error> {
|
||||
index::delete(&cache, &key)
|
||||
pub fn entry<P: AsRef<Path>>(cache: P, key: &str) -> Result<(), Error> {
|
||||
index::delete(cache.as_ref(), &key)
|
||||
}
|
||||
|
||||
pub fn content(cache: &Path, sri: &Integrity) -> Result<(), Error> {
|
||||
rm::rm(&cache, &sri)
|
||||
pub fn content<P: AsRef<Path>>(cache: P, sri: &Integrity) -> Result<(), Error> {
|
||||
rm::rm(cache.as_ref(), &sri)
|
||||
}
|
||||
|
||||
pub fn all(cache: &Path) -> Result<(), Error> {
|
||||
for entry in cache.read_dir()? {
|
||||
pub fn all<P: AsRef<Path>>(cache: P) -> Result<(), Error> {
|
||||
for entry in cache.as_ref().read_dir()? {
|
||||
if let Ok(entry) = entry {
|
||||
fs::remove_dir_all(entry.path())?;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue