Use AsRef for passing Selections and Registers

This commit is contained in:
Austen Adler 2022-07-07 00:28:07 -04:00
parent ea32174522
commit 9fd54770ab
2 changed files with 30 additions and 11 deletions

View File

@ -163,24 +163,34 @@ pub fn escape<S: AsRef<str>>(s: S) -> String {
/// # Errors /// # Errors
/// ///
/// Will return `Err` if command fifo could not be opened or written to /// Will return `Err` if command fifo could not be opened or written to
pub fn cmd(cmd: &str) -> Result<(), KakError> { pub fn cmd<S>(cmd: S) -> Result<(), KakError>
where
S: AsRef<str>,
{
let mut f = open_command_fifo()?; let mut f = open_command_fifo()?;
write!(f, "{};", cmd)?; write!(f, "{};", cmd.as_ref())?;
f.flush().map_err(Into::into) f.flush().map_err(Into::into)
} }
pub fn restore_register(r: &Register) -> Result<(), KakError> { pub fn restore_register<R>(r: R) -> Result<(), KakError>
cmd(&format!("execute-keys '\"{}z'", r.kak_escaped())) where
R: AsRef<Register>,
{
cmd(&format!("execute-keys '\"{}z'", r.as_ref().kak_escaped()))
} }
/// # Errors /// # Errors
/// ///
/// Will return `Err` if command fifo could not be opened or written to /// Will return `Err` if command fifo could not be opened or written to
pub fn response(msg: &str) -> Result<Vec<String>, KakError> { pub fn response<S>(msg: S) -> Result<Vec<String>, KakError>
where
S: AsRef<str>,
{
cmd(&format!( cmd(&format!(
"echo -quoting shell -to-file {} -- {msg}", "echo -quoting shell -to-file {} -- {}",
get_var("kak_response_fifo")? get_var("kak_response_fifo")?,
msg.as_ref()
))?; ))?;
let selections = shellwords::split(&fs::read_to_string(&get_var("kak_response_fifo")?)?)?; let selections = shellwords::split(&fs::read_to_string(&get_var("kak_response_fifo")?)?)?;
@ -203,13 +213,16 @@ pub fn open_command_fifo() -> Result<BufWriter<File>, KakError> {
/// # Errors /// # Errors
/// ///
/// Will return `Err` if requested environment variable is not unicode or not present /// Will return `Err` if requested environment variable is not unicode or not present
pub fn get_var(var_name: &str) -> Result<String, KakError> { pub fn get_var<S>(var_name: S) -> Result<String, KakError>
env::var(var_name).map_err(|e| match e { where
S: AsRef<str>,
{
env::var(var_name.as_ref()).map_err(|e| match e {
env::VarError::NotPresent => { 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(_) => { 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()))
} }
}) })
} }

View File

@ -358,6 +358,12 @@ impl Register {
} }
} }
impl AsRef<Register> for Register {
fn as_ref(&self) -> &Self {
&self
}
}
impl FromStr for Register { impl FromStr for Register {
type Err = KakError; type Err = KakError;
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {