Skip to content

Commit 95167be

Browse files
committed
refactor(commands): rename module
1 parent b233a52 commit 95167be

File tree

8 files changed

+57
-45
lines changed

8 files changed

+57
-45
lines changed

src/lib/commands/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "commands"
2+
name = "ferrumc-commands"
33
version = "0.1.0"
44
edition = "2021"
55

src/lib/commands/src/arg/parser/int.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ impl ArgumentParser for IntParser {
1212

1313
match token.parse::<u32>() {
1414
Ok(int) => Ok(Box::new(int)),
15-
Err(err) => Err(error(err))
15+
Err(err) => Err(error(err)),
1616
}
1717
}
1818

@@ -22,4 +22,4 @@ impl ArgumentParser for IntParser {
2222
{
2323
IntParser
2424
}
25-
}
25+
}

src/lib/commands/src/arg/parser/mod.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@ pub mod string;
88
pub(crate) mod utils;
99

1010
pub trait ArgumentParser: Send + Sync {
11-
fn parse(
12-
&self,
13-
context: Arc<&CommandContext>,
14-
input: Arc<Mutex<CommandInput>>,
15-
) -> ParserResult;
11+
fn parse(&self, context: Arc<&CommandContext>, input: Arc<Mutex<CommandInput>>)
12+
-> ParserResult;
1613
fn new() -> Self
1714
where
1815
Self: Sized;

src/lib/commands/src/arg/parser/utils.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@ use std::error::Error;
33
use ferrumc_text::{NamedColor, TextComponent, TextComponentBuilder};
44

55
pub(crate) fn error(err: impl Error) -> TextComponent {
6-
TextComponentBuilder::new(err.to_string()).color(NamedColor::Red).build()
7-
}
6+
TextComponentBuilder::new(err.to_string())
7+
.color(NamedColor::Red)
8+
.build()
9+
}

src/lib/commands/src/ctx.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl CommandContext {
3434
todo!("failed downcasting command argument, change design of this fn");
3535
}
3636
},
37-
Err(_) => unreachable!("arg has already been validated")
37+
Err(_) => unreachable!("arg has already been validated"),
3838
};
3939
} else {
4040
todo!();

src/lib/commands/src/input.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,4 +106,4 @@ impl CommandInput {
106106
self.skip_whitespace(u32::MAX, preserve_single);
107107
read_string
108108
}
109-
}
109+
}

src/lib/commands/src/lib.rs

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
use std::{any::Any, future::Future, pin::Pin, sync::{Arc, Mutex}};
1+
use std::{
2+
any::Any,
3+
future::Future,
4+
pin::Pin,
5+
sync::{Arc, Mutex},
6+
};
27

38
use arg::CommandArgument;
49
use ctx::CommandContext;
510
use ferrumc_text::TextComponent;
611
use input::CommandInput;
712

8-
pub mod errors;
9-
pub mod input;
10-
pub mod ctx;
1113
pub mod arg;
14+
pub mod ctx;
15+
pub mod errors;
1216
pub mod infrastructure;
17+
pub mod input;
1318

1419
#[cfg(test)]
15-
pub(crate) mod tests;
20+
mod tests;
1621

1722
pub type ParserResult = Result<Box<dyn Any>, TextComponent>;
1823
pub type CommandResult = Result<TextComponent, TextComponent>;
19-
pub type CommandOutput = Pin<Box<dyn Future<Output=CommandResult> + Send + 'static>>;
20-
pub type CommandExecutor = Arc<dyn for<'a> Fn(Arc<CommandContext>) -> CommandOutput + Send + Sync + 'static>;
24+
pub type CommandOutput = Pin<Box<dyn Future<Output = CommandResult> + Send + 'static>>;
25+
pub type CommandExecutor =
26+
Arc<dyn for<'a> Fn(Arc<CommandContext>) -> CommandOutput + Send + Sync + 'static>;
2127

