diff options
| author | Ellie Huxtable <ellie@elliehuxtable.com> | 2024-04-23 14:45:07 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-04-23 14:45:07 +0100 |
| commit | bf88b42cec26ee2912c5e25eaadd5764b3a480f0 (patch) | |
| tree | 3ddd529084a13b2c2b0ef1f1941797be4d176a63 /crates/atuin-common/src | |
| parent | chore(deps): Fix ratatui update (#1975) (diff) | |
| download | atuin-bf88b42cec26ee2912c5e25eaadd5764b3a480f0.zip | |
fix(dotfiles): unquote aliases before quoting (#1976)
* fix(dotfiles): unquote aliases before quoting
* tests
Diffstat (limited to 'crates/atuin-common/src')
| -rw-r--r-- | crates/atuin-common/src/utils.rs | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/crates/atuin-common/src/utils.rs b/crates/atuin-common/src/utils.rs index 7c533663..3815085e 100644 --- a/crates/atuin-common/src/utils.rs +++ b/crates/atuin-common/src/utils.rs @@ -2,6 +2,8 @@ use std::borrow::Cow; use std::env; use std::path::PathBuf; +use eyre::{eyre, Result}; + use rand::RngCore; use uuid::Uuid; @@ -144,6 +146,30 @@ pub trait Escapable: AsRef<str> { } } +pub fn unquote(s: &str) -> Result<String> { + if s.chars().count() < 2 { + return Err(eyre!("not enough chars")); + } + + let quote = s.chars().next().unwrap(); + + // not quoted, do nothing + if quote != '"' && quote != '\'' && quote != '`' { + return Ok(s.to_string()); + } + + if s.chars().last().unwrap() != quote { + return Err(eyre!("unexpected eof, quotes do not match")); + } + + // removes quote characters + // the sanity checks performed above ensure that the quotes will be ASCII and this will not + // panic + let s = &s[1..s.len() - 1]; + + Ok(s.to_string()) +} + impl<T: AsRef<str>> Escapable for T {} #[cfg(test)] |
