diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-08-17 06:57:09 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-08-17 06:57:09 +0800 |
| commit | c6ab865d5b19e3a57b76562adb9540cbf03d77c4 (patch) | |
| tree | b501bbc4cd2df84029e2eb875dd3f054c32b55df /mingling_core/src | |
| parent | 14374ac30f61085d80d59ad76c8ab1392acc76e7 (diff) | |
feat(macros): relax completion attribute signature
Allow completion functions to omit the context parameter, accept owned
ShellContext or any From<&ShellContext> type, and return any type
implementing Into<Suggest>. Add From<&str> for SuggestItem and derive
Clone for ShellContext.
Diffstat (limited to 'mingling_core/src')
| -rw-r--r-- | mingling_core/src/comp/shell_ctx.rs | 8 | ||||
| -rw-r--r-- | mingling_core/src/comp/suggest.rs | 13 |
2 files changed, 15 insertions, 6 deletions
diff --git a/mingling_core/src/comp/shell_ctx.rs b/mingling_core/src/comp/shell_ctx.rs index 552e514..3a1b8f2 100644 --- a/mingling_core/src/comp/shell_ctx.rs +++ b/mingling_core/src/comp/shell_ctx.rs @@ -96,7 +96,7 @@ use std::collections::HashSet; /// If the `-F` flag is present without a value, `shell_flag` becomes /// `ShellFlag::Other(String::new())`. If `-F` is absent, it defaults to /// `ShellFlag::Other("unknown".to_string())`. -#[derive(Default, Debug)] +#[derive(Default, Debug, Clone)] #[cfg_attr(feature = "structural_renderer", derive(serde::Serialize))] pub struct ShellContext { /// The full command line @@ -182,6 +182,12 @@ impl TryFrom<Vec<String>> for ShellContext { } } +impl From<&Self> for ShellContext { + fn from(ctx: &Self) -> Self { + ctx.clone() + } +} + impl ShellContext { /// Checks if a flag appears exactly once in the command line arguments. /// diff --git a/mingling_core/src/comp/suggest.rs b/mingling_core/src/comp/suggest.rs index 4a26c38..2cbd391 100644 --- a/mingling_core/src/comp/suggest.rs +++ b/mingling_core/src/comp/suggest.rs @@ -321,13 +321,10 @@ impl Suggest { impl<T> From<T> for Suggest where T: IntoIterator, - T::Item: Into<String>, + T::Item: Into<SuggestItem>, { fn from(items: T) -> Self { - let suggests = items - .into_iter() - .map(|item| SuggestItem::new(item.into())) - .collect(); + let suggests = items.into_iter().map(Into::into).collect(); Self::Suggest(suggests) } } @@ -701,6 +698,12 @@ impl From<String> for SuggestItem { } } +impl From<&str> for SuggestItem { + fn from(suggest: &str) -> Self { + Self::new(suggest.to_string()) + } +} + impl From<(String, String)> for SuggestItem { fn from((suggest, description): (String, String)) -> Self { Self::new_with_desc(suggest, description) |
