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::errors::Error;
|
||||||
use crate::index::{self, Entry};
|
use crate::index::{self, Entry};
|
||||||
|
|
||||||
pub fn read(cache: &Path, key: &str) -> Result<Vec<u8>, Error> {
|
pub fn read<P: AsRef<Path>, K: AsRef<str>>(cache: P, key: K) -> Result<Vec<u8>, Error> {
|
||||||
if let Some(entry) = index::find(&cache, &key)? {
|
if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? {
|
||||||
read_hash(cache, &entry.integrity)
|
read_hash(cache, &entry.integrity)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::NotFound)
|
Err(Error::NotFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn read_hash(cache: &Path, sri: &Integrity) -> Result<Vec<u8>, Error> {
|
pub fn read_hash<P: AsRef<Path>>(cache: P, sri: &Integrity) -> Result<Vec<u8>, Error> {
|
||||||
Ok(read::read(cache, sri)?)
|
Ok(read::read(cache.as_ref(), sri)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn copy(cache: &Path, key: &str, to: &Path) -> Result<u64, Error> {
|
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, &key)? {
|
if let Some(entry) = index::find(cache.as_ref(), key.as_ref())? {
|
||||||
copy_hash(cache, &entry.integrity, to)
|
copy_hash(cache, &entry.integrity, to)
|
||||||
} else {
|
} else {
|
||||||
Err(Error::NotFound)
|
Err(Error::NotFound)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn copy_hash(cache: &Path, sri: &Integrity, to: &Path) -> Result<u64, Error> {
|
pub fn copy_hash<P: AsRef<Path>>(cache: P, sri: &Integrity, to: P) -> Result<u64, Error> {
|
||||||
Ok(read::copy(cache, sri, to)?)
|
Ok(read::copy(cache.as_ref(), sri, to.as_ref())?)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn info(cache: &Path, key: &str) -> Result<Option<Entry>, Error> {
|
pub fn info<P: AsRef<Path>, K: AsRef<str>>(cache: P, key: K) -> Result<Option<Entry>, Error> {
|
||||||
index::find(cache, &key)
|
index::find(cache.as_ref(), key.as_ref())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn hash_exists(cache: &Path, sri: &Integrity) -> bool {
|
pub fn hash_exists<P: AsRef<Path>>(cache: P, sri: &Integrity) -> bool {
|
||||||
read::has_content(&cache, &sri).is_some()
|
read::has_content(cache.as_ref(), &sri).is_some()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,6 @@ use std::path::Path;
|
||||||
|
|
||||||
use crate::index;
|
use crate::index;
|
||||||
|
|
||||||
pub fn all(cache: &Path) -> impl Iterator {
|
pub fn all<P: AsRef<Path>>(cache: P) -> impl Iterator {
|
||||||
index::ls(cache)
|
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::index;
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
|
|
||||||
pub fn data(cache: &Path, key: &str, data: Vec<u8>) -> Result<Integrity, Error> {
|
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, &data)?;
|
let sri = write::write(cache.as_ref(), data.as_ref())?;
|
||||||
Writer::new(cache, &key).integrity(sri).commit(data)
|
Writer::new(cache.as_ref(), key.as_ref()).integrity(sri).commit(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Writer {
|
pub struct Writer {
|
||||||
|
|
@ -26,10 +26,10 @@ pub struct Writer {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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 {
|
Writer {
|
||||||
cache: cache.to_path_buf(),
|
cache: cache.as_ref().to_path_buf(),
|
||||||
key: String::from(key),
|
key: String::from(key.as_ref()),
|
||||||
sri: None,
|
sri: None,
|
||||||
size: None,
|
size: None,
|
||||||
time: None,
|
time: None,
|
||||||
|
|
@ -65,18 +65,18 @@ impl Writer {
|
||||||
self
|
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 let Some(sri) = &self.sri {
|
||||||
if sri.clone().check(&data).is_none() {
|
if sri.clone().check(&data).is_none() {
|
||||||
return Err(Error::IntegrityError);
|
return Err(Error::IntegrityError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(size) = self.size {
|
if let Some(size) = self.size {
|
||||||
if size != data.len() {
|
if size != data.as_ref().len() {
|
||||||
return Err(Error::SizeError);
|
return Err(Error::SizeError);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let sri = write::write(&self.cache, &data)?;
|
let sri = write::write(&self.cache, data.as_ref())?;
|
||||||
index::insert(self)?;
|
index::insert(self)?;
|
||||||
Ok(sri)
|
Ok(sri)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
src/rm.rs
12
src/rm.rs
|
|
@ -8,16 +8,16 @@ use crate::content::rm;
|
||||||
use crate::errors::Error;
|
use crate::errors::Error;
|
||||||
use crate::index;
|
use crate::index;
|
||||||
|
|
||||||
pub fn entry(cache: &Path, key: &str) -> Result<(), Error> {
|
pub fn entry<P: AsRef<Path>>(cache: P, key: &str) -> Result<(), Error> {
|
||||||
index::delete(&cache, &key)
|
index::delete(cache.as_ref(), &key)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn content(cache: &Path, sri: &Integrity) -> Result<(), Error> {
|
pub fn content<P: AsRef<Path>>(cache: P, sri: &Integrity) -> Result<(), Error> {
|
||||||
rm::rm(&cache, &sri)
|
rm::rm(cache.as_ref(), &sri)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn all(cache: &Path) -> Result<(), Error> {
|
pub fn all<P: AsRef<Path>>(cache: P) -> Result<(), Error> {
|
||||||
for entry in cache.read_dir()? {
|
for entry in cache.as_ref().read_dir()? {
|
||||||
if let Ok(entry) = entry {
|
if let Ok(entry) = entry {
|
||||||
fs::remove_dir_all(entry.path())?;
|
fs::remove_dir_all(entry.path())?;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue