migrate some api to use AsRef

This commit is contained in:
Cyryl Płotnicki 2020-12-25 16:29:00 +00:00
parent 4266f48b03
commit 769bc6b9f7
7 changed files with 97 additions and 21 deletions

73
Cargo.lock generated
View file

@ -38,7 +38,7 @@ dependencies = [
"log",
"nix 0.19.0",
"proptest",
"rand 0.7.3",
"rand 0.8.0",
"rayon",
"rust-crypto",
"serde",
@ -48,6 +48,7 @@ dependencies = [
"tempfile",
"thiserror",
"uuid",
"vfs",
"walkdir",
]
@ -262,6 +263,17 @@ dependencies = [
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "getrandom"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ee8025cf36f917e6a52cce185b7c7177689b838b7ec138364e50cc2277a56cf4"
dependencies = [
"cfg-if 0.1.10",
"libc",
"wasi 0.9.0+wasi-snapshot-preview1",
]
[[package]]
name = "glob"
version = "0.3.0"
@ -412,7 +424,7 @@ dependencies = [
"num-traits",
"quick-error",
"rand 0.7.3",
"rand_chacha",
"rand_chacha 0.2.2",
"rand_xorshift",
"regex-syntax",
"rusty-fork",
@ -463,11 +475,23 @@ version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [
"getrandom",
"getrandom 0.1.15",
"libc",
"rand_chacha",
"rand_chacha 0.2.2",
"rand_core 0.5.1",
"rand_hc",
"rand_hc 0.2.0",
]
[[package]]
name = "rand"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a76330fb486679b4ace3670f117bbc9e16204005c4bde9c4bd372f45bed34f12"
dependencies = [
"libc",
"rand_chacha 0.3.0",
"rand_core 0.6.0",
"rand_hc 0.3.0",
]
[[package]]
@ -480,6 +504,16 @@ dependencies = [
"rand_core 0.5.1",
]
[[package]]
name = "rand_chacha"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d"
dependencies = [
"ppv-lite86",
"rand_core 0.6.0",
]
[[package]]
name = "rand_core"
version = "0.3.1"
@ -501,7 +535,16 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [
"getrandom",
"getrandom 0.1.15",
]
[[package]]
name = "rand_core"
version = "0.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a8b34ba8cfb21243bd8df91854c830ff0d785fff2e82ebd4434c2644cb9ada18"
dependencies = [
"getrandom 0.2.0",
]
[[package]]
@ -513,6 +556,15 @@ dependencies = [
"rand_core 0.5.1",
]
[[package]]
name = "rand_hc"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73"
dependencies = [
"rand_core 0.6.0",
]
[[package]]
name = "rand_xorshift"
version = "0.2.0"
@ -776,6 +828,15 @@ version = "0.9.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
[[package]]
name = "vfs"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7cc382cffc2f8d6f9490b96d42a7408d6aa719c2db86491b8b31780208ac9d86"
dependencies = [
"thiserror",
]
[[package]]
name = "void"
version = "1.0.2"

View file

@ -15,7 +15,7 @@ glob = "0.3"
hex = "0.4"
log = "0.4"
nix = "0.19"
rand = "0.7"
rand = "0.8"
rayon = "1.5"
rust-crypto = "0.2"
serde = { version = "1.0", features = ["derive"] }
@ -29,6 +29,7 @@ walkdir = "2.3"
[dev-dependencies]
proptest = "0.10"
vfs = "0.4"
[dev-dependencies.cargo-husky]
version = "1"

View file

@ -8,11 +8,11 @@ use crate::repository::Repository;
pub struct Engine<'a> {
source_path: &'a Path,
repository: &'a mut Repository<'a>,
repository: &'a mut Repository,
}
impl<'a> Engine<'a> {
pub fn new(source_path: &'a Path, repository: &'a mut Repository<'a>) -> Result<Self> {
pub fn new(source_path: &'a Path, repository: &'a mut Repository) -> Result<Self> {
if source_path.ancestors().any(|a| a == repository.path()) {
return Err(anyhow!("source same as repository"));
}

View file

@ -15,7 +15,8 @@ use nix::unistd::getpid;
use std::{cmp::max, io::Write};
impl Index {
pub fn load(repository_path: &Path) -> Result<Self> {
pub fn load<T: AsRef<Path>>(repository_path: T) -> Result<Self> {
let repository_path = repository_path.as_ref();
if !repository_path.exists() {
let mut index = Index::new(repository_path);
index.save()?;

View file

@ -24,7 +24,8 @@ pub struct Index {
}
impl Index {
pub fn new(repository_path: &Path) -> Self {
pub fn new<T: AsRef<Path>>(repository_path: T) -> Self {
let repository_path = repository_path.as_ref();
Index {
newest_items_by_source_path: Default::default(),
items_by_file_id: Default::default(),

View file

@ -19,9 +19,9 @@ use walkdir::WalkDir;
/// right now only on-disk directory storage is supported
/// repository always knows the newest version of the index and is responsible for syncing the index to disk
/// and making sure that different threads can access index in parallel
pub struct Repository<'a> {
pub struct Repository {
/// absolute path to where the repository is stored on disk
path: &'a Path,
path: PathBuf,
index: Index,
}
@ -81,20 +81,32 @@ impl fmt::Display for ItemId {
}
}
impl<'a> Repository<'a> {
pub fn init(path: &Path) -> Result<()> {
let mut index = Index::new(path);
impl<'a> Repository {
pub fn init<T>(path: T) -> Result<()>
where
T: AsRef<Path>,
{
let mut index = Index::new(path.as_ref());
index.save()?;
Ok(())
}
pub fn open(path: &Path) -> Result<Repository> {
pub fn open<T>(path: T) -> Result<Repository>
where
T: AsRef<Path>,
{
let path = path.as_ref();
if !path.is_absolute() {
return Err(anyhow!("path to repository not absolute"));
}
let index = Index::load(path)?;
Ok(Repository { path, index })
let mut repository_path = PathBuf::new();
repository_path.push(path);
Ok(Repository {
path: repository_path,
index,
})
}
pub fn path(&self) -> &Path {
@ -120,7 +132,7 @@ impl<'a> Repository<'a> {
.ok_or_else(|| anyhow!("cannot compute parent path for {}", &destination_path.to_string_lossy()))?;
fs::create_dir_all(parent)?;
fs::copy(source_path, destination_path)?;
let relative_path = destination_path.strip_prefix(self.path)?;
let relative_path = destination_path.strip_prefix(&self.path)?;
self.index.remember(source_path, relative_path, id);
}
Ok(())

View file

@ -5,12 +5,12 @@ use anyhow::Result;
use anyhow::*;
pub struct Engine<'a> {
repository: &'a mut Repository<'a>,
repository: &'a mut Repository,
target_path: &'a Path,
}
impl<'a> Engine<'a> {
pub fn new(repository: &'a mut Repository<'a>, target_path: &'a Path) -> Result<Self> {
pub fn new(repository: &'a mut Repository, target_path: &'a Path) -> Result<Self> {
if !target_path.is_absolute() {
return Err(anyhow!("path to store not absolute"));
}