Split out errors

This commit is contained in:
Austen Adler 2022-01-29 10:23:54 -05:00
parent aa1b27280e
commit 60d32a89a6
4 changed files with 71 additions and 20 deletions

11
Cargo.lock generated
View File

@ -194,6 +194,17 @@ dependencies = [
"alphanumeric-sort", "alphanumeric-sort",
"clap", "clap",
"regex", "regex",
"shellwords",
]
[[package]]
name = "shellwords"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "89e515aa4699a88148ed5ef96413ceef0048ce95b43fbc955a33bde0a70fcae6"
dependencies = [
"lazy_static",
"regex",
] ]
[[package]] [[package]]

View File

@ -10,8 +10,9 @@ keywords = ["cli", "kakoune"]
[dependencies] [dependencies]
regex = "1" regex = "1"
clap = {version = "3", features = ["derive"]} clap = {version = "3", features = ["derive", "env"]}
alphanumeric-sort = "1" alphanumeric-sort = "1"
shellwords = "1"
[profile.release] [profile.release]
lto = true lto = true

23
src/errors.rs Normal file
View File

@ -0,0 +1,23 @@
#[derive(Debug)]
pub struct KakMessage(pub String, pub Option<String>);
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)
}
}
impl From<shellwords::MismatchedQuotes> for KakMessage {
fn from(err: shellwords::MismatchedQuotes) -> Self {
Self("Corrupt kak response".to_string(), Some(err.to_string()))
}
}

View File

@ -20,7 +20,7 @@ struct Options {
// #[clap(env = "kak_response_fifo", takes_value = false)] // #[clap(env = "kak_response_fifo", takes_value = false)]
// kak_response_fifo_name: PathBuf, // kak_response_fifo_name: PathBuf,
#[clap(index = 1)] #[clap(index = 1)]
regex: String, regex: Option<String>,
#[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,
@ -31,28 +31,32 @@ struct Options {
} }
fn main() { fn main() {
match run() { let msg = match run() {
Ok(()) => send_message(&KakMessage("Replaced successfully".to_string(), None)), Ok(msg) => msg,
Err(msg) => send_message(&msg), Err(msg) => {
} eprintln!("{} (Debug info: {:?})", msg.0, msg.1);
msg
}
};
send_message(&msg);
} }
fn send_message(msg: &KakMessage) { fn send_message(msg: &KakMessage) {
// TODO: This isn't echoing anything
eprintln!("{} (Debug info: {:?})", msg.0, msg.1);
let msg_str = msg.0.replace('\'', "''"); let msg_str = msg.0.replace('\'', "''");
let mut f = open_command_fifo().unwrap(); {
let mut f = open_command_fifo().unwrap();
write!(f, "echo '{}';", msg_str).unwrap(); write!(f, "echo '{}';", msg_str).unwrap();
write!(f, "echo -debug '{}';", msg_str).unwrap(); write!(f, "echo -debug '{}';", msg_str).unwrap();
if let Some(debug_msg_str) = &msg.1 { if let Some(debug_msg_str) = &msg.1 {
write!(f, "echo -debug '{}';", debug_msg_str.replace('\'', "''")).unwrap(); write!(f, "echo -debug '{}';", debug_msg_str.replace('\'', "''")).unwrap();
}
} }
} }
fn run() -> Result<(), KakMessage> { fn run() -> Result<KakMessage, KakMessage> {
let options = Options::try_parse().map_err(|e| { let options = Options::try_parse().map_err(|e| {
KakMessage( KakMessage(
"Error parsing arguments".to_string(), "Error parsing arguments".to_string(),
@ -60,8 +64,17 @@ fn run() -> Result<(), KakMessage> {
) )
})?; })?;
let re = Regex::new(&options.regex) let re = options
.map_err(|_| format!("Invalid regular expression: {}", options.regex))?; .regex
.as_ref()
.map(|r| Regex::new(r))
.transpose()
.map_err(|_| {
format!(
"Invalid regular expression: {}",
options.regex.unwrap_or("".to_string())
)
})?;
let selections = read_selections()?; let selections = read_selections()?;
@ -78,7 +91,7 @@ fn run() -> Result<(), KakMessage> {
} }
}) })
.map(|a| { .map(|a| {
let captures = re.captures(a)?; let captures = re.as_ref()?.captures(a)?;
captures captures
.get(1) .get(1)
.or_else(|| captures.get(0)) .or_else(|| captures.get(0))
@ -112,9 +125,12 @@ fn run() -> Result<(), KakMessage> {
let new_selection = i.0.replace('\'', "''"); let new_selection = i.0.replace('\'', "''");
write!(f, " '{}'", new_selection)?; write!(f, " '{}'", new_selection)?;
} }
write!(f, " ;")?; write!(f, " ; exec R;")?;
Ok(()) Ok(KakMessage(
format!("Sorted {} selections", selections.len()),
None,
))
} }
fn read_selections() -> Result<Vec<String>, KakMessage> { fn read_selections() -> Result<Vec<String>, KakMessage> {