Skip to content

Commit

Permalink
Don't use doccomments for repository paths
Browse files Browse the repository at this point in the history
  • Loading branch information
aawsome committed Aug 14, 2023
1 parent d3ba595 commit d7bfe84
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 20 deletions.
11 changes: 4 additions & 7 deletions crates/rustic_core/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub(crate) mod stdin;
use std::{io::Read, path::PathBuf};

use bytes::Bytes;
use displaydoc::Display;
use log::trace;
use serde::{Deserialize, Serialize};

Expand All @@ -28,7 +27,7 @@ pub const ALL_FILE_TYPES: [FileType; 4] = [
];

/// Type for describing the kind of a file that can occur.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Display, Serialize, Deserialize)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum FileType {
/// Config file
#[serde(rename = "config")]
Expand All @@ -47,19 +46,17 @@ pub enum FileType {
Pack,
}

impl From<FileType> for &'static str {
fn from(value: FileType) -> &'static str {
match value {
impl FileType {
const fn dirname(self) -> &'static str {
match self {
FileType::Config => "config",
FileType::Snapshot => "snapshots",
FileType::Index => "index",
FileType::Key => "keys",
FileType::Pack => "data",
}
}
}

impl FileType {
/// Returns if the file type is cacheable.
const fn is_cacheable(self) -> bool {
match self {
Expand Down
8 changes: 4 additions & 4 deletions crates/rustic_core/src/backend/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use walkdir::WalkDir;
use crate::{
backend::{FileType, ReadBackend, WriteBackend},
error::CacheBackendErrorKind,
id::Id,
error::RusticResult,
id::Id,
};

/// Backend that caches data.
Expand Down Expand Up @@ -267,7 +267,7 @@ impl Cache {
#[must_use]
pub fn dir(&self, tpe: FileType, id: &Id) -> PathBuf {
let hex_id = id.to_hex();
self.path.join(tpe.to_string()).join(&hex_id[0..2])
self.path.join(tpe.dirname()).join(&hex_id[0..2])
}

/// Returns the path to the given file.
Expand All @@ -280,7 +280,7 @@ impl Cache {
pub fn path(&self, tpe: FileType, id: &Id) -> PathBuf {
let hex_id = id.to_hex();
self.path
.join(tpe.to_string())
.join(tpe.dirname())
.join(&hex_id[0..2])
.join(hex_id)
}
Expand All @@ -295,7 +295,7 @@ impl Cache {
///
/// * [`CacheBackendErrorKind::FromIoError`] - If the cache directory could not be read.
pub fn list_with_size(&self, tpe: FileType) -> RusticResult<HashMap<Id, u32>> {
let path = self.path.join(tpe.to_string());
let path = self.path.join(tpe.dirname());

let walker = WalkDir::new(path)
.into_iter()
Expand Down
10 changes: 5 additions & 5 deletions crates/rustic_core/src/backend/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl LocalBackend {
match tpe {
FileType::Config => self.path.join("config"),
FileType::Pack => self.path.join("data").join(&hex_id[0..2]).join(hex_id),
_ => self.path.join(tpe.to_string()).join(hex_id),
_ => self.path.join(tpe.dirname()).join(hex_id),
}
}

Expand Down Expand Up @@ -140,7 +140,7 @@ impl LocalBackend {
let id = id.to_hex();
let patterns = &["%file", "%type", "%id"];
let ac = AhoCorasick::new(patterns).map_err(LocalErrorKind::FromAhoCorasick)?;
let replace_with = &[filename.to_str().unwrap(), tpe.into(), id.as_str()];
let replace_with = &[filename.to_str().unwrap(), tpe.dirname(), id.as_str()];
let actual_command = ac.replace_all(command, replace_with);
debug!("calling {actual_command}...");
let commands = parse_command::<()>(&actual_command)
Expand Down Expand Up @@ -219,7 +219,7 @@ impl ReadBackend for LocalBackend {
});
}

let walker = WalkDir::new(self.path.join(tpe.to_string()))
let walker = WalkDir::new(self.path.join(tpe.dirname()))
.into_iter()
.filter_map(walkdir::Result::ok)
.filter(|e| e.file_type().is_file())
Expand All @@ -242,7 +242,7 @@ impl ReadBackend for LocalBackend {
///
fn list_with_size(&self, tpe: FileType) -> RusticResult<Vec<(Id, u32)>> {
trace!("listing tpe: {tpe:?}");
let path = self.path.join(tpe.to_string());
let path = self.path.join(tpe.dirname());

if tpe == FileType::Config {
return Ok(if path.exists() {
Expand Down Expand Up @@ -345,7 +345,7 @@ impl WriteBackend for LocalBackend {
trace!("creating repo at {:?}", self.path);

for tpe in ALL_FILE_TYPES {
fs::create_dir_all(self.path.join(tpe.to_string()))
fs::create_dir_all(self.path.join(tpe.dirname()))
.map_err(LocalErrorKind::DirectoryCreationFailed)?;
}
for i in 0u8..=255 {
Expand Down
4 changes: 2 additions & 2 deletions crates/rustic_core/src/backend/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ impl RestBackend {
"config".to_string()
} else {
let hex_id = id.to_hex();
let mut path = tpe.to_string();
let mut path = tpe.dirname().to_string();
path.push('/');
path.push_str(&hex_id);
path
Expand Down Expand Up @@ -256,7 +256,7 @@ impl ReadBackend for RestBackend {
.join("config")
.map_err(RestErrorKind::JoiningUrlFailed)?
} else {
let mut path = tpe.to_string();
let mut path = tpe.dirname().to_string();
path.push('/');
self.url
.join(&path)
Expand Down
4 changes: 2 additions & 2 deletions crates/rustic_core/src/commands/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl CheckOptions {
// TODO: Only list the files once...
_ = be.list_with_size(file_type)?;

let p = pb.progress_bytes(format!("checking {file_type} in cache..."));
let p = pb.progress_bytes(format!("checking {file_type:?} in cache..."));
// TODO: Make concurrency (20) customizable
check_cache_files(20, cache, raw_be, file_type, &p)?;
}
Expand Down Expand Up @@ -150,7 +150,7 @@ fn check_hot_files(
file_type: FileType,
pb: &impl ProgressBars,
) -> RusticResult<()> {
let p = pb.progress_spinner(format!("checking {file_type} in hot repo..."));
let p = pb.progress_spinner(format!("checking {file_type:?} in hot repo..."));
let mut files = be
.list_with_size(file_type)?
.into_iter()
Expand Down

0 comments on commit d7bfe84

Please sign in to comment.