diff --git a/kakplugin/src/lib.rs b/kakplugin/src/lib.rs index 4d64fe7..a10df64 100644 --- a/kakplugin/src/lib.rs +++ b/kakplugin/src/lib.rs @@ -163,24 +163,34 @@ pub fn escape>(s: S) -> String { /// # Errors /// /// Will return `Err` if command fifo could not be opened or written to -pub fn cmd(cmd: &str) -> Result<(), KakError> { +pub fn cmd(cmd: S) -> Result<(), KakError> +where + S: AsRef, +{ let mut f = open_command_fifo()?; - write!(f, "{};", cmd)?; + write!(f, "{};", cmd.as_ref())?; f.flush().map_err(Into::into) } -pub fn restore_register(r: &Register) -> Result<(), KakError> { - cmd(&format!("execute-keys '\"{}z'", r.kak_escaped())) +pub fn restore_register(r: R) -> Result<(), KakError> +where + R: AsRef, +{ + cmd(&format!("execute-keys '\"{}z'", r.as_ref().kak_escaped())) } /// # Errors /// /// Will return `Err` if command fifo could not be opened or written to -pub fn response(msg: &str) -> Result, KakError> { +pub fn response(msg: S) -> Result, KakError> +where + S: AsRef, +{ cmd(&format!( - "echo -quoting shell -to-file {} -- {msg}", - get_var("kak_response_fifo")? + "echo -quoting shell -to-file {} -- {}", + get_var("kak_response_fifo")?, + msg.as_ref() ))?; let selections = shellwords::split(&fs::read_to_string(&get_var("kak_response_fifo")?)?)?; @@ -203,13 +213,16 @@ pub fn open_command_fifo() -> Result, KakError> { /// # Errors /// /// Will return `Err` if requested environment variable is not unicode or not present -pub fn get_var(var_name: &str) -> Result { - env::var(var_name).map_err(|e| match e { +pub fn get_var(var_name: S) -> Result +where + S: AsRef, +{ + env::var(var_name.as_ref()).map_err(|e| match e { env::VarError::NotPresent => { - KakError::EnvVarNotSet(format!("Env var {} is not defined", var_name)) + KakError::EnvVarNotSet(format!("Env var {} is not defined", var_name.as_ref())) } env::VarError::NotUnicode(_) => { - KakError::EnvVarUnicode(format!("Env var {} is not unicode", var_name)) + KakError::EnvVarUnicode(format!("Env var {} is not unicode", var_name.as_ref())) } }) } diff --git a/kakplugin/src/types.rs b/kakplugin/src/types.rs index c9479b9..3de3eb6 100644 --- a/kakplugin/src/types.rs +++ b/kakplugin/src/types.rs @@ -358,6 +358,12 @@ impl Register { } } +impl AsRef for Register { + fn as_ref(&self) -> &Self { + &self + } +} + impl FromStr for Register { type Err = KakError; fn from_str(s: &str) -> Result {