feat: implement proper cli exit codes
This commit is contained in:
parent
3a7df31b08
commit
f8181ee583
3 changed files with 29 additions and 8 deletions
29
src/main.rs
29
src/main.rs
|
|
@ -1,4 +1,8 @@
|
|||
use std::process::ExitCode;
|
||||
|
||||
use clap::Parser;
|
||||
|
||||
use crate::matches::Matches;
|
||||
mod matches;
|
||||
mod parser;
|
||||
|
||||
|
|
@ -8,16 +12,25 @@ struct Cli {
|
|||
file_path: String,
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
fn main() -> ExitCode {
|
||||
match run() {
|
||||
Ok(matches) if matches.is_empty() == true => ExitCode::FAILURE,
|
||||
Ok(matches) => {
|
||||
println!("{}", matches);
|
||||
ExitCode::SUCCESS
|
||||
}
|
||||
Err(error) => {
|
||||
eprintln!("{}", error);
|
||||
ExitCode::from(2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn run() -> anyhow::Result<Matches> {
|
||||
let cli = Cli::parse();
|
||||
|
||||
let lines_iter = parser::read_lines(&cli.file_path)?;
|
||||
let matches = parser::match_pattern(lines_iter, &cli.pattern, &cli.file_path);
|
||||
let matches = parser::match_pattern(lines_iter, &cli.pattern, &cli.file_path)?;
|
||||
|
||||
println!("Pattern: {}", cli.pattern);
|
||||
println!("File path: {}", cli.file_path);
|
||||
println!("---");
|
||||
println!("{}", matches?);
|
||||
|
||||
Ok(())
|
||||
Ok(matches)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,6 +11,12 @@ pub struct Matches {
|
|||
pub pattern: String,
|
||||
}
|
||||
|
||||
impl Matches {
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.items.is_empty()
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Match {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}: line {}: {}", self.file_name, self.line_number, self.text)
|
||||
|
|
|
|||
2
test.txt
2
test.txt
|
|
@ -1,2 +1,4 @@
|
|||
hola
|
||||
no
|
||||
hola again
|
||||
yep it's me
|
||||
|
|
|
|||
Loading…
Reference in a new issue