use std::any::type_name;
use std::sync::Arc;
use clap::builder::ArgExt;
use super::CompletionCandidate;
#[derive(Clone)]
pub struct ArgValueCompleter(Arc<dyn CustomCompleter>);
impl ArgValueCompleter {
pub fn new<C>(completer: C) -> Self
where
C: CustomCompleter + 'static,
{
Self(Arc::new(completer))
}
pub fn candidates(&self) -> Vec<CompletionCandidate> {
self.0.candidates()
}
}
impl std::fmt::Debug for ArgValueCompleter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(type_name::<Self>())
}
}
impl ArgExt for ArgValueCompleter {}
pub trait CustomCompleter: Send + Sync {
fn candidates(&self) -> Vec<CompletionCandidate>;
}
impl<F> CustomCompleter for F
where
F: Fn() -> Vec<CompletionCandidate> + Send + Sync,
{
fn candidates(&self) -> Vec<CompletionCandidate> {
self()
}
}