Skip to content

Commit

Permalink
refactor: Adjust for clippy (-Dwarnings)
Browse files Browse the repository at this point in the history
  • Loading branch information
attakei committed Apr 9, 2024
1 parent 4f13f52 commit 95ba4ff
Show file tree
Hide file tree
Showing 13 changed files with 59 additions and 61 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
2 changes: 1 addition & 1 deletion src/commands/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
6 changes: 3 additions & 3 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ 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 -%}
{{ f }}
{% 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}}\""
Expand Down
4 changes: 2 additions & 2 deletions src/commands/major.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current_version);
let context = make_context(&current_version, &new_version);
let new_version = up_major(current_version);
let context = make_context(current_version, &new_version);
workspace.update_files(&context)
}
4 changes: 2 additions & 2 deletions src/commands/minor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current_version);
let context = make_context(&current_version, &new_version);
let new_version = up_minor(current_version);
let context = make_context(current_version, &new_version);
workspace.update_files(&context)
}
4 changes: 2 additions & 2 deletions src/commands/patch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current_version);
let context = make_context(&current_version, &new_version);
let new_version = up_patch(current_version);
let context = make_context(current_version, &new_version);
workspace.update_files(&context)
}
2 changes: 1 addition & 1 deletion src/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(&current_version, &args.new_version);
let context = make_context(current_version, &args.new_version);
workspace.update_files(&context)
}
35 changes: 18 additions & 17 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Configuration manager.

use std::path::PathBuf;
use std::path::{Path, PathBuf};

use anyhow::{anyhow, Result};
use log::info;
Expand All @@ -25,13 +25,14 @@ pub struct FileConfig {
}

pub trait ParseAvailable {
fn new(root: &PathBuf) -> Result<Self>
fn new(root: &Path) -> Result<Self>
where
Self: Sized;
fn get_config(&self) -> Result<Config>;
fn update_version(&mut self, version: &Version) -> Result<()>;
}

#[allow(clippy::enum_variant_names)]
#[derive(Debug)]
pub enum ConfigDocument {
AgeToml(age_toml::Property),
Expand All @@ -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()))
};
Expand All @@ -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()))
};
Expand Down
6 changes: 3 additions & 3 deletions src/config/age_toml.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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;
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 {
Expand All @@ -17,7 +17,7 @@ pub struct Property {
}

impl ParseAvailable for Property {
fn new(root: &PathBuf) -> Result<Self> {
fn new(root: &Path) -> Result<Self> {
let filepath = root.join(FILENAME);
if !filepath.exists() {
return Err(anyhow!("Configuration file is not found."));
Expand Down
6 changes: 3 additions & 3 deletions src/config/cargo_toml.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -18,7 +18,7 @@ pub struct Property {
}

impl ParseAvailable for Property {
fn new(root: &PathBuf) -> Result<Self> {
fn new(root: &Path) -> Result<Self> {
let filepath = root.join(FILENAME);
if !filepath.exists() {
return Err(anyhow!("Configuration file is not found."));
Expand Down
6 changes: 3 additions & 3 deletions src/config/pyproject_toml.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand All @@ -18,7 +18,7 @@ pub struct Property {
}

impl ParseAvailable for Property {
fn new(root: &PathBuf) -> Result<Self> {
fn new(root: &Path) -> Result<Self> {
let filepath = root.join(FILENAME);
if !filepath.exists() {
return Err(anyhow!("Configuration file is not found."));
Expand Down
13 changes: 5 additions & 8 deletions src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,8 @@ pub struct Workspace {
impl Workspace {
pub fn try_new(root: PathBuf) -> Result<Self> {
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 })
}

Expand All @@ -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;
Expand Down Expand Up @@ -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
}
}

Expand Down
30 changes: 15 additions & 15 deletions src/writer.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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(),
);
Expand All @@ -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<()> {
Expand All @@ -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<String> = VecDeque::new();
let mut output: Vec<String> = Vec::new();
for line in target.split("\n") {
for line in target.split('\n') {
if buf.len() == lines {
output.push(buf.pop_front().unwrap());
}
Expand All @@ -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")
}
}

Expand Down

0 comments on commit 95ba4ff

Please sign in to comment.