#[ allow( clippy ::std_instead_of_alloc, clippy ::std_instead_of_core ) ]
mod private
{
use crate :: *;
use former ::Former;
use indexmap ::IndexMap;
use iter_tools ::Itertools;
use grammar ::Command;
use crate ::ca ::Order;
#[ derive( Debug, Default, Former, Clone ) ]
pub struct Dictionary
{
#[ scalar( setter = false ) ]
pub( crate ) commands: IndexMap< String, Command >,
#[ scalar( setter = false ) ]
pub( crate ) order: Order,
}
impl DictionaryFormer
{
pub fn command( mut self, command: Command ) -> Self
{
let mut commands = self.storage.commands.unwrap_or_default();
commands.insert( command.phrase.clone(), command );
self.storage.commands = Some( commands );
self
}
}
impl Dictionary
{
pub fn register( &mut self, command: Command ) -> Option< Command >
{
self.commands.insert( command.phrase.clone(), command )
}
pub fn command< Name >( &self, name: &Name ) -> Option< &Command >
where
String: std ::borrow ::Borrow< Name >,
Name: std ::hash ::Hash + Eq,
{
self.commands.get( name )
}
pub fn search< NamePart >( &self, name_part: NamePart ) -> Vec< &Command >
where
NamePart: AsRef< str >,
{
self.commands.values().filter( | command | command.phrase.starts_with( name_part.as_ref() ) ).collect()
}
#[ must_use ]
pub fn commands( &self ) -> Vec< ( &String, &Command ) >
{
match self.order
{
Order ::Nature =>
{
self.commands.iter().collect()
}
Order ::Lexicography =>
{
self.commands.iter().sorted_by_key( | ( key, _ ) | *key ).collect()
}
}
}
}
}
crate ::mod_interface!
{
exposed use Dictionary;
}