fix: clippy warnings

This commit is contained in:
Kristofers Solo 2025-01-15 16:10:17 +02:00
parent 36705f8c1a
commit 888bdd70ce
4 changed files with 50 additions and 5 deletions

20
.gitignore vendored
View File

@ -1 +1,21 @@
/target /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

View File

@ -7,3 +7,9 @@ edition = "2021"
clap = { version = "4.5", features = ["derive"] } clap = { version = "4.5", features = ["derive"] }
[dev-dependencies] [dev-dependencies]
[lints.clippy]
pedantic = "warn"
nursery = "warn"
unwrap_used = "warn"
expect_used = "warn"

View File

@ -1,11 +1,14 @@
use clap::Parser;
use std::{ use std::{
fs::{read_to_string, write}, fs::{read_to_string, write},
io::{self}, io::{self},
path::PathBuf, 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)] #[derive(Debug, Parser)]
#[command(author, version, about, long_about = None)] #[command(author, version, about, long_about = None)]
pub struct Cli { pub struct Cli {
@ -22,27 +25,43 @@ pub struct Cli {
pub line_by_line: bool, pub line_by_line: bool,
} }
/// Reverses the provided string
#[inline]
#[must_use]
pub fn encode(s: &str) -> String { pub fn encode(s: &str) -> String {
s.chars().rev().collect() 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 { pub fn encode_string(s: &str) -> String {
encode(s) 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<PathBuf>, line_by_line: bool) -> io::Result<String> { pub fn encode_file(path: impl Into<PathBuf>, line_by_line: bool) -> io::Result<String> {
let path = path.into(); let path = path.into();
let content = read_to_string(path)?; let content = read_to_string(path)?;
if line_by_line { if line_by_line {
return Ok(content return Ok(content
.lines() .lines()
.map(|line| encode(line)) .map(encode)
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join("\n")); .join("\n"));
} }
Ok(encode(&content)) 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<Option<String>> { pub fn process_input(cli: &Cli) -> io::Result<Option<String>> {
let result = if cli.file { let result = if cli.file {
encode_file(&cli.input, cli.line_by_line)? encode_file(&cli.input, cli.line_by_line)?

View File

@ -14,7 +14,7 @@ fn main() -> io::Result<()> {
Ok(()) Ok(())
} }
Err(e) => { Err(e) => {
eprintln!("Error: {}", e); eprintln!("Error: {e}");
Err(e) Err(e)
} }
} }