2228
pub struct Command {
2329
pub name: &'static str,
@@ -30,7 +36,11 @@ impl Command {
3036
(self.executor)(ctx)
3137
}
3238

33-
pub fn validate(&self, ctx: Arc<&CommandContext>, input: Arc<Mutex<CommandInput>>) -> Result<(), TextComponent> {
39+
pub fn validate(
40+
&self,
41+
ctx: Arc<&CommandContext>,
42+
input: Arc<Mutex<CommandInput>>,
43+
) -> Result<(), TextComponent> {
3444
for arg in &self.args {
3545
arg.parser.parse(ctx.clone(), input.clone())?;
3646
}
@@ -44,7 +54,5 @@ where
4454
F: Fn(Arc<CommandContext>) -> Fut + Send + Sync + 'static,
4555
Fut: Future<Output = CommandResult> + Send + 'static,
4656
{
47-
Arc::new(move |ctx: Arc<CommandContext>| {
48-
Box::pin(func(ctx)) as CommandOutput
49-
})
57+
Arc::new(move |ctx: Arc<CommandContext>| Box::pin(func(ctx)) as CommandOutput)
5058
}

src/lib/commands/src/tests.rs

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ use ferrumc_text::{TextComponentBuilder, TextContent};
66
use ferrumc_world::World;
77
use tokio::net::TcpListener;
88

9-
use crate::{arg::{parser::int::IntParser, CommandArgument}, ctx::CommandContext, executor, infrastructure::{find_command, register_command}, input::CommandInput, CommandResult};
9+
use crate::{
10+
arg::{parser::int::IntParser, CommandArgument},
11+
ctx::CommandContext,
12+
executor,
13+
infrastructure::{find_command, register_command},
14+
input::CommandInput,
15+
CommandResult,
16+
};
1017

1118
async fn state() -> GlobalState {
12-
Arc::new(
13-
ServerState {
14-
universe: Universe::new(),
15-
tcp_listener: TcpListener::bind("0.0.0.0:0").await.unwrap(),
16-
world: World::new().await
17-
}
18-
)
19+
Arc::new(ServerState {
20+
universe: Universe::new(),
21+
tcp_listener: TcpListener::bind("0.0.0.0:0").await.unwrap(),
22+
world: World::new().await,
23+
})
1924
}
2025

2126
#[tokio::test]
@@ -30,21 +35,21 @@ async fn arg_parse_test() {
3035
args: vec![CommandArgument {
3136
name: "number".to_string(),
3237
required: true,
33-
parser: Box::new(IntParser)
38+
parser: Box::new(IntParser),
3439
}],
35-
executor: executor(test_executor)
40+
executor: executor(test_executor),
3641
};
3742
let command = Arc::new(command);
38-
43+
3944
let state = state().await;
40-
45+
4146
let ctx = CommandContext::new(CommandInput::of("42".to_string()), command.clone(), state);
42-
47+
4348
let result = command.execute(ctx).await;
4449
let TextContent::Text { text } = result.unwrap().content else {
4550
panic!("result is not text")
4651
};
47-
52+
4853
assert_eq!(text, "42".to_string());
4954
}
5055

@@ -60,24 +65,24 @@ async fn parse_test() {
6065
args: vec![CommandArgument {
6166
name: "number".to_string(),
6267
required: true,
63-
parser: Box::new(IntParser)
68+
parser: Box::new(IntParser),
6469
}],
65-
executor: executor(test_executor)
70+
executor: executor(test_executor),
6671
};
6772
let command = Arc::new(command);
68-
73+
6974
let state = state().await;
70-
75+
7176
let ctx = CommandContext::new(CommandInput::of("42".to_string()), command.clone(), state);
72-
77+
7378
register_command(command.clone());
74-
79+
7580
let found_command = find_command("input_test 42").unwrap();
76-
81+
7782
let result = found_command.execute(ctx).await;
7883
let TextContent::Text { text } = result.unwrap().content else {
7984
panic!("result is not text")
8085
};
81-
86+
8287
assert_eq!(text, "42".to_string());
8388
}

0 commit comments

Comments
 (0)