-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.rs
More file actions
77 lines (70 loc) · 1.97 KB
/
Copy pathmain.rs
File metadata and controls
77 lines (70 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
mod chifoumi;
mod greetings;
mod search;
use chifoumi::{play, random_game, Game};
use greetings::greets;
use search::{display_news, search_news};
use clap::{Parser, Subcommand};
#[derive(Parser)]
#[clap(
author = "Julien Rollin",
version = "1.0.0",
about = "Crabby cli",
long_about = None
)]
#[clap(propagate_version = true)]
struct Cli {
#[clap(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Greets with name
Greets {
/// Name of the person to greet
#[clap(short, long, value_parser)]
name: String,
},
/// Play chifoumi with players
Chifoumi {
#[clap(short = 'a', long, value_enum)]
one: Game,
/// random game if not provided
#[clap(short = 'b', long, value_enum)]
two: Option<Game>,
},
/// Search news by keyword
Search {
/// search news related to this keyword
#[clap(short = 'k', long, value_parser)]
keyword: String,
},
}
const API_URL: &str = "https://hn.algolia.com";
fn main() {
let cli = Cli::parse();
match &cli.command {
Commands::Chifoumi { one, two } => match two {
// player two provided
Some(p) => {
let result = play(one.clone(), p.clone());
println!("p1: {:?} vs p2: {:?} => {:?}", one, p, result)
}
None => {
let random = random_game();
let result = play(one.clone(), random.clone());
println!("p1: {:?} vs p2: {:?} => {:?}", one, random, result)
}
},
Commands::Greets { name } => {
println!("{}", greets(name));
}
Commands::Search { keyword } => match search_news(API_URL, keyword) {
Ok(results) => {
let news = display_news(results).expect("Failed to display news");
println!("{news}");
}
Err(e) => eprintln!("err: {}", e),
},
}
}