Skip to content

Commit 59f1602

Browse files
committed
cargo fmt
1 parent d48fe8d commit 59f1602

File tree

9 files changed

+299
-211
lines changed

9 files changed

+299
-211
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "mini-async-repl"
33
version = "0.2.1"
44
authors = ["Martin Verzilli <[email protected]", "Jędrzej Boczar <[email protected]>"]
5-
edition = "2023"
5+
edition = "2021"
66
license = "MIT OR Apache-2.0"
77
description = "An async-first REPL"
88
repository = "https://github.com/manastech/mini-async-repl"

examples/errors.rs

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,19 @@ use std::time::Instant;
33
use anyhow::{self, Context};
44
use easy_repl::{
55
command::{
6-
ExecuteCommand,
7-
NewCommand,
8-
CommandArgInfo,
9-
CommandArgType,
10-
Validator,
11-
ArgsError,
12-
Critical,
6+
ArgsError, CommandArgInfo, CommandArgType, Critical, ExecuteCommand, NewCommand, Validator,
137
},
14-
CommandStatus,
15-
Repl,
8+
CommandStatus, Repl,
169
};
17-
use std::pin::Pin;
1810
use std::future::Future;
11+
use std::pin::Pin;
1912

2013
struct OkCommandHandler {}
2114
impl OkCommandHandler {
2215
pub fn new() -> Self {
2316
Self {}
2417
}
25-
async fn handle_command(&mut self) -> anyhow::Result<CommandStatus> {
18+
async fn handle_command(&mut self) -> anyhow::Result<CommandStatus> {
2619
Ok(CommandStatus::Done)
2720
}
2821
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
@@ -33,10 +26,17 @@ impl OkCommandHandler {
3326
}
3427
}
3528
impl ExecuteCommand for OkCommandHandler {
36-
fn execute(&mut self, args: Vec<String>) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
37-
let valid = Validator::validate(args.clone(), vec![
38-
CommandArgInfo::new_with_name(CommandArgType::String, "name"),
39-
]);
29+
fn execute(
30+
&mut self,
31+
args: Vec<String>,
32+
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
33+
let valid = Validator::validate(
34+
args.clone(),
35+
vec![CommandArgInfo::new_with_name(
36+
CommandArgType::String,
37+
"name",
38+
)],
39+
);
4040
if let Err(e) = valid {
4141
return Box::pin(OkCommandHandler::resolved(Err(e)));
4242
}
@@ -49,7 +49,7 @@ impl RecoverableErrorHandler {
4949
pub fn new() -> Self {
5050
Self {}
5151
}
52-
async fn handle_command(&mut self, text: String) -> anyhow::Result<CommandStatus> {
52+
async fn handle_command(&mut self, text: String) -> anyhow::Result<CommandStatus> {
5353
Self::may_throw(text)?;
5454
Ok(CommandStatus::Done)
5555
}
@@ -66,8 +66,17 @@ impl RecoverableErrorHandler {
6666
}
6767
}
6868
impl ExecuteCommand for RecoverableErrorHandler {
69-
fn execute(&mut self, args: Vec<String>) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
70-
let valid = Validator::validate(args.clone(), vec![CommandArgInfo::new_with_name(CommandArgType::String, "text")]);
69+
fn execute(
70+
&mut self,
71+
args: Vec<String>,
72+
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
73+
let valid = Validator::validate(
74+
args.clone(),
75+
vec![CommandArgInfo::new_with_name(
76+
CommandArgType::String,
77+
"text",
78+
)],
79+
);
7180
if let Err(e) = valid {
7281
return Box::pin(RecoverableErrorHandler::resolved(Err(e)));
7382
}
@@ -80,7 +89,7 @@ impl CriticalErrorHandler {
8089
pub fn new() -> Self {
8190
Self {}
8291
}
83-
async fn handle_command(&mut self, text: String) -> anyhow::Result<CommandStatus> {
92+
async fn handle_command(&mut self, text: String) -> anyhow::Result<CommandStatus> {
8493
// Short notation using the Critical trait
8594
Self::may_throw(text).into_critical()?;
8695
// More explicitly it could be:
@@ -106,8 +115,17 @@ impl CriticalErrorHandler {
106115
}
107116
}
108117
impl ExecuteCommand for CriticalErrorHandler {
109-
fn execute(&mut self, args: Vec<String>) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
110-
let valid = Validator::validate(args.clone(), vec![CommandArgInfo::new_with_name(CommandArgType::String, "text")]);
118+
fn execute(
119+
&mut self,
120+
args: Vec<String>,
121+
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
122+
let valid = Validator::validate(
123+
args.clone(),
124+
vec![CommandArgInfo::new_with_name(
125+
CommandArgType::String,
126+
"text",
127+
)],
128+
);
111129
if let Err(e) = valid {
112130
return Box::pin(CriticalErrorHandler::resolved(Err(e)));
113131
}
@@ -131,7 +149,6 @@ impl RouletteErrorHandler {
131149
_ => (),
132150
}
133151
Ok(CommandStatus::Done)
134-
135152
}
136153
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
137154
match result {
@@ -146,7 +163,10 @@ impl RouletteErrorHandler {
146163
}
147164
}
148165
impl ExecuteCommand for RouletteErrorHandler {
149-
fn execute(&mut self, args: Vec<String>) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
166+
fn execute(
167+
&mut self,
168+
args: Vec<String>,
169+
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
150170
let valid = Validator::validate(args.clone(), vec![]);
151171
if let Err(e) = valid {
152172
return Box::pin(RouletteErrorHandler::resolved(Err(e)));
@@ -192,4 +212,4 @@ async fn main() -> anyhow::Result<()> {
192212
Ok(())
193213
}
194214
}
195-
}
215+
}

examples/from_str.rs

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,18 @@ use std::path::PathBuf;
33

44
use anyhow::{self, Context};
55
use easy_repl::{
6-
command::{
7-
ExecuteCommand,
8-
NewCommand,
9-
CommandArgInfo,
10-
CommandArgType,
11-
Validator,
12-
ArgsError,
13-
},
14-
CommandStatus,
15-
Repl,
6+
command::{ArgsError, CommandArgInfo, CommandArgType, ExecuteCommand, NewCommand, Validator},
7+
CommandStatus, Repl,
168
};
17-
use std::pin::Pin;
189
use std::future::Future;
10+
use std::pin::Pin;
1911

2012
struct LsCommandHandler {}
2113
impl LsCommandHandler {
2214
pub fn new() -> Self {
2315
Self {}
2416
}
25-
async fn handle_command(&mut self, dir: PathBuf) -> anyhow::Result<CommandStatus> {
17+
async fn handle_command(&mut self, dir: PathBuf) -> anyhow::Result<CommandStatus> {
2618
for entry in dir.read_dir()? {
2719
println!("{}", entry?.path().to_string_lossy());
2820
}
@@ -36,10 +28,14 @@ impl LsCommandHandler {
3628
}
3729
}
3830
impl ExecuteCommand for LsCommandHandler {
39-
fn execute(&mut self, args: Vec<String>) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
40-
let valid = Validator::validate(args.clone(), vec![
41-
CommandArgInfo::new_with_name(CommandArgType::Custom, "dir"),
42-
]);
31+
fn execute(
32+
&mut self,
33+
args: Vec<String>,
34+
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
35+
let valid = Validator::validate(
36+
args.clone(),
37+
vec![CommandArgInfo::new_with_name(CommandArgType::Custom, "dir")],
38+
);
4339
if let Err(e) = valid {
4440
return Box::pin(LsCommandHandler::resolved(Err(e)));
4541
}
@@ -66,27 +62,32 @@ impl IpAddrCommandHandler {
6662
}
6763
}
6864
impl ExecuteCommand for IpAddrCommandHandler {
69-
fn execute(&mut self, args: Vec<String>) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
70-
let valid = Validator::validate(args.clone(), vec![
71-
CommandArgInfo::new_with_name(CommandArgType::Custom, "ip"),
72-
]);
65+
fn execute(
66+
&mut self,
67+
args: Vec<String>,
68+
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
69+
let valid = Validator::validate(
70+
args.clone(),
71+
vec![CommandArgInfo::new_with_name(CommandArgType::Custom, "ip")],
72+
);
7373
if let Err(e) = valid {
7474
return Box::pin(IpAddrCommandHandler::resolved(Err(e)));
7575
}
7676

7777
let ip = args[0].parse();
7878

7979
match ip {
80-
Ok(ip) => Box::pin(self.handle_command(ip)),
81-
Err(e) => Box::pin(IpAddrCommandHandler::resolved(Err(ArgsError::WrongArgumentValue {
82-
argument: args[0].clone(),
83-
error: e.to_string(),
84-
})))
80+
Ok(ip) => Box::pin(self.handle_command(ip)),
81+
Err(e) => Box::pin(IpAddrCommandHandler::resolved(Err(
82+
ArgsError::WrongArgumentValue {
83+
argument: args[0].clone(),
84+
error: e.to_string(),
85+
},
86+
))),
8587
}
8688
}
8789
}
8890

89-
9091
#[tokio::main]
9192
async fn main() -> anyhow::Result<()> {
9293
#[rustfmt::skip]

examples/minimal.rs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,17 @@
11
use anyhow::{self, Context};
22
use easy_repl::{
3-
command::{
4-
ExecuteCommand,
5-
NewCommand,
6-
CommandArgInfo,
7-
CommandArgType,
8-
Validator,
9-
ArgsError,
10-
},
11-
CommandStatus,
12-
Repl,
3+
command::{ArgsError, CommandArgInfo, CommandArgType, ExecuteCommand, NewCommand, Validator},
4+
CommandStatus, Repl,
135
};
14-
use std::pin::Pin;
156
use std::future::Future;
7+
use std::pin::Pin;
168

179
struct SayHelloCommandHandler {}
1810
impl SayHelloCommandHandler {
1911
pub fn new() -> Self {
2012
Self {}
2113
}
22-
async fn handle_command(&mut self, name: String) -> anyhow::Result<CommandStatus> {
14+
async fn handle_command(&mut self, name: String) -> anyhow::Result<CommandStatus> {
2315
println!("Hello {}!", name);
2416
Ok(CommandStatus::Done)
2517
}
@@ -31,10 +23,17 @@ impl SayHelloCommandHandler {
3123
}
3224
}
3325
impl ExecuteCommand for SayHelloCommandHandler {
34-
fn execute(&mut self, args: Vec<String>) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
35-
let valid = Validator::validate(args.clone(), vec![
36-
CommandArgInfo::new_with_name(CommandArgType::String, "name"),
37-
]);
26+
fn execute(
27+
&mut self,
28+
args: Vec<String>,
29+
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
30+
let valid = Validator::validate(
31+
args.clone(),
32+
vec![CommandArgInfo::new_with_name(
33+
CommandArgType::String,
34+
"name",
35+
)],
36+
);
3837
if let Err(e) = valid {
3938
return Box::pin(AddCommandHandler::resolved(Err(e)));
4039
}
@@ -47,9 +46,9 @@ impl AddCommandHandler {
4746
pub fn new() -> Self {
4847
Self {}
4948
}
50-
async fn handle_command(&mut self, x: i32, y:i32) -> anyhow::Result<CommandStatus> {
49+
async fn handle_command(&mut self, x: i32, y: i32) -> anyhow::Result<CommandStatus> {
5150
println!("{} + {} = {}", x, y, x + y);
52-
Ok(CommandStatus::Done)
51+
Ok(CommandStatus::Done)
5352
}
5453
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
5554
match result {
@@ -59,12 +58,18 @@ impl AddCommandHandler {
5958
}
6059
}
6160
impl ExecuteCommand for AddCommandHandler {
62-
fn execute(&mut self, args: Vec<String>) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
61+
fn execute(
62+
&mut self,
63+
args: Vec<String>,
64+
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
6365
// TODO: validator
64-
let valid = Validator::validate(args.clone(), vec![
65-
CommandArgInfo::new_with_name(CommandArgType::I32, "X"),
66-
CommandArgInfo::new_with_name(CommandArgType::I32, "Y"),
67-
]);
66+
let valid = Validator::validate(
67+
args.clone(),
68+
vec![
69+
CommandArgInfo::new_with_name(CommandArgType::I32, "X"),
70+
CommandArgInfo::new_with_name(CommandArgType::I32, "Y"),
71+
],
72+
);
6873
if let Err(e) = valid {
6974
return Box::pin(AddCommandHandler::resolved(Err(e)));
7075
}
@@ -74,7 +79,7 @@ impl ExecuteCommand for AddCommandHandler {
7479

7580
match (x, y) {
7681
(Ok(x), Ok(y)) => Box::pin(self.handle_command(x, y)),
77-
_ => panic!("Unreachable, validator should have covered this")
82+
_ => panic!("Unreachable, validator should have covered this"),
7883
}
7984
}
8085
}
@@ -83,7 +88,10 @@ impl ExecuteCommand for AddCommandHandler {
8388
async fn main() -> anyhow::Result<()> {
8489
let hello_cmd = NewCommand {
8590
description: "Say hello".into(),
86-
args_info: vec![CommandArgInfo::new_with_name(CommandArgType::String, "name")],
91+
args_info: vec![CommandArgInfo::new_with_name(
92+
CommandArgType::String,
93+
"name",
94+
)],
8795
handler: Box::new(SayHelloCommandHandler::new()),
8896
};
8997

0 commit comments

Comments
 (0)