From b677713a7caf179b144674ff4c6e6b7171ad1337 Mon Sep 17 00:00:00 2001 From: Jonas Maier <> Date: Fri, 17 Apr 2026 17:56:58 +0200 Subject: aliases --- src/parse/mod.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'src/parse/mod.rs') diff --git a/src/parse/mod.rs b/src/parse/mod.rs index 8682f04..184a514 100644 --- a/src/parse/mod.rs +++ b/src/parse/mod.rs @@ -1,4 +1,4 @@ -use crate::BString; +use crate::{BString, bstr}; #[cfg(test)] mod test; @@ -30,6 +30,17 @@ pub trait Expander { type Error; fn expand_var(&mut self, v: BString, default: Option) -> Res; fn expand_cmd(&mut self, c: Ast) -> Res; + + type AliasAge; + fn expand_alias( + &mut self, + cmd: &bstr, + age: Option, + ) -> Res)>, Self::Error> { + let _ = cmd; + let _ = age; + Ok(None) + } } #[derive(Debug, Clone, PartialEq)] @@ -857,7 +868,30 @@ pub struct Command { } impl Command { - fn expand(self, e: &mut E) -> Res, E::Error> { + fn full_alias_expansion(&mut self, e: &mut E) -> Res<(), E::Error> { + self.args.reverse(); + let mut age = None; + + while self.cmd.parts.len() == 1 + && let StringPart::Boring(s) = &self.cmd.parts[0] + { + if let Some((new_age, exp)) = e.expand_alias(&s, age.take())? { + age = Some(new_age); + self.cmd = exp.first().unwrap().clone(); + for e in exp.into_iter().skip(1).rev() { + self.args.push(e); + } + } else { + break; + } + } + self.args.reverse(); + + Ok(()) + } + + fn expand(mut self, e: &mut E) -> Res, E::Error> { + self.full_alias_expansion(e)?; let cmd = self.cmd.expand(e)?; let mut args = Vec::with_capacity(self.args.len()); for arg in self.args.into_iter() { @@ -999,8 +1033,11 @@ pub fn completion_context(x: &[u8], e: &mut E) -> CompletionContext ast.completion(e) } -trait Parse: Sized { +pub trait Parse: Sized { fn parse(b: &mut Cursor<'_>) -> Result; + fn parse_from_bytes(x: &[u8]) -> Result { + Self::parse(&mut Cursor::new(x, ParseMode::Command)) + } } enum ParseMode { @@ -1008,7 +1045,7 @@ enum ParseMode { Completion, } -struct Cursor<'a> { +pub struct Cursor<'a> { buf: &'a [u8], mode: ParseMode, -- cgit v1.2.3