use {
crate::{ArgSpec, Command},
reovim_kernel::api::v1::{CommandId, Service},
};
#[derive(Debug, Clone)]
pub struct CommandInfo {
pub id: CommandId,
pub names: Vec<String>,
pub description: String,
pub args: Vec<ArgSpec>,
}
impl CommandInfo {
pub fn from_command<C: Command + ?Sized>(cmd: &C) -> Self {
Self {
id: cmd.id(),
names: cmd.names().iter().map(|s| (*s).to_string()).collect(),
description: cmd.description().to_string(),
args: cmd.args(),
}
}
#[must_use]
pub const fn has_user_names(&self) -> bool {
!self.names.is_empty()
}
}
pub trait CommandQueryService: Service + Send + Sync {
fn search_by_prefix(&self, prefix: &str) -> Vec<CommandInfo>;
fn find_by_name(&self, name: &str) -> Option<CommandInfo>;
fn list_user_commands(&self) -> Vec<CommandInfo>;
fn list_all(&self) -> Vec<CommandInfo>;
fn count(&self) -> usize;
}
pub struct CommandQueryProvider {
commands: Vec<CommandInfo>,
}
impl Service for CommandQueryProvider {}
impl CommandQueryProvider {
#[must_use]
pub const fn new(commands: Vec<CommandInfo>) -> Self {
Self { commands }
}
#[must_use]
pub fn list_all(&self) -> &[CommandInfo] {
&self.commands
}
#[must_use]
pub const fn count(&self) -> usize {
self.commands.len()
}
}
#[cfg(test)]
#[path = "query_tests.rs"]
mod tests;