Skip to content

Commit 5596ccc

Browse files
committed
Shared resolved_command conversion
1 parent 5beb298 commit 5596ccc

File tree

6 files changed

+43
-99
lines changed

6 files changed

+43
-99
lines changed

examples/errors.rs

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::time::Instant;
33
use anyhow::{self, Context};
44
use mini_async_repl::{
55
command::{
6-
ArgsError, Command, CommandArgInfo, CommandArgType, Critical, ExecuteCommand, Validator,
6+
resolved_command, Command, CommandArgInfo, CommandArgType, Critical, ExecuteCommand,
7+
Validator,
78
},
89
CommandStatus, Repl,
910
};
@@ -18,12 +19,6 @@ impl OkCommandHandler {
1819
async fn handle_command(&mut self) -> anyhow::Result<CommandStatus> {
1920
Ok(CommandStatus::Done)
2021
}
21-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
22-
match result {
23-
Ok(_) => Ok(CommandStatus::Done),
24-
Err(e) => Err(e.into()),
25-
}
26-
}
2722
}
2823
impl ExecuteCommand for OkCommandHandler {
2924
fn execute(
@@ -33,7 +28,7 @@ impl ExecuteCommand for OkCommandHandler {
3328
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
3429
let valid = Validator::validate(args.clone(), args_info.clone());
3530
if let Err(e) = valid {
36-
return Box::pin(OkCommandHandler::resolved(Err(e)));
31+
return Box::pin(resolved_command(Err(e)));
3732
}
3833
Box::pin(self.handle_command())
3934
}
@@ -48,12 +43,6 @@ impl RecoverableErrorHandler {
4843
Self::may_throw(text)?;
4944
Ok(CommandStatus::Done)
5045
}
51-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
52-
match result {
53-
Ok(_) => Ok(CommandStatus::Done),
54-
Err(e) => Err(e.into()),
55-
}
56-
}
5746
// this could be any function returning Result with an error implementing Error
5847
// here for simplicity we make use of the Other variant of std::io::Error
5948
fn may_throw(description: String) -> Result<(), std::io::Error> {
@@ -68,7 +57,7 @@ impl ExecuteCommand for RecoverableErrorHandler {
6857
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
6958
let valid = Validator::validate(args.clone(), args_info.clone());
7059
if let Err(e) = valid {
71-
return Box::pin(RecoverableErrorHandler::resolved(Err(e)));
60+
return Box::pin(resolved_command(Err(e)));
7261
}
7362
Box::pin(self.handle_command(args[0].clone()))
7463
}
@@ -92,12 +81,6 @@ impl CriticalErrorHandler {
9281
// }
9382
Ok(CommandStatus::Done)
9483
}
95-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
96-
match result {
97-
Ok(_) => Ok(CommandStatus::Done),
98-
Err(e) => Err(e.into()),
99-
}
100-
}
10184
// this could be any function returning Result with an error implementing Error
10285
// here for simplicity we make use of the Other variant of std::io::Error
10386
fn may_throw(description: String) -> Result<(), std::io::Error> {
@@ -112,7 +95,7 @@ impl ExecuteCommand for CriticalErrorHandler {
11295
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
11396
let valid = Validator::validate(args.clone(), args_info.clone());
11497
if let Err(e) = valid {
115-
return Box::pin(CriticalErrorHandler::resolved(Err(e)));
98+
return Box::pin(resolved_command(Err(e)));
11699
}
117100
Box::pin(self.handle_command(args[0].clone()))
118101
}
@@ -135,12 +118,6 @@ impl RouletteErrorHandler {
135118
}
136119
Ok(CommandStatus::Done)
137120
}
138-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
139-
match result {
140-
Ok(_) => Ok(CommandStatus::Done),
141-
Err(e) => Err(e.into()),
142-
}
143-
}
144121
// this could be any function returning Result with an error implementing Error
145122
// here for simplicity we make use of the Other variant of std::io::Error
146123
fn may_throw(description: String) -> Result<(), std::io::Error> {
@@ -155,7 +132,7 @@ impl ExecuteCommand for RouletteErrorHandler {
155132
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
156133
let valid = Validator::validate(args.clone(), args_info.clone());
157134
if let Err(e) = valid {
158-
return Box::pin(RouletteErrorHandler::resolved(Err(e)));
135+
return Box::pin(resolved_command(Err(e)));
159136
}
160137
Box::pin(self.handle_command())
161138
}

examples/from_str.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ use std::path::PathBuf;
33

44
use anyhow::{self, Context};
55
use mini_async_repl::{
6-
command::{ArgsError, Command, CommandArgInfo, CommandArgType, ExecuteCommand, Validator},
6+
command::{
7+
resolved_command, ArgsError, Command, CommandArgInfo, CommandArgType, ExecuteCommand,
8+
Validator,
9+
},
710
CommandStatus, Repl,
811
};
912
use std::future::Future;
@@ -20,12 +23,6 @@ impl LsCommandHandler {
2023
}
2124
Ok(CommandStatus::Done)
2225
}
23-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
24-
match result {
25-
Ok(_) => Ok(CommandStatus::Done),
26-
Err(e) => Err(e.into()),
27-
}
28-
}
2926
}
3027
impl ExecuteCommand for LsCommandHandler {
3128
fn execute(
@@ -35,7 +32,7 @@ impl ExecuteCommand for LsCommandHandler {
3532
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
3633
let valid = Validator::validate(args.clone(), args_info.clone());
3734
if let Err(e) = valid {
38-
return Box::pin(LsCommandHandler::resolved(Err(e)));
35+
return Box::pin(resolved_command(Err(e)));
3936
}
4037

4138
let dir_buf: PathBuf = args[0].clone().into();
@@ -52,12 +49,6 @@ impl IpAddrCommandHandler {
5249
println!("{}", ip);
5350
Ok(CommandStatus::Done)
5451
}
55-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
56-
match result {
57-
Ok(_) => Ok(CommandStatus::Done),
58-
Err(e) => Err(e.into()),
59-
}
60-
}
6152
}
6253
impl ExecuteCommand for IpAddrCommandHandler {
6354
fn execute(
@@ -67,19 +58,17 @@ impl ExecuteCommand for IpAddrCommandHandler {
6758
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
6859
let valid = Validator::validate(args.clone(), args_info.clone());
6960
if let Err(e) = valid {
70-
return Box::pin(IpAddrCommandHandler::resolved(Err(e)));
61+
return Box::pin(resolved_command(Err(e)));
7162
}
7263

7364
let ip = args[0].parse();
7465

7566
match ip {
7667
Ok(ip) => Box::pin(self.handle_command(ip)),
77-
Err(e) => Box::pin(IpAddrCommandHandler::resolved(Err(
78-
ArgsError::WrongArgumentValue {
79-
argument: args[0].clone(),
80-
error: e.to_string(),
81-
},
82-
))),
68+
Err(e) => Box::pin(resolved_command(Err(ArgsError::WrongArgumentValue {
69+
argument: args[0].clone(),
70+
error: e.to_string(),
71+
}))),
8372
}
8473
}
8574
}

examples/minimal.rs

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use anyhow::{self, Context};
22
use mini_async_repl::{
3-
command::{ArgsError, Command, CommandArgInfo, CommandArgType, ExecuteCommand, Validator},
3+
command::{
4+
resolved_command, Command, CommandArgInfo, CommandArgType, ExecuteCommand, Validator,
5+
},
46
CommandStatus, Repl,
57
};
68
use std::future::Future;
@@ -15,12 +17,6 @@ impl SayHelloCommandHandler {
1517
println!("Hello {}!", name);
1618
Ok(CommandStatus::Done)
1719
}
18-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
19-
match result {
20-
Ok(_) => Ok(CommandStatus::Done),
21-
Err(e) => Err(e.into()),
22-
}
23-
}
2420
}
2521
impl ExecuteCommand for SayHelloCommandHandler {
2622
fn execute(
@@ -30,7 +26,7 @@ impl ExecuteCommand for SayHelloCommandHandler {
3026
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
3127
let valid = Validator::validate(args.clone(), args_info.clone());
3228
if let Err(e) = valid {
33-
return Box::pin(SayHelloCommandHandler::resolved(Err(e)));
29+
return Box::pin(resolved_command(Err(e)));
3430
}
3531
Box::pin(self.handle_command(args[0].clone()))
3632
}
@@ -45,12 +41,6 @@ impl AddCommandHandler {
4541
println!("{} + {} = {}", x, y, x + y);
4642
Ok(CommandStatus::Done)
4743
}
48-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
49-
match result {
50-
Ok(_) => Ok(CommandStatus::Done),
51-
Err(e) => Err(e.into()),
52-
}
53-
}
5444
}
5545
impl ExecuteCommand for AddCommandHandler {
5646
fn execute(
@@ -61,7 +51,7 @@ impl ExecuteCommand for AddCommandHandler {
6151
// TODO: validator
6252
let valid = Validator::validate(args.clone(), args_info.clone());
6353
if let Err(e) = valid {
64-
return Box::pin(AddCommandHandler::resolved(Err(e)));
54+
return Box::pin(resolved_command(Err(e)));
6555
}
6656

6757
let x = args[0].parse::<i32>();
@@ -97,7 +87,7 @@ async fn main() -> anyhow::Result<()> {
9787
#[rustfmt::skip]
9888
let mut repl = Repl::builder()
9989
.add("hello", hello_cmd)
100-
.add("add", add_cmd)
90+
.add("add", add_cmd)
10191
.build()
10292
.context("Failed to create repl")?;
10393

examples/mut_state.rs

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use anyhow::{self, Context};
22
use mini_async_repl::{
3-
command::{ArgsError, Command, CommandArgInfo, CommandArgType, ExecuteCommand, Validator},
3+
command::{
4+
resolved_command, Command, CommandArgInfo, CommandArgType, ExecuteCommand, Validator,
5+
},
46
CommandStatus, Repl,
57
};
68
use std::cell::RefCell;
@@ -20,12 +22,6 @@ impl CountCommandHandler {
2022
println!();
2123
Ok(CommandStatus::Done)
2224
}
23-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
24-
match result {
25-
Ok(_) => Ok(CommandStatus::Done),
26-
Err(e) => Err(e.into()),
27-
}
28-
}
2925
}
3026
impl ExecuteCommand for CountCommandHandler {
3127
fn execute(
@@ -36,7 +32,7 @@ impl ExecuteCommand for CountCommandHandler {
3632
// TODO: validator
3733
let valid = Validator::validate(args.clone(), args_info.clone());
3834
if let Err(e) = valid {
39-
return Box::pin(CountCommandHandler::resolved(Err(e)));
35+
return Box::pin(resolved_command(Err(e)));
4036
}
4137

4238
let x = args[0].parse::<i32>();
@@ -58,12 +54,6 @@ impl SayCommandHandler {
5854
println!("x is equal to {}", x);
5955
Ok(CommandStatus::Done)
6056
}
61-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
62-
match result {
63-
Ok(_) => Ok(CommandStatus::Done),
64-
Err(e) => Err(e.into()),
65-
}
66-
}
6757
}
6858
impl ExecuteCommand for SayCommandHandler {
6959
fn execute(
@@ -73,7 +63,7 @@ impl ExecuteCommand for SayCommandHandler {
7363
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
7464
let valid = Validator::validate(args.clone(), args_info.clone());
7565
if let Err(e) = valid {
76-
return Box::pin(SayCommandHandler::resolved(Err(e)));
66+
return Box::pin(resolved_command(Err(e)));
7767
}
7868

7969
let x = args[0].parse::<f32>();
@@ -97,12 +87,6 @@ impl OutXCommandHandler {
9787
println!("{}", x);
9888
Ok(CommandStatus::Done)
9989
}
100-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
101-
match result {
102-
Ok(_) => Ok(CommandStatus::Done),
103-
Err(e) => Err(e.into()),
104-
}
105-
}
10690
}
10791
impl ExecuteCommand for OutXCommandHandler {
10892
fn execute(
@@ -112,7 +96,7 @@ impl ExecuteCommand for OutXCommandHandler {
11296
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
11397
let valid = Validator::validate(args.clone(), args_info.clone());
11498
if let Err(e) = valid {
115-
return Box::pin(OutXCommandHandler::resolved(Err(e)));
99+
return Box::pin(resolved_command(Err(e)));
116100
}
117101
Box::pin(self.handle_command())
118102
}

examples/overload.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
use anyhow::{self, Context};
22
use mini_async_repl::{
3-
command::{ArgsError, Command, CommandArgInfo, CommandArgType, ExecuteCommand, Validator},
3+
command::{
4+
resolved_command, ArgsError, Command, CommandArgInfo, CommandArgType, ExecuteCommand,
5+
Validator,
6+
},
47
CommandStatus, Repl,
58
};
69
use std::future::Future;
@@ -23,12 +26,6 @@ impl DescribeCommandHandler {
2326
println!("An integer `{}` and a string `{}`", a, b);
2427
Ok(CommandStatus::Done)
2528
}
26-
async fn resolved(result: Result<(), ArgsError>) -> Result<CommandStatus, anyhow::Error> {
27-
match result {
28-
Ok(_) => Ok(CommandStatus::Done),
29-
Err(e) => Err(e.into()),
30-
}
31-
}
3229
}
3330
impl ExecuteCommand for DescribeCommandHandler {
3431
fn execute(
@@ -38,7 +35,7 @@ impl ExecuteCommand for DescribeCommandHandler {
3835
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>> {
3936
let valid = Validator::validate(args.clone(), args_info.clone());
4037
if let Err(e) = valid {
41-
return Box::pin(DescribeCommandHandler::resolved(Err(e)));
38+
return Box::pin(resolved_command(Err(e)));
4239
}
4340

4441
// Note: this example could also be implemented by
@@ -88,9 +85,7 @@ impl ExecuteCommand for DescribeCommandHandler {
8885
}
8986
}
9087

91-
Box::pin(DescribeCommandHandler::resolved(Err(
92-
ArgsError::NoVariantFound,
93-
)))
88+
Box::pin(resolved_command(Err(ArgsError::NoVariantFound)))
9489
}
9590
}
9691

src/command.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ pub trait ExecuteCommand {
1616
) -> Pin<Box<dyn Future<Output = anyhow::Result<CommandStatus>> + '_>>;
1717
}
1818

19+
pub async fn resolved_command(
20+
result: Result<(), ArgsError>,
21+
) -> Result<CommandStatus, anyhow::Error> {
22+
match result {
23+
Ok(_) => Ok(CommandStatus::Done),
24+
Err(e) => Err(e.into()),
25+
}
26+
}
27+
1928
pub struct TrivialCommandHandler {}
2029
impl TrivialCommandHandler {
2130
pub fn new() -> Self {

0 commit comments

Comments
 (0)