Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor cleanup, resolve some Clippy lints #155

Merged
merged 18 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "dotter"
version = "0.13.2"
authors = ["SuperCuber <[email protected]>"]
description = "A dotfile manager and templater written in rust"
edition = "2018"
edition = "2021"
repository = "https://github.com/SuperCuber/dotter"
readme = "README.md"
keywords = ["dotter", "dotfiles", "manager"]
Expand Down
4 changes: 2 additions & 2 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct RealActionRunner<'a> {
impl<'a> RealActionRunner<'a> {
pub fn new(
fs: &'a mut dyn Filesystem,
handlebars: &'a Handlebars,
handlebars: &'a Handlebars<'_>,
variables: &'a Variables,
force: bool,
diff_context_lines: usize,
Expand Down Expand Up @@ -544,7 +544,7 @@ pub fn update_template(
);
if log_enabled!(log::Level::Info) {
info!("Refusing because of the following changes in target location: ");
print_diff(diff, diff_context_lines);
print_diff(&diff, diff_context_lines);
}
Ok(false)
} else {
Expand Down
60 changes: 28 additions & 32 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ pub enum UnixUser {
impl fmt::Display for UnixUser {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
UnixUser::Uid(uid) => write!(f, "{}", uid),
UnixUser::Name(name) => write!(f, "{}", name),
UnixUser::Uid(uid) => write!(f, "{uid}"),
UnixUser::Name(name) => write!(f, "{name}"),
}
}
}
Expand Down Expand Up @@ -133,7 +133,7 @@ pub fn load_configuration(
) -> Result<Configuration> {
let global: GlobalConfig = filesystem::load_file(global_config)
.and_then(|c| c.ok_or_else(|| anyhow::anyhow!("file not found")))
.with_context(|| format!("load global config {:?}", global_config))?;
.with_context(|| format!("load global config {global_config:?}"))?;
trace!("Global config: {:#?}", global);

// If local.toml can't be found, look for a file named <hostname>.toml instead
Expand All @@ -147,12 +147,12 @@ pub fn load_configuration(
"{:?} not found, using {}.toml instead (based on hostname)",
local_config, hostname
);
local_config_buf.set_file_name(&format!("{}.toml", hostname));
local_config_buf.set_file_name(&format!("{hostname}.toml"));
}

let local: LocalConfig = filesystem::load_file(local_config_buf.as_path())
.and_then(|c| c.ok_or_else(|| anyhow::anyhow!("file not found")))
.with_context(|| format!("load local config {:?}", local_config))?;
.with_context(|| format!("load local config {local_config:?}"))?;
trace!("Local config: {:#?}", local);

let mut merged_config =
Expand Down Expand Up @@ -290,7 +290,7 @@ fn merge_configuration_files(

Ok(())
}()
.with_context(|| format!("including file {:?}", included_path))?;
.with_context(|| format!("including file {included_path:?}"))?;
}

// Enable depended packages
Expand All @@ -305,7 +305,7 @@ fn merge_configuration_files(
global
.packages
.get(package)
.with_context(|| format!("get info of package {}", package))?
.with_context(|| format!("get info of package {package}"))?
.depends
.clone(),
);
Expand Down Expand Up @@ -343,9 +343,8 @@ fn merge_configuration_files(
for (file_name, file_target) in package.files {
if first_package.files.contains_key(&file_name) {
anyhow::bail!("file {:?} already encountered", file_name);
} else {
first_package.files.insert(file_name, file_target);
}
first_package.files.insert(file_name, file_target);
}

for (variable_name, variable_value) in package.variables {
Expand All @@ -369,7 +368,7 @@ fn merge_configuration_files(

Ok(())
}()
.with_context(|| format!("merge package {:?}", package_name))?;
.with_context(|| format!("merge package {package_name:?}"))?;
}
output.files = first_package.files;
output.variables = first_package.variables;
Expand Down Expand Up @@ -404,16 +403,16 @@ impl FileTarget {
FileTarget::Automatic(ref mut path) => *path = new_path.into(),
FileTarget::Symbolic(SymbolicTarget { target, .. })
| FileTarget::ComplexTemplate(TemplateTarget { target, .. }) => {
*target = new_path.into()
*target = new_path.into();
}
}
}

