sula_alus/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use clap::Parser;
use std::{
    fs::{read_to_string, write},
    io::{self},
    path::PathBuf,
};

/// 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 {
    /// The input text or file path to hash
    pub input: String,
    /// Specify if the input is a file
    #[arg(short, long)]
    pub file: bool,
    /// Output file path (optional)
    #[arg(short, long)]
    pub output: Option<PathBuf>,
    /// Reverse by line instead of the whole content
    #[arg(short, long)]
    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(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)?
    } else {
        encode_string(&cli.input)
    };

    if let Some(output_path) = &cli.output {
        write(output_path, result)?;
        return Ok(None);
    }
    Ok(Some(result))
}