Skip to content

Commit

Permalink
chore: restructure inbuilt commands and add uai to it
Browse files Browse the repository at this point in the history
  • Loading branch information
raklaptudirm committed Apr 19, 2024
1 parent 433c7b1 commit 23b9c81
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 16 deletions.
21 changes: 14 additions & 7 deletions uai/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Send, E: RunError> {
inbuilt: HashMap<String, inbuilt::Command>,
context: inbuilt::Context,
commands: HashMap<String, Command<T, E>>,
}

Expand All @@ -41,7 +41,7 @@ impl<T: Send + 'static, E: RunError + 'static> Client<T, E> {

// 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.
Expand Down Expand Up @@ -73,7 +73,7 @@ impl<T: Send + 'static, E: RunError + 'static> Client<T, E> {
}

// 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) {
Expand Down Expand Up @@ -158,10 +158,7 @@ impl<T: Send, E: RunError> Client<T, E> {
#[rustfmt::skip]
pub fn new() -> Self {
Client::<T, E> {
inbuilt: HashMap::from([
("quit".into(), inbuilt::quit()),
("isready".into(), inbuilt::isready()),
]),
context: Default::default(),
commands: HashMap::new(),
}
}
Expand All @@ -182,4 +179,14 @@ impl<T: Send, E: RunError> Client<T, E> {
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
}
}
54 changes: 45 additions & 9 deletions uai/src/inbuilt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Context, RunErrorType>;

pub fn quit() -> Command {
Command::new(|_ctx, _flag| quit!())
lazy_static! {
pub static ref COMMANDS: HashMap<String, Command> = 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(),
}
}
}

0 comments on commit 23b9c81

Please sign in to comment.