diff --git a/src/lib/commands/Cargo.toml b/src/lib/commands/Cargo.toml index 2ce9e463..88d08ffa 100644 --- a/src/lib/commands/Cargo.toml +++ b/src/lib/commands/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "commands" +name = "ferrumc-commands" version = "0.1.0" edition = "2021" diff --git a/src/lib/commands/src/arg/parser/int.rs b/src/lib/commands/src/arg/parser/int.rs index b49dcab5..2569e52b 100644 --- a/src/lib/commands/src/arg/parser/int.rs +++ b/src/lib/commands/src/arg/parser/int.rs @@ -12,7 +12,7 @@ impl ArgumentParser for IntParser { match token.parse::() { Ok(int) => Ok(Box::new(int)), - Err(err) => Err(error(err)) + Err(err) => Err(error(err)), } } @@ -22,4 +22,4 @@ impl ArgumentParser for IntParser { { IntParser } -} \ No newline at end of file +} diff --git a/src/lib/commands/src/arg/parser/mod.rs b/src/lib/commands/src/arg/parser/mod.rs index c9cb1f92..1b2bc038 100644 --- a/src/lib/commands/src/arg/parser/mod.rs +++ b/src/lib/commands/src/arg/parser/mod.rs @@ -8,11 +8,8 @@ pub mod string; pub(crate) mod utils; pub trait ArgumentParser: Send + Sync { - fn parse( - &self, - context: Arc<&CommandContext>, - input: Arc>, - ) -> ParserResult; + fn parse(&self, context: Arc<&CommandContext>, input: Arc>) + -> ParserResult; fn new() -> Self where Self: Sized; diff --git a/src/lib/commands/src/arg/parser/utils.rs b/src/lib/commands/src/arg/parser/utils.rs index aeef1e31..4c688462 100644 --- a/src/lib/commands/src/arg/parser/utils.rs +++ b/src/lib/commands/src/arg/parser/utils.rs @@ -3,5 +3,7 @@ use std::error::Error; use ferrumc_text::{NamedColor, TextComponent, TextComponentBuilder}; pub(crate) fn error(err: impl Error) -> TextComponent { - TextComponentBuilder::new(err.to_string()).color(NamedColor::Red).build() -} \ No newline at end of file + TextComponentBuilder::new(err.to_string()) + .color(NamedColor::Red) + .build() +} diff --git a/src/lib/commands/src/ctx.rs b/src/lib/commands/src/ctx.rs index 87c3c9ab..e1d205ae 100644 --- a/src/lib/commands/src/ctx.rs +++ b/src/lib/commands/src/ctx.rs @@ -34,7 +34,7 @@ impl CommandContext { todo!("failed downcasting command argument, change design of this fn"); } }, - Err(_) => unreachable!("arg has already been validated") + Err(_) => unreachable!("arg has already been validated"), }; } else { todo!(); diff --git a/src/lib/commands/src/input.rs b/src/lib/commands/src/input.rs index e1dd0277..fb885eca 100644 --- a/src/lib/commands/src/input.rs +++ b/src/lib/commands/src/input.rs @@ -106,4 +106,4 @@ impl CommandInput { self.skip_whitespace(u32::MAX, preserve_single); read_string } -} \ No newline at end of file +} diff --git a/src/lib/commands/src/lib.rs b/src/lib/commands/src/lib.rs index 7abf30b9..c31bb2b4 100644 --- a/src/lib/commands/src/lib.rs +++ b/src/lib/commands/src/lib.rs @@ -1,23 +1,29 @@ -use std::{any::Any, future::Future, pin::Pin, sync::{Arc, Mutex}}; +use std::{ + any::Any, + future::Future, + pin::Pin, + sync::{Arc, Mutex}, +}; use arg::CommandArgument; use ctx::CommandContext; use ferrumc_text::TextComponent; use input::CommandInput; -pub mod errors; -pub mod input; -pub mod ctx; pub mod arg; +pub mod ctx; +pub mod errors; pub mod infrastructure; +pub mod input; #[cfg(test)] -pub(crate) mod tests; +mod tests; pub type ParserResult = Result, TextComponent>; pub type CommandResult = Result; -pub type CommandOutput = Pin + Send + 'static>>; -pub type CommandExecutor = Arc Fn(Arc) -> CommandOutput + Send + Sync + 'static>; +pub type CommandOutput = Pin + Send + 'static>>; +pub type CommandExecutor = + Arc Fn(Arc) -> CommandOutput + Send + Sync + 'static>; pub struct Command { pub name: &'static str, @@ -30,7 +36,11 @@ impl Command { (self.executor)(ctx) } - pub fn validate(&self, ctx: Arc<&CommandContext>, input: Arc>) -> Result<(), TextComponent> { + pub fn validate( + &self, + ctx: Arc<&CommandContext>, + input: Arc>, + ) -> Result<(), TextComponent> { for arg in &self.args { arg.parser.parse(ctx.clone(), input.clone())?; } @@ -44,7 +54,5 @@ where F: Fn(Arc) -> Fut + Send + Sync + 'static, Fut: Future + Send + 'static, { - Arc::new(move |ctx: Arc| { - Box::pin(func(ctx)) as CommandOutput - }) + Arc::new(move |ctx: Arc| Box::pin(func(ctx)) as CommandOutput) } diff --git a/src/lib/commands/src/tests.rs b/src/lib/commands/src/tests.rs index 4a4455fe..5a0c3ad4 100644 --- a/src/lib/commands/src/tests.rs +++ b/src/lib/commands/src/tests.rs @@ -6,16 +6,21 @@ use ferrumc_text::{TextComponentBuilder, TextContent}; use ferrumc_world::World; use tokio::net::TcpListener; -use crate::{arg::{parser::int::IntParser, CommandArgument}, ctx::CommandContext, executor, infrastructure::{find_command, register_command}, input::CommandInput, CommandResult}; +use crate::{ + arg::{parser::int::IntParser, CommandArgument}, + ctx::CommandContext, + executor, + infrastructure::{find_command, register_command}, + input::CommandInput, + CommandResult, +}; async fn state() -> GlobalState { - Arc::new( - ServerState { - universe: Universe::new(), - tcp_listener: TcpListener::bind("0.0.0.0:0").await.unwrap(), - world: World::new().await - } - ) + Arc::new(ServerState { + universe: Universe::new(), + tcp_listener: TcpListener::bind("0.0.0.0:0").await.unwrap(), + world: World::new().await, + }) } #[tokio::test] @@ -30,21 +35,21 @@ async fn arg_parse_test() { args: vec![CommandArgument { name: "number".to_string(), required: true, - parser: Box::new(IntParser) + parser: Box::new(IntParser), }], - executor: executor(test_executor) + executor: executor(test_executor), }; let command = Arc::new(command); - + let state = state().await; - + let ctx = CommandContext::new(CommandInput::of("42".to_string()), command.clone(), state); - + let result = command.execute(ctx).await; let TextContent::Text { text } = result.unwrap().content else { panic!("result is not text") }; - + assert_eq!(text, "42".to_string()); } @@ -60,24 +65,24 @@ async fn parse_test() { args: vec![CommandArgument { name: "number".to_string(), required: true, - parser: Box::new(IntParser) + parser: Box::new(IntParser), }], - executor: executor(test_executor) + executor: executor(test_executor), }; let command = Arc::new(command); - + let state = state().await; - + let ctx = CommandContext::new(CommandInput::of("42".to_string()), command.clone(), state); - + register_command(command.clone()); - + let found_command = find_command("input_test 42").unwrap(); - + let result = found_command.execute(ctx).await; let TextContent::Text { text } = result.unwrap().content else { panic!("result is not text") }; - + assert_eq!(text, "42".to_string()); }