From df610c7e0314bc6ec90c85b53e75f14216f1cfee Mon Sep 17 00:00:00 2001 From: Austen Adler Date: Wed, 6 Jul 2022 21:29:02 -0400 Subject: [PATCH] Error if empty list is passed to set selection --- kakplugin/src/errors.rs | 7 +++++++ kakplugin/src/lib.rs | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/kakplugin/src/errors.rs b/kakplugin/src/errors.rs index 68d7e0b..accbf11 100644 --- a/kakplugin/src/errors.rs +++ b/kakplugin/src/errors.rs @@ -16,6 +16,8 @@ pub enum KakError { NotImplemented(&'static str), /// Custom error string Custom(String), + /// The selections/selections_desc list passed was empty + SetEmptySelections, } impl KakError { @@ -28,6 +30,9 @@ impl KakError { Self::Io(e) => format!("{e:?}"), Self::NotImplemented(e) => e.to_string(), Self::Custom(s) => s.clone(), + Self::SetEmptySelections => { + String::from("Attempted to set selections/selections_desc to empty list") + } } } } @@ -44,6 +49,8 @@ impl ToString for KakError { Self::Io(_) => "IO error", Self::NotImplemented(_) => "Not Implemented", Self::Custom(s) => s, + Self::SetEmptySelections => + "Attempted to set selections/selections_desc to empty list", } ) } diff --git a/kakplugin/src/lib.rs b/kakplugin/src/lib.rs index df8f37a..82f189f 100644 --- a/kakplugin/src/lib.rs +++ b/kakplugin/src/lib.rs @@ -96,9 +96,14 @@ where I: IntoIterator, S: AsRef, { + let mut selections_iter = selections.into_iter().peekable(); + if selections_iter.peek().is_none() { + return Err(KakError::SetEmptySelections); + } + let mut f = open_command_fifo()?; write!(f, "set-register '\"'")?; - for i in selections { + for i in selections_iter { write!(f, " '{}'", escape(i))?; } write!(f, "; execute-keys R;")?; @@ -113,9 +118,14 @@ pub fn set_selections_desc<'a, I>(selections: I) -> Result<(), KakError> where I: IntoIterator, { + let mut selections_iter = selections.into_iter().peekable(); + if selections_iter.peek().is_none() { + return Err(KakError::SetEmptySelections); + } + let mut f = open_command_fifo()?; write!(f, "select")?; - for i in selections { + for i in selections_iter { write!(f, " {}", i)?; } write!(f, ";")?;