Split out errors
This commit is contained in:
parent
aa1b27280e
commit
60d32a89a6
11
Cargo.lock
generated
11
Cargo.lock
generated
@ -194,6 +194,17 @@ dependencies = [
|
||||
"alphanumeric-sort",
|
||||
"clap",
|
||||
"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]]
|
||||
|
@ -10,8 +10,9 @@ keywords = ["cli", "kakoune"]
|
||||
|
||||
[dependencies]
|
||||
regex = "1"
|
||||
clap = {version = "3", features = ["derive"]}
|
||||
clap = {version = "3", features = ["derive", "env"]}
|
||||
alphanumeric-sort = "1"
|
||||
shellwords = "1"
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
|
23
src/errors.rs
Normal file
23
src/errors.rs
Normal 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()))
|
||||
}
|
||||
}
|
42
src/main.rs
42
src/main.rs
@ -20,7 +20,7 @@ struct Options {
|
||||
// #[clap(env = "kak_response_fifo", takes_value = false)]
|
||||
// kak_response_fifo_name: PathBuf,
|
||||
#[clap(index = 1)]
|
||||
regex: String,
|
||||
regex: Option<String>,
|
||||
#[clap(short = 'S', long)]
|
||||
// TODO: Can we invert a boolean? This name is terrible
|
||||
no_skip_whitespace: bool,
|
||||
@ -31,17 +31,20 @@ struct Options {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
match run() {
|
||||
Ok(()) => send_message(&KakMessage("Replaced successfully".to_string(), None)),
|
||||
Err(msg) => send_message(&msg),
|
||||
let msg = match run() {
|
||||
Ok(msg) => msg,
|
||||
Err(msg) => {
|
||||
eprintln!("{} (Debug info: {:?})", msg.0, msg.1);
|
||||
msg
|
||||
}
|
||||
};
|
||||
|
||||
send_message(&msg);
|
||||
}
|
||||
|
||||
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 mut f = open_command_fifo().unwrap();
|
||||
|
||||
write!(f, "echo '{}';", msg_str).unwrap();
|
||||
@ -51,8 +54,9 @@ fn send_message(msg: &KakMessage) {
|
||||
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| {
|
||||
KakMessage(
|
||||
"Error parsing arguments".to_string(),
|
||||
@ -60,8 +64,17 @@ fn run() -> Result<(), KakMessage> {
|
||||
)
|
||||
})?;
|
||||
|
||||
let re = Regex::new(&options.regex)
|
||||
.map_err(|_| format!("Invalid regular expression: {}", options.regex))?;
|
||||
let re = options
|
||||
.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()?;
|
||||
|
||||
@ -78,7 +91,7 @@ fn run() -> Result<(), KakMessage> {
|
||||
}
|
||||
})
|
||||
.map(|a| {
|
||||
let captures = re.captures(a)?;
|
||||
let captures = re.as_ref()?.captures(a)?;
|
||||
captures
|
||||
.get(1)
|
||||
.or_else(|| captures.get(0))
|
||||
@ -112,9 +125,12 @@ fn run() -> Result<(), KakMessage> {
|
||||
let new_selection = i.0.replace('\'', "''");
|
||||
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> {
|
||||
|
Loading…
Reference in New Issue
Block a user