Skip to content

Commit

Permalink
refactor(commands): rename module
Browse files Browse the repository at this point in the history
  • Loading branch information
radstevee committed Dec 23, 2024
1 parent b233a52 commit 95167be
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/lib/commands/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "commands"
name = "ferrumc-commands"
version = "0.1.0"
edition = "2021"

Expand Down
4 changes: 2 additions & 2 deletions src/lib/commands/src/arg/parser/int.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ impl ArgumentParser for IntParser {

match token.parse::<u32>() {
Ok(int) => Ok(Box::new(int)),
Err(err) => Err(error(err))
Err(err) => Err(error(err)),
}
}

Expand All @@ -22,4 +22,4 @@ impl ArgumentParser for IntParser {
{
IntParser
}
}
}
7 changes: 2 additions & 5 deletions src/lib/commands/src/arg/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ pub mod string;
pub(crate) mod utils;

pub trait ArgumentParser: Send + Sync {
fn parse(
&self,
context: Arc<&CommandContext>,
input: Arc<Mutex<CommandInput>>,
) -> ParserResult;
fn parse(&self, context: Arc<&CommandContext>, input: Arc<Mutex<CommandInput>>)
-> ParserResult;
fn new() -> Self
where
Self: Sized;
Expand Down
6 changes: 4 additions & 2 deletions src/lib/commands/src/arg/parser/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
TextComponentBuilder::new(err.to_string())
.color(NamedColor::Red)
.build()
}
2 changes: 1 addition & 1 deletion src/lib/commands/src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!();
Expand Down
2 changes: 1 addition & 1 deletion src/lib/commands/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,4 @@ impl CommandInput {
self.skip_whitespace(u32::MAX, preserve_single);
read_string
}
}
}
30 changes: 19 additions & 11 deletions src/lib/commands/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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<Box<dyn Any>, TextComponent>;
pub type CommandResult = Result<TextComponent, TextComponent>;
pub type CommandOutput = Pin<Box<dyn Future<Output=CommandResult> + Send + 'static>>;
pub type CommandExecutor = Arc<dyn for<'a> Fn(Arc<CommandContext>) -> CommandOutput + Send + Sync + 'static>;
pub type CommandOutput = Pin<Box<dyn Future<Output = CommandResult> + Send + 'static>>;
pub type CommandExecutor =
Arc<dyn for<'a> Fn(Arc<CommandContext>) -> CommandOutput + Send + Sync + 'static>;

pub struct Command {
pub name: &'static str,
Expand All @@ -30,7 +36,11 @@ impl Command {
(self.executor)(ctx)
}

pub fn validate(&self, ctx: Arc<&CommandContext>, input: Arc<Mutex<CommandInput>>) -> Result<(), TextComponent> {
pub fn validate(
&self,
ctx: Arc<&CommandContext>,
input: Arc<Mutex<CommandInput>>,
) -> Result<(), TextComponent> {
for arg in &self.args {
arg.parser.parse(ctx.clone(), input.clone())?;
}
Expand All @@ -44,7 +54,5 @@ where
F: Fn(Arc<CommandContext>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = CommandResult> + Send + 'static,
{
Arc::new(move |ctx: Arc<CommandContext>| {
Box::pin(func(ctx)) as CommandOutput
})
Arc::new(move |ctx: Arc<CommandContext>| Box::pin(func(ctx)) as CommandOutput)
}
49 changes: 27 additions & 22 deletions src/lib/commands/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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());
}

Expand All @@ -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());
}

0 comments on commit 95167be

Please sign in to comment.