From 95ba4ff418e9e6ff4b277c86ada9d43c34769936 Mon Sep 17 00:00:00 2001 From: Kazuya Takei Date: Wed, 10 Apr 2024 01:01:06 +0900 Subject: [PATCH] refactor: Adjust for clippy (-Dwarnings) --- .github/workflows/main.yml | 2 +- src/commands/info.rs | 2 +- src/commands/init.rs | 6 +++--- src/commands/major.rs | 4 ++-- src/commands/minor.rs | 4 ++-- src/commands/patch.rs | 4 ++-- src/commands/update.rs | 2 +- src/config.rs | 35 ++++++++++++++++++----------------- src/config/age_toml.rs | 6 +++--- src/config/cargo_toml.rs | 6 +++--- src/config/pyproject_toml.rs | 6 +++--- src/workspace.rs | 13 +++++-------- src/writer.rs | 30 +++++++++++++++--------------- 13 files changed, 59 insertions(+), 61 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8cef99b..f750241 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -20,7 +20,7 @@ jobs: - name: 'Run formatter' run: cargo +nightly fmt --all --check - name: 'Run linter' - run: cargo clippy --workspace --all-targets --all-features --locked + run: cargo clippy --workspace --all-targets --all-features --locked -- -Dwarnings test: needs: ['lint'] runs-on: 'ubuntu-latest' diff --git a/src/commands/info.rs b/src/commands/info.rs index 7302621..370eb94 100644 --- a/src/commands/info.rs +++ b/src/commands/info.rs @@ -37,7 +37,7 @@ pub(crate) fn execute(_args: &Arguments, workspace: &Workspace) -> Result<()> { Ok(()) } -const DISPLAY_TEMPLATE: &'static str = r#" +const DISPLAY_TEMPLATE: &str = r#" # Workspace - Root: {{ workspace_root|safe }} diff --git a/src/commands/init.rs b/src/commands/init.rs index 2bb5886..3dd6e38 100644 --- a/src/commands/init.rs +++ b/src/commands/init.rs @@ -9,7 +9,7 @@ use tera::{Context, Tera}; use crate::config; use crate::config::age_toml; -const TEMPLATE_BASE: &'static str = r#" +const TEMPLATE_BASE: &str = r#" current_version = "{{ current_version }}" {% for f in files -%} @@ -17,14 +17,14 @@ current_version = "{{ current_version }}" {% endfor -%} "#; -const TEMPLATE_FOR_RUST: &'static str = r#" +const TEMPLATE_FOR_RUST: &str = r#" [[files]] path = "Cargo.toml" search = "version = \"{{current_version}}\"" replace = "version = \"{{new_version}}\"" "#; -const TEMPLATE_FOR_PYTHON: &'static str = r#" +const TEMPLATE_FOR_PYTHON: &str = r#" [[files]] path = "pyproject.toml" search = "version = \"{{current_version}}\"" diff --git a/src/commands/major.rs b/src/commands/major.rs index 07cc4b8..c4170ed 100644 --- a/src/commands/major.rs +++ b/src/commands/major.rs @@ -9,7 +9,7 @@ pub(crate) struct Arguments {} pub(crate) fn execute(_args: &Arguments, workspace: &mut Workspace) -> Result<()> { let current_version = &workspace.config.current_version; - let new_version = up_major(¤t_version); - let context = make_context(¤t_version, &new_version); + let new_version = up_major(current_version); + let context = make_context(current_version, &new_version); workspace.update_files(&context) } diff --git a/src/commands/minor.rs b/src/commands/minor.rs index ad4f836..a8aedf6 100644 --- a/src/commands/minor.rs +++ b/src/commands/minor.rs @@ -9,7 +9,7 @@ pub(crate) struct Arguments {} pub(crate) fn execute(_args: &Arguments, workspace: &mut Workspace) -> Result<()> { let current_version = &workspace.config.current_version; - let new_version = up_minor(¤t_version); - let context = make_context(¤t_version, &new_version); + let new_version = up_minor(current_version); + let context = make_context(current_version, &new_version); workspace.update_files(&context) } diff --git a/src/commands/patch.rs b/src/commands/patch.rs index a381320..a1f97d1 100644 --- a/src/commands/patch.rs +++ b/src/commands/patch.rs @@ -9,7 +9,7 @@ pub(crate) struct Arguments {} pub(crate) fn execute(_args: &Arguments, workspace: &mut Workspace) -> Result<()> { let current_version = &workspace.config.current_version; - let new_version = up_patch(¤t_version); - let context = make_context(¤t_version, &new_version); + let new_version = up_patch(current_version); + let context = make_context(current_version, &new_version); workspace.update_files(&context) } diff --git a/src/commands/update.rs b/src/commands/update.rs index 329f1d6..b6bd17a 100644 --- a/src/commands/update.rs +++ b/src/commands/update.rs @@ -11,6 +11,6 @@ pub(crate) struct Arguments { pub(crate) fn execute(args: &Arguments, workspace: &mut Workspace) -> Result<()> { let current_version = &workspace.config.current_version; - let context = make_context(¤t_version, &args.new_version); + let context = make_context(current_version, &args.new_version); workspace.update_files(&context) } diff --git a/src/config.rs b/src/config.rs index 6abfc06..cf538b1 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,6 @@ // Configuration manager. -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use anyhow::{anyhow, Result}; use log::info; @@ -25,13 +25,14 @@ pub struct FileConfig { } pub trait ParseAvailable { - fn new(root: &PathBuf) -> Result + fn new(root: &Path) -> Result where Self: Sized; fn get_config(&self) -> Result; fn update_version(&mut self, version: &Version) -> Result<()>; } +#[allow(clippy::enum_variant_names)] #[derive(Debug)] pub enum ConfigDocument { AgeToml(age_toml::Property), @@ -56,32 +57,32 @@ impl ConfigDocument { } } -pub fn resolve_config(root: &PathBuf) -> Result<(ConfigDocument, Config)> { +pub fn resolve_config(root: &Path) -> Result<(ConfigDocument, Config)> { let _age_toml = '_age_toml: { let doc = age_toml::Property::new(root); - if doc.is_err() { - break '_age_toml Err(doc.unwrap_err()); + if let Err(err) = doc { + break '_age_toml Err(err); } let doc = doc.unwrap(); let config = doc.get_config(); - if config.is_err() { - break '_age_toml Err(config.unwrap_err()); + if let Err(err) = config { + break '_age_toml Err(err); } Ok((ConfigDocument::AgeToml(doc), config.unwrap())) }; - if _age_toml.is_ok() { + if let Ok(result) = _age_toml { info!("Found valid .age.toml"); - return _age_toml; + return Ok(result); } let _cargo_toml = '_cargo_toml: { let doc = cargo_toml::Property::new(root); - if doc.is_err() { - break '_cargo_toml Err(doc.unwrap_err()); + if let Err(err) = doc { + break '_cargo_toml Err(err); } let doc = doc.unwrap(); let config = doc.get_config(); - if config.is_err() { - break '_cargo_toml Err(config.unwrap_err()); + if let Err(err) = config { + break '_cargo_toml Err(err); } Ok((ConfigDocument::CargoToml(doc), config.unwrap())) }; @@ -91,13 +92,13 @@ pub fn resolve_config(root: &PathBuf) -> Result<(ConfigDocument, Config)> { } let _pyproject_toml = '_pyproject_toml: { let doc = pyproject_toml::Property::new(root); - if doc.is_err() { - break '_pyproject_toml Err(doc.unwrap_err()); + if let Err(err) = doc { + break '_pyproject_toml Err(err); } let doc = doc.unwrap(); let config = doc.get_config(); - if config.is_err() { - break '_pyproject_toml Err(config.unwrap_err()); + if let Err(err) = config { + break '_pyproject_toml Err(err); } Ok((ConfigDocument::PyprojectToml(doc), config.unwrap())) }; diff --git a/src/config/age_toml.rs b/src/config/age_toml.rs index e0ef9a3..e5dc7e9 100644 --- a/src/config/age_toml.rs +++ b/src/config/age_toml.rs @@ -1,6 +1,6 @@ use std::fs::{read_to_string, File}; use std::io::prelude::*; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use anyhow::{anyhow, Result}; use toml_edit::de::from_document; @@ -8,7 +8,7 @@ use toml_edit::{value, DocumentMut}; use super::{Config, ParseAvailable}; -pub const FILENAME: &'static str = ".age.toml"; +pub const FILENAME: &str = ".age.toml"; #[derive(Debug)] pub struct Property { @@ -17,7 +17,7 @@ pub struct Property { } impl ParseAvailable for Property { - fn new(root: &PathBuf) -> Result { + fn new(root: &Path) -> Result { let filepath = root.join(FILENAME); if !filepath.exists() { return Err(anyhow!("Configuration file is not found.")); diff --git a/src/config/cargo_toml.rs b/src/config/cargo_toml.rs index 49504de..a2236ab 100644 --- a/src/config/cargo_toml.rs +++ b/src/config/cargo_toml.rs @@ -1,6 +1,6 @@ use std::fs::{read_to_string, File}; use std::io::prelude::*; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use anyhow::{anyhow, Result}; use toml::de::Error; @@ -9,7 +9,7 @@ use toml_edit::{value, DocumentMut}; use super::{Config, ParseAvailable}; -pub const FILENAME: &'static str = "Cargo.toml"; +pub const FILENAME: &str = "Cargo.toml"; #[derive(Debug)] pub struct Property { @@ -18,7 +18,7 @@ pub struct Property { } impl ParseAvailable for Property { - fn new(root: &PathBuf) -> Result { + fn new(root: &Path) -> Result { let filepath = root.join(FILENAME); if !filepath.exists() { return Err(anyhow!("Configuration file is not found.")); diff --git a/src/config/pyproject_toml.rs b/src/config/pyproject_toml.rs index 8832dd7..78227bf 100644 --- a/src/config/pyproject_toml.rs +++ b/src/config/pyproject_toml.rs @@ -1,6 +1,6 @@ use std::fs::{read_to_string, File}; use std::io::prelude::*; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use anyhow::{anyhow, Result}; use toml::de::Error; @@ -9,7 +9,7 @@ use toml_edit::{value, DocumentMut}; use super::{Config, ParseAvailable}; -pub const FILENAME: &'static str = "pyproject.toml"; +pub const FILENAME: &str = "pyproject.toml"; #[derive(Debug)] pub struct Property { @@ -18,7 +18,7 @@ pub struct Property { } impl ParseAvailable for Property { - fn new(root: &PathBuf) -> Result { + fn new(root: &Path) -> Result { let filepath = root.join(FILENAME); if !filepath.exists() { return Err(anyhow!("Configuration file is not found.")); diff --git a/src/workspace.rs b/src/workspace.rs index 94c61d8..99304d0 100644 --- a/src/workspace.rs +++ b/src/workspace.rs @@ -23,11 +23,8 @@ pub struct Workspace { impl Workspace { pub fn try_new(root: PathBuf) -> Result { debug!("Trying init workspace on : {}", root.display().to_string()); - let resolved = resolve_config(&root); - if resolved.is_err() { - return Err(resolved.unwrap_err()); - } - let (doc, config) = resolved.unwrap(); + let resolved = resolve_config(&root)?; + let (doc, config) = resolved; Ok(Self { root, doc, config }) } @@ -38,8 +35,8 @@ impl Workspace { let mut pwd = root.clone(); loop { let ws = Self::try_new(pwd.clone()); - if ws.is_ok() { - return Ok(ws.unwrap()); + if let Ok(ws) = ws { + return Ok(ws); } if pwd.join(".git").exists() { break; @@ -104,7 +101,7 @@ impl Context { ctx.insert("current_version", &self.current_version); ctx.insert("new_version", &self.new_version); ctx.insert("now", &self.now.to_rfc3339()); - return ctx; + ctx } } diff --git a/src/writer.rs b/src/writer.rs index 32ee69a..4a250d2 100644 --- a/src/writer.rs +++ b/src/writer.rs @@ -1,7 +1,7 @@ use std::collections::{HashMap, VecDeque}; use std::fs::{read_to_string, File}; use std::io::prelude::*; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use anyhow::Result; use tera::{Context, Tera}; @@ -38,24 +38,24 @@ pub struct WriteRule { impl Writer { pub fn new(ctx: &Context) -> Self { - return Self { + Self { context: ctx.clone(), targets: HashMap::new(), - }; + } } - pub fn add_target(&mut self, path: &PathBuf, search: &String, replace: &String) { + pub fn add_target(&mut self, path: &Path, search: &str, replace: &str) { let path_key = path.display().to_string(); if !self.targets.contains_key(&path_key) { self.targets - .insert(path_key.clone(), WriteTarget::new(&path)); + .insert(path_key.clone(), WriteTarget::new(path)); } let target = self.targets.get_mut(&path_key).unwrap(); target.add_rule( - Tera::one_off(&search, &self.context, true) + Tera::one_off(search, &self.context, true) .unwrap() .to_string(), - Tera::one_off(&replace, &self.context, true) + Tera::one_off(replace, &self.context, true) .unwrap() .to_string(), ); @@ -70,11 +70,11 @@ impl Writer { } impl WriteTarget { - pub fn new(path: &PathBuf) -> Self { - return Self { - path: path.clone(), + pub fn new(path: &Path) -> Self { + Self { + path: path.to_path_buf(), rules: Vec::new(), - }; + } } pub fn update(&self) -> Result<()> { @@ -94,10 +94,10 @@ impl WriteTarget { impl WriteRule { fn update(&self, target: String) -> String { - let lines = self.search.split("\n").count(); + let lines = self.search.split('\n').count(); let mut buf: VecDeque = VecDeque::new(); let mut output: Vec = Vec::new(); - for line in target.split("\n") { + for line in target.split('\n') { if buf.len() == lines { output.push(buf.pop_front().unwrap()); } @@ -108,12 +108,12 @@ impl WriteRule { buf.clear(); } } - if buf.len() > 0 { + if !buf.is_empty() { for line in buf { output.push(line.to_string()); } } - return output.join("\n"); + output.join("\n") } }