Add support for accepting AsRef<str>

This commit is contained in:
Austen Adler 2022-08-10 20:21:58 -04:00
parent a2a09715de
commit 3e84e06fb9
4 changed files with 14 additions and 8 deletions

View File

@ -23,8 +23,11 @@ pub fn get_selections(keys: Option<&'_ str>) -> Result<Vec<Selection>, KakError>
/// ///
/// Will return `Err` if command fifo could not be opened, read from, or written to /// Will return `Err` if command fifo could not be opened, read from, or written to
// TODO: Use AsRef // TODO: Use AsRef
pub fn get_selections_desc(keys: Option<&'_ str>) -> Result<Vec<SelectionDesc>, KakError> { pub fn get_selections_desc<S>(keys: Option<S>) -> Result<Vec<SelectionDesc>, KakError>
response("%val{selections_desc}", keys)? where
S: AsRef<str>,
{
response("%val{selections_desc}", keys.as_ref())?
.iter() .iter()
.map(|sd| SelectionDesc::from_str(sd)) .map(|sd| SelectionDesc::from_str(sd))
.collect::<Result<Vec<_>, KakError>>() .collect::<Result<Vec<_>, KakError>>()
@ -184,9 +187,10 @@ where
/// # 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<S1>(msg: S1, keys: Option<&'_ str>) -> Result<Vec<String>, KakError> pub fn response<S1, S2>(msg: S1, keys: Option<S2>) -> Result<Vec<String>, KakError>
where where
S1: AsRef<str>, S1: AsRef<str>,
S2: AsRef<str>,
{ {
let response_fifo = get_var("kak_response_fifo")?; let response_fifo = get_var("kak_response_fifo")?;

View File

@ -12,8 +12,10 @@ pub struct Options {
pub fn box_(options: &Options) -> Result<String, KakError> { pub fn box_(options: &Options) -> Result<String, KakError> {
if options.bounding_box { if options.bounding_box {
// The user requested only the bounding box, so select it first // The user requested only the bounding box, so select it first
set_selections_desc(vec![get_bounding_box(get_selections_desc(None)?) set_selections_desc(vec![get_bounding_box(get_selections_desc::<&str>(None)?)
.ok_or_else(|| KakError::Custom(String::from("Selection is empty")))?])?; .ok_or_else(|| {
KakError::Custom(String::from("Selection is empty"))
})?])?;
} }
let ret_selections_desc = boxed_selections(options)?; let ret_selections_desc = boxed_selections(options)?;
@ -62,7 +64,7 @@ where
fn boxed_selections(_options: &Options) -> Result<Vec<SelectionDesc>, KakError> { fn boxed_selections(_options: &Options) -> Result<Vec<SelectionDesc>, KakError> {
// The selections we want to box, one per box // The selections we want to box, one per box
let selections_desc = { let selections_desc = {
let mut ret = get_selections_desc(None)?; let mut ret = get_selections_desc::<&str>(None)?;
ret.sort(); ret.sort();
ret ret
}; };

View File

@ -48,7 +48,7 @@ enum Commands {
Shuf(shuf::Options), Shuf(shuf::Options),
#[clap(about = "Find unique selections based on optional regex match")] #[clap(about = "Find unique selections based on optional regex match")]
Uniq(uniq::Options), Uniq(uniq::Options),
#[clap(about = "Invert all selections"), visible_aliases = &["inverse"]] #[clap(about = "Invert all selections", visible_aliases = &["inverse"])]
Invert(invert::Options), Invert(invert::Options),
#[clap(about = "Evaluate selections as a math expression", visible_aliases = &["bc", "eval"])] #[clap(about = "Evaluate selections as a math expression", visible_aliases = &["bc", "eval"])]
MathEval(math_eval::Options), MathEval(math_eval::Options),

View File

@ -51,7 +51,7 @@ pub fn uniq(options: &Options) -> Result<String, KakError> {
}))?; }))?;
// Deselect all `None` strings (all rows that have been seen before) // Deselect all `None` strings (all rows that have been seen before)
let mut new_selections_desc = get_selections_desc(None)?; let mut new_selections_desc = get_selections_desc::<&str>(None)?;
new_selections_desc.sort(); new_selections_desc.sort();
set_selections_desc( set_selections_desc(
// Refresh seelections_desc because positions have changed // Refresh seelections_desc because positions have changed