From 5af622eb30b9f177117ce2f8ad17690313fba50a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Sun, 9 Jun 2019 10:59:36 -0700 Subject: [PATCH] feat(api): AsRef all the things! --- src/get.rs | 24 ++++++++++++------------ src/ls.rs | 4 ++-- src/put.rs | 18 +++++++++--------- src/rm.rs | 12 ++++++------ 4 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/get.rs b/src/get.rs index c5547a8..b1f8f97 100644 --- a/src/get.rs +++ b/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, Error> { - if let Some(entry) = index::find(&cache, &key)? { +pub fn read, K: AsRef>(cache: P, key: K) -> Result, 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, Error> { - Ok(read::read(cache, sri)?) +pub fn read_hash>(cache: P, sri: &Integrity) -> Result, Error> { + Ok(read::read(cache.as_ref(), sri)?) } -pub fn copy(cache: &Path, key: &str, to: &Path) -> Result { - if let Some(entry) = index::find(&cache, &key)? { +pub fn copy, K: AsRef>(cache: P, key: K, to: P) -> Result { + 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 { - Ok(read::copy(cache, sri, to)?) +pub fn copy_hash>(cache: P, sri: &Integrity, to: P) -> Result { + Ok(read::copy(cache.as_ref(), sri, to.as_ref())?) } -pub fn info(cache: &Path, key: &str) -> Result, Error> { - index::find(cache, &key) +pub fn info, K: AsRef>(cache: P, key: K) -> Result, 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>(cache: P, sri: &Integrity) -> bool { + read::has_content(cache.as_ref(), &sri).is_some() } diff --git a/src/ls.rs b/src/ls.rs index 73e5938..a6f18f6 100644 --- a/src/ls.rs +++ b/src/ls.rs @@ -3,6 +3,6 @@ use std::path::Path; use crate::index; -pub fn all(cache: &Path) -> impl Iterator { - index::ls(cache) +pub fn all>(cache: P) -> impl Iterator { + index::ls(cache.as_ref()) } diff --git a/src/put.rs b/src/put.rs index 049c341..39917f5 100644 --- a/src/put.rs +++ b/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) -> Result { - let sri = write::write(&cache, &data)?; - Writer::new(cache, &key).integrity(sri).commit(data) +pub fn data, D: AsRef<[u8]>, K: AsRef>(cache: P, key: K, data: D) -> Result { + 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, K: AsRef>(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) -> Result { + pub fn commit>(self, data: D) -> Result { 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) } diff --git a/src/rm.rs b/src/rm.rs index 7be1387..503e05a 100644 --- a/src/rm.rs +++ b/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>(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>(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>(cache: P) -> Result<(), Error> { + for entry in cache.as_ref().read_dir()? { if let Ok(entry) = entry { fs::remove_dir_all(entry.path())?; }