fix: clippy warnings

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

View File

@@ -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<PathBuf>, line_by_line: bool) -> io::Result<String> {
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::<Vec<String>>()
.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<Option<String>> {
let result = if cli.file {
encode_file(&cli.input, cli.line_by_line)?

View File

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