From 23b9c81be22aabf141410776ac7a6082c7a7e73c Mon Sep 17 00:00:00 2001 From: Rak Laptudirm Date: Fri, 19 Apr 2024 23:53:45 +0530 Subject: [PATCH] chore: restructure inbuilt commands and add uai to it --- uai/src/client.rs | 21 ++++++++++++------ uai/src/inbuilt.rs | 54 ++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 16 deletions(-) diff --git a/uai/src/client.rs b/uai/src/client.rs index f624975..f46a359 100644 --- a/uai/src/client.rs +++ b/uai/src/client.rs @@ -26,7 +26,7 @@ use super::{Command, FlagValues, RunError, RunErrorType}; /// Commands sent from the GUI are automatically parsed and executed according /// to the Command schema provided by the user to the Client. pub struct Client { - inbuilt: HashMap, + context: inbuilt::Context, commands: HashMap>, } @@ -41,7 +41,7 @@ impl Client { // Make the context thread safe to allow commands to run in parallel. let context = Arc::new(Mutex::new(context)); - let our_ctx = Arc::new(Mutex::new(Default::default())); + let our_ctx = Arc::new(Mutex::new(self.context.clone())); // Iterate over the lines in the input, since Commands for the GUI are // separated by newlines and we want to parse each Command separately. @@ -73,7 +73,7 @@ impl Client { } // Try to find a Command with the given name. - let cmd = self.inbuilt.get(cmd_name); + let cmd = inbuilt::COMMANDS.get(cmd_name); if cmd.is_some() { // Parsing complete, run the Command and handle any errors. match self.run(cmd.unwrap(), &our_ctx, args) { @@ -158,10 +158,7 @@ impl Client { #[rustfmt::skip] pub fn new() -> Self { Client:: { - inbuilt: HashMap::from([ - ("quit".into(), inbuilt::quit()), - ("isready".into(), inbuilt::isready()), - ]), + context: Default::default(), commands: HashMap::new(), } } @@ -182,4 +179,14 @@ impl Client { self.commands.insert(name.to_string(), cmd); self } + + pub fn engine(mut self, name: &str) -> Self { + self.context.engine = name.to_owned(); + self + } + + pub fn author(mut self, name: &str) -> Self { + self.context.author = name.to_owned(); + self + } } diff --git a/uai/src/inbuilt.rs b/uai/src/inbuilt.rs index 627f9db..bfcd6e2 100644 --- a/uai/src/inbuilt.rs +++ b/uai/src/inbuilt.rs @@ -11,20 +11,56 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::collections::HashMap; + use crate::{quit, RunErrorType}; +use lazy_static::lazy_static; pub type Command = crate::Command; -pub fn quit() -> Command { - Command::new(|_ctx, _flag| quit!()) +lazy_static! { + pub static ref COMMANDS: HashMap = HashMap::from( + [ + ("quit", Command::new(|_ctx, _flags| quit!())), + ( + "isready", + Command::new(|_ctx, _flags| { + println!("readyok"); + Ok(()) + }) + ), + ( + "uai", + Command::new(|ctx, _flags| { + let ctx = ctx.lock().unwrap(); + + println!("id name {}", ctx.engine); + println!("id author {}", ctx.author); + println!(); + println!("uaiok"); + + Ok(()) + }) + ) + ] + .map(|a| { + let (b, c) = a; + (b.to_owned(), c) + }) + ); } -pub fn isready() -> Command { - Command::new(|_ctx, _flag| { - println!("readyok"); - Ok(()) - }) +#[derive(Clone)] +pub struct Context { + pub engine: String, + pub author: String, } -#[derive(Default)] -pub struct Context {} +impl Default for Context { + fn default() -> Self { + Context { + engine: "Nameless v0.0.0".to_string(), + author: "Anonymous".to_string(), + } + } +}