more consistent use of asref

This commit is contained in:
Cyryl Płotnicki 2020-12-25 16:40:03 +00:00
parent 769bc6b9f7
commit 94ccf98a1c
4 changed files with 13 additions and 19 deletions

View file

@ -43,20 +43,16 @@ impl Index {
self.version = max(self.version, index.version); self.version = max(self.version, index.version);
} }
self.version = self.version.next(); self.version = self.version.next();
self.write_index_to_file(self.index_file_path())?; self.write_index_to_file(&self.index_file_path())?;
lock.release()?; lock.release()?;
log::debug!("[{}] saved index version {} with lock id {}", getpid(), self.version, lock_id,); log::debug!("[{}] saved index version {} with lock id {}", getpid(), self.version, lock_id,);
Ok(()) Ok(())
} }
fn write_index_to_file<T>(&mut self, path: T) -> Result<()> fn write_index_to_file(&mut self, path: &Path) -> Result<()> {
where
T: AsRef<Path>,
{
fs::create_dir_all( fs::create_dir_all(
path.as_ref() path.parent()
.parent() .ok_or_else(|| anyhow!("cannot compute parent path for {}", path.to_string_lossy()))?,
.ok_or_else(|| anyhow!("cannot compute parent path for {}", path.as_ref().to_string_lossy()))?,
) )
.context("create index directory")?; .context("create index directory")?;

View file

@ -14,7 +14,8 @@ pub struct Lock {
} }
impl Lock { impl Lock {
pub fn lock(index_directory: &Path) -> Result<Self> { pub fn lock<T: AsRef<Path>>(index_directory: T) -> Result<Self> {
let index_directory = index_directory.as_ref();
let mut buffer = [0u8; 16]; let mut buffer = [0u8; 16];
OsRng.fill_bytes(&mut buffer); OsRng.fill_bytes(&mut buffer);
let id = Uuid::from_bytes(buffer); let id = Uuid::from_bytes(buffer);

View file

@ -35,7 +35,9 @@ impl Index {
} }
} }
pub fn remember(&mut self, original_source_path: &Path, relative_path: &Path, id: ItemId) { pub fn remember<S: AsRef<Path>, R: AsRef<Path>>(&mut self, original_source_path: S, relative_path: R, id: ItemId) {
let original_source_path = original_source_path.as_ref();
let relative_path = relative_path.as_ref();
let item = if let Some(old) = self let item = if let Some(old) = self
.newest_items_by_source_path .newest_items_by_source_path
.get(&original_source_path.to_string_lossy().to_string()) .get(&original_source_path.to_string_lossy().to_string())
@ -71,7 +73,8 @@ impl Index {
) )
} }
pub fn newest_item_by_source_path(&self, path: &Path) -> Result<Option<IndexItem>> { pub fn newest_item_by_source_path<T: AsRef<Path>>(&self, path: T) -> Result<Option<IndexItem>> {
let path = path.as_ref();
if !path.is_absolute() { if !path.is_absolute() {
return Err(anyhow!("repository path not absolute")); return Err(anyhow!("repository path not absolute"));
} }

View file

@ -82,19 +82,13 @@ impl fmt::Display for ItemId {
} }
impl<'a> Repository { impl<'a> Repository {
pub fn init<T>(path: T) -> Result<()> pub fn init<T: AsRef<Path>>(path: T) -> Result<()> {
where
T: AsRef<Path>,
{
let mut index = Index::new(path.as_ref()); let mut index = Index::new(path.as_ref());
index.save()?; index.save()?;
Ok(()) Ok(())
} }
pub fn open<T>(path: T) -> Result<Repository> pub fn open<T: AsRef<Path>>(path: T) -> Result<Repository> {
where
T: AsRef<Path>,
{
let path = path.as_ref(); let path = path.as_ref();
if !path.is_absolute() { if !path.is_absolute() {
return Err(anyhow!("path to repository not absolute")); return Err(anyhow!("path to repository not absolute"));