pub fn condition(&self) -> Option<&String> {
match self {
FileTarget::Automatic(_) => None,
FileTarget::Symbolic(SymbolicTarget { condition, .. }) => condition.as_ref(),
FileTarget::ComplexTemplate(TemplateTarget { condition, .. }) => condition.as_ref(),
FileTarget::Symbolic(SymbolicTarget { condition, .. })
| FileTarget::ComplexTemplate(TemplateTarget { condition, .. }) => condition.as_ref(),
}
}
}
Expand Down Expand Up @@ -500,7 +499,7 @@ fn expand_directories(config: &Configuration) -> Result<Files> {
.files
.iter()
.map(|(source, target)| {
expand_directory(source, target, config).context(format!("expand file {:?}", source))
expand_directory(source, target, config).context(format!("expand file {source:?}"))
})
.collect::<Result<Vec<Files>>>()?;
Ok(expanded.into_iter().flatten().collect::<Files>())
Expand Down Expand Up @@ -539,7 +538,7 @@ fn expand_directory(source: &Path, target: &FileTarget, config: &Configuration)
let mut child_target = target.clone();
child_target.set_path(child_target.path().join(&child));
expand_directory(&child_source, &child_target, config)
.context(format!("expand file {:?}", child_source))
.context(format!("expand file {child_source:?}"))
})
.collect::<Result<Vec<Files>>>()?; // Use transposition of Iterator<Result<T,E>> -> Result<Sequence<T>, E>
Ok(expanded.into_iter().flatten().collect())
Expand All @@ -551,30 +550,30 @@ impl UnixUser {
pub fn as_sudo_arg(&self) -> String {
match self {
UnixUser::Name(n) => n.clone(),
UnixUser::Uid(id) => format!("#{}", id),
UnixUser::Uid(id) => format!("#{id}"),
}
}

pub fn as_chown_arg(&self) -> String {
match self {
UnixUser::Name(n) => n.clone(),
UnixUser::Uid(id) => format!("{}", id),
UnixUser::Uid(id) => format!("{id}"),
}
}
}

#[cfg(test)]
mod tests {
mod test {
use super::*;

#[test]
fn deserialize_file_target() {
#[derive(Deserialize)]
#[derive(Debug, Deserialize)]
struct Helper {
file: FileTarget,
}

let parse = |s| toml::from_str::<Helper>(s);
let parse = toml::from_str::<Helper>;

assert_eq!(
parse(
Expand Down Expand Up @@ -623,17 +622,14 @@ mod tests {
.file,
FileTarget::ComplexTemplate(PathBuf::from("~/.QuarticCat").into()),
);
assert_eq!(
parse(
r#"
[file]
target = '~/.QuarticCat'
type = 'symbolic'
append = 'whatever'
"#,
)
.is_err(),
true
);
parse(
r#"
[file]
target = '~/.QuarticCat'
type = 'symbolic'
append = 'whatever'
"#,
)
.unwrap_err();
}
}
44 changes: 22 additions & 22 deletions src/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Proceeding by copying instead of symlinking."
match target {
FileTarget::Automatic(target) => {
if filesystem::is_template(&source)
.context(format!("check whether {:?} is a template", source))?
.context(format!("check whether {source:?} is a template"))?
{
desired_templates.insert(source, target.into());
} else {
Expand Down Expand Up @@ -157,7 +157,7 @@ Proceeding by copying instead of symlinking."
Ok(error_occurred)
}

pub fn undeploy(opt: Options) -> Result<bool> {
pub fn undeploy(opt: &Options) -> Result<bool> {
// === Load configuration ===
let mut config = config::load_configuration(&opt.local_config, &opt.global_config, None)
.context("get a configuration")?;
Expand Down Expand Up @@ -198,7 +198,7 @@ pub fn undeploy(opt: Options) -> Result<bool> {
execute_action(
actions::delete_symlink(&deleted_symlink, &target, fs, opt.force),
|| cache.symlinks.remove(&deleted_symlink),
|| format!("delete symlink {:?} -> {:?}", deleted_symlink, target),
|| format!("delete symlink {deleted_symlink:?} -> {target:?}"),
&mut suggest_force,
&mut error_occurred,
);
Expand All @@ -214,7 +214,7 @@ pub fn undeploy(opt: Options) -> Result<bool> {
opt.force,
),
|| cache.templates.remove(&deleted_template),
|| format!("delete template {:?} -> {:?}", deleted_template, target),
|| format!("delete template {deleted_template:?} -> {target:?}"),
&mut suggest_force,
&mut error_occurred,
);
Expand Down Expand Up @@ -287,7 +287,7 @@ fn run_deploy<A: ActionRunner>(
execute_action(
runner.delete_symlink(source, target),
|| resulting_cache.symlinks.remove(source),
|| format!("delete symlink {:?} -> {:?}", source, target),
|| format!("delete symlink {source:?} -> {target:?}"),
&mut suggest_force,
&mut error_occurred,
);
Expand All @@ -299,7 +299,7 @@ fn run_deploy<A: ActionRunner>(
execute_action(
runner.delete_template(source, &opt.cache_directory.join(source), target),
|| resulting_cache.templates.remove(source),
|| format!("delete template {:?} -> {:?}", source, target),
|| format!("delete template {source:?} -> {target:?}"),
&mut suggest_force,
&mut error_occurred,
);
Expand All @@ -321,7 +321,7 @@ fn run_deploy<A: ActionRunner>(
.symlinks
.insert(source.clone(), target_path.clone())
},
|| format!("create symlink {:?} -> {:?}", source, target_path),
|| format!("create symlink {source:?} -> {target_path:?}"),
&mut suggest_force,
&mut error_occurred,
);
Expand All @@ -343,7 +343,7 @@ fn run_deploy<A: ActionRunner>(
.templates
.insert(source.clone(), target_path.clone())
},
|| format!("create template {:?} -> {:?}", source, target_path),
|| format!("create template {source:?} -> {target_path:?}"),
&mut suggest_force,
&mut error_occurred,
);
Expand All @@ -358,7 +358,7 @@ fn run_deploy<A: ActionRunner>(
execute_action(
runner.update_symlink(source, target),
|| (),
|| format!("update symlink {:?} -> {:?}", source, target_path),
|| format!("update symlink {source:?} -> {target_path:?}"),
&mut suggest_force,
&mut error_occurred,
);
Expand All @@ -373,7 +373,7 @@ fn run_deploy<A: ActionRunner>(
execute_action(
runner.update_template(source, &opt.cache_directory.join(source), target),
|| (),
|| format!("update template {:?} -> {:?}", source, target_path),
|| format!("update template {source:?} -> {target_path:?}"),
&mut suggest_force,
&mut error_occurred,
);
Expand Down Expand Up @@ -468,8 +468,8 @@ mod test {
},
);

assert_eq!(suggest_force, false);
assert_eq!(error_occurred, false);
assert!(!suggest_force);
assert!(!error_occurred);

assert!(cache.symlinks.contains_key(&PathBuf::from("a_in")));
assert!(cache.templates.contains_key(&PathBuf::from("b_in")));
Expand Down Expand Up @@ -525,8 +525,8 @@ mod test {
},
);

assert_eq!(suggest_force, true);
assert_eq!(error_occurred, true);
assert!(suggest_force);
assert!(error_occurred);

assert_eq!(cache.symlinks.len(), 0);
assert_eq!(cache.templates.len(), 0);
Expand Down Expand Up @@ -577,8 +577,8 @@ mod test {
},
);

assert_eq!(suggest_force, false);
assert_eq!(error_occurred, false);
assert!(!suggest_force);
assert!(!error_occurred);

assert_eq!(cache.symlinks.len(), 1);
assert_eq!(cache.templates.len(), 0);
Expand Down Expand Up @@ -633,8 +633,8 @@ mod test {
},
);

assert_eq!(suggest_force, false);
assert_eq!(error_occurred, false);
assert!(!suggest_force);
assert!(!error_occurred);

assert_eq!(cache.symlinks.len(), 1);
assert_eq!(cache.templates.len(), 0);
Expand Down Expand Up @@ -682,8 +682,8 @@ mod test {
},
);

assert_eq!(suggest_force, false);
assert_eq!(error_occurred, false);
assert!(!suggest_force);
assert!(!error_occurred);

assert_eq!(cache.symlinks.len(), 1);
assert_eq!(cache.templates.len(), 0);
Expand All @@ -697,7 +697,7 @@ mod test {

let opt = Options::default();
let handlebars = handlebars::Handlebars::new();
let variables = Default::default();
let variables = BTreeMap::new();

// Expectation:
// create_symlink
Expand Down Expand Up @@ -800,7 +800,7 @@ mod test {

let opt = Options::default();
let handlebars = handlebars::Handlebars::new();
let variables = Default::default();
let variables = BTreeMap::new();

// Expectation:
// create_symlink
Expand Down
6 changes: 3 additions & 3 deletions src/difference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub fn print_template_diff(
source,
target.target
);
print_diff(diff, diff_context_lines);
print_diff(&diff, diff_context_lines);
}
}
Err(e) => {
Expand Down Expand Up @@ -86,7 +86,7 @@ pub fn diff_nonempty(diff: &[diff::Result<String>]) -> bool {
false
}

fn hunkify_diff(diff: Diff, extra_lines: usize) -> HunkDiff {
fn hunkify_diff(diff: &[diff::Result<String>], extra_lines: usize) -> HunkDiff {
let mut hunks = vec![];

let mut left_line_number: usize = 1;
Expand Down Expand Up @@ -191,7 +191,7 @@ fn print_hunk(mut left_line: usize, mut right_line: usize, hunk: Diff, max_digit
}
}

pub fn print_diff(diff: Diff, extra_lines: usize) {
pub fn print_diff(diff: &[diff::Result<String>], extra_lines: usize) {
let mut diff = hunkify_diff(diff, extra_lines);

let last_hunk = diff.pop().expect("at least one hunk");
Expand Down
Loading
Loading