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

View file

@ -14,7 +14,8 @@ pub struct 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];
OsRng.fill_bytes(&mut 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
.newest_items_by_source_path
.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() {
return Err(anyhow!("repository path not absolute"));
}

View file

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