From 888bdd70ce9836b5c922bd52e1cdb949adc02d62 Mon Sep 17 00:00:00 2001 From: Kristofers Solo Date: Wed, 15 Jan 2025 16:10:17 +0200 Subject: [PATCH] fix: clippy warnings --- .gitignore | 20 ++++++++++++++++++++ Cargo.toml | 6 ++++++ src/lib.rs | 27 +++++++++++++++++++++++---- src/main.rs | 2 +- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index ea8c4bf..4e98773 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,21 @@ /target +#--------------------------------------------------# +# The following was generated with gitignore.nvim: # +#--------------------------------------------------# +# Gitignore for the following technologies: Rust + +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + diff --git a/Cargo.toml b/Cargo.toml index 6011763..0b6deeb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,9 @@ edition = "2021" clap = { version = "4.5", features = ["derive"] } [dev-dependencies] + +[lints.clippy] +pedantic = "warn" +nursery = "warn" +unwrap_used = "warn" +expect_used = "warn" diff --git a/src/lib.rs b/src/lib.rs index a76f622..d823428 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,11 +1,14 @@ +use clap::Parser; use std::{ fs::{read_to_string, write}, io::{self}, path::PathBuf, }; -use clap::Parser; - +/// A CLI tool for reversing text content +/// +/// This tool provides functionality to reverse text, either from direct input +/// or from files. It supports both whole-content reversal and line-by-line reversal. #[derive(Debug, Parser)] #[command(author, version, about, long_about = None)] pub struct Cli { @@ -22,27 +25,43 @@ pub struct Cli { pub line_by_line: bool, } +/// Reverses the provided string +#[inline] +#[must_use] pub fn encode(s: &str) -> String { s.chars().rev().collect() } - +/// Reverses a string directly +/// +/// This is a convenience wrapper around the `encode` function. +#[inline] +#[must_use] pub fn encode_string(s: &str) -> String { encode(s) } +/// Reverses the contents of a file +/// +/// # Errors +/// - The file cannot be read +/// - The file content is not valid UTF-8 pub fn encode_file(path: impl Into, line_by_line: bool) -> io::Result { let path = path.into(); let content = read_to_string(path)?; if line_by_line { return Ok(content .lines() - .map(|line| encode(line)) + .map(encode) .collect::>() .join("\n")); } Ok(encode(&content)) } +/// Processes the input based on CLI arguments +/// +/// # Errors +/// - There was an error reading or writing files pub fn process_input(cli: &Cli) -> io::Result> { let result = if cli.file { encode_file(&cli.input, cli.line_by_line)? diff --git a/src/main.rs b/src/main.rs index e30ff99..a6c0bd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,7 +14,7 @@ fn main() -> io::Result<()> { Ok(()) } Err(e) => { - eprintln!("Error: {}", e); + eprintln!("Error: {e}"); Err(e) } }