Skip to content

Commit a2c8de5

Browse files
committed
Implement 04-enum-formatting
1 parent ef20868 commit a2c8de5

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

Cargo.toml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,6 @@ path = "src/03-subcommands.rs"
2525
name = "04-enum-formatting"
2626
path = "src/04-enum-formatting.rs"
2727

28-
[[bin]]
29-
name = "05-input-output-files"
30-
path = "src/05-input-output-files.rs"
31-
3228
[dependencies]
3329
structopt = { version = "0.3", features = [ "paw" ] }
3430
paw = "1.0"

src/04-enum-formatting.rs

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,52 @@
11
use structopt::StructOpt;
2+
use std::str::FromStr;
3+
use std::fmt::{Display, Formatter};
24

35
#[derive(Debug, StructOpt)]
46
#[structopt(name = "rustcli", about = "Rust CLI examples with StructOpt")]
5-
enum Opt {
6-
#[structopt(about = "Create a new entity")]
7-
Create {
8-
title: String,
9-
text: Option<String>
10-
},
11-
#[structopt(about = "Edit an existing entity")]
12-
Edit {
13-
id: i32,
14-
title: Option<String>,
15-
text: Option<String>
16-
},
17-
#[structopt(about = "List all entities")]
18-
List
7+
struct Opt {
8+
#[structopt(help = "Status of the task (todo, inprogress, blocked, done)")]
9+
status: Status
10+
}
11+
12+
#[derive(Debug)]
13+
enum Status {
14+
ToDo,
15+
InProgress,
16+
Done,
17+
Blocked
18+
}
19+
20+
impl FromStr for Status {
21+
// TODO: understand why I need this line
22+
type Err = ParseError;
23+
24+
fn from_str(s: &str) -> Result<Self, Self::Err> {
25+
// TODO: understand how to convert to lowercase here without String::from later
26+
match s {
27+
"todo" => Ok(Status::ToDo),
28+
"inprogress" => Ok(Status::InProgress),
29+
"blocked" => Ok(Status::Blocked),
30+
"done" => Ok(Status::Done),
31+
// TODO: understand how to simplify this
32+
_ => Err(ParseError(String::from("Status must be one of the following: todo, inprogress, blocked, or done.")))
33+
}
34+
}
35+
}
36+
37+
// TODO: research ways to reuse well-known errors
38+
#[derive(Debug)]
39+
struct ParseError(String);
40+
// TODO: understand the difference between std::error::Error and std::fmt::Error
41+
impl std::error::Error for ParseError { }
42+
impl Display for ParseError {
43+
// TODO: understand why lifetime specifier is required here
44+
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
45+
write!(f, "{}", self.0)
46+
}
1947
}
2048

2149
fn main() {
2250
let opt = Opt::from_args();
2351
println!("opt = {:?}", opt);
24-
match opt {
25-
Opt::Create { title, text } => println!("Processing Opt::Create with title = {}, text = {:?}", title, text),
26-
Opt::Edit { .. } => println!("Processing Opt::Edit with {:?}", opt),
27-
Opt::List => println!("Processing Opt::List"),
28-
}
2952
}

src/05-input-output-files.rs

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)