Improve error handling
This commit is contained in:
parent
af8724d5eb
commit
731c97117f
32
src/main.rs
32
src/main.rs
@ -3,12 +3,15 @@
|
|||||||
use alphanumeric_sort::compare_str;
|
use alphanumeric_sort::compare_str;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
type KakMessage = (String, Option<String>);
|
struct KakMessage(String, Option<String>);
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
#[clap(about, version, author)]
|
#[clap(about, version, author)]
|
||||||
struct Options {
|
struct Options {
|
||||||
|
#[clap(short, long)]
|
||||||
|
fifo_name: PathBuf,
|
||||||
#[clap(short = 'S', long)]
|
#[clap(short = 'S', long)]
|
||||||
// TODO: Can we invert a boolean? This name is terrible
|
// TODO: Can we invert a boolean? This name is terrible
|
||||||
no_skip_whitespace: bool,
|
no_skip_whitespace: bool,
|
||||||
@ -24,7 +27,7 @@ struct Options {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
match run() {
|
match run() {
|
||||||
Ok(()) => send_message(&("Replaced successfully".to_string(), None)),
|
Ok(()) => send_message(&KakMessage("Replaced successfully".to_string(), None)),
|
||||||
Err(msg) => send_message(&msg),
|
Err(msg) => send_message(&msg),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -42,7 +45,7 @@ fn send_message(msg: &KakMessage) {
|
|||||||
|
|
||||||
fn run() -> Result<(), KakMessage> {
|
fn run() -> Result<(), KakMessage> {
|
||||||
let options = Options::try_parse().map_err(|e| {
|
let options = Options::try_parse().map_err(|e| {
|
||||||
(
|
KakMessage(
|
||||||
"Error parsing arguments".to_string(),
|
"Error parsing arguments".to_string(),
|
||||||
Some(format!("Could not parse: {:?}", e)),
|
Some(format!("Could not parse: {:?}", e)),
|
||||||
)
|
)
|
||||||
@ -50,12 +53,8 @@ fn run() -> Result<(), KakMessage> {
|
|||||||
|
|
||||||
let replacement_re = options.regex;
|
let replacement_re = options.regex;
|
||||||
|
|
||||||
let re = Regex::new(&replacement_re).map_err(|_| {
|
let re = Regex::new(&replacement_re)
|
||||||
(
|
.map_err(|_| format!("Invalid regular expression: {}", replacement_re))?;
|
||||||
format!("Invalid regular expression: {}", replacement_re),
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
})?;
|
|
||||||
|
|
||||||
let mut zipped = options
|
let mut zipped = options
|
||||||
.selections
|
.selections
|
||||||
@ -107,3 +106,18 @@ fn run() -> Result<(), KakMessage> {
|
|||||||
print!(" ;");
|
print!(" ;");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<std::io::Error> for KakMessage {
|
||||||
|
fn from(err: std::io::Error) -> Self {
|
||||||
|
Self(
|
||||||
|
"Error writing to fifo".to_string(),
|
||||||
|
Some(format!("{:?}", err)),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<String> for KakMessage {
|
||||||
|
fn from(err: String) -> Self {
|
||||||
|
Self(err, None)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user