-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.rs
More file actions
115 lines (106 loc) · 3.37 KB
/
main.rs
File metadata and controls
115 lines (106 loc) · 3.37 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use clap::{App, Arg};
use std::{fmt, io};
use world_map_gen::{draw, gen};
enum Error {
GenFail(world_map_gen::error::Error),
CliParseFail { name: String, msg: String },
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::GenFail(e) => write!(f, "{}", e),
Error::CliParseFail { name, msg } => {
write!(f, "Cannot parse CLI option '{}': {}", name, msg)
}
}
}
}
impl From<world_map_gen::error::Error> for Error {
fn from(err: world_map_gen::error::Error) -> Error {
Error::GenFail(err)
}
}
fn parse_opt<T>(name: &str, opt: Option<&str>) -> Result<Option<T>, Error>
where
T: std::str::FromStr,
<T as std::str::FromStr>::Err: fmt::Display,
{
match opt {
Some(s) => match s.parse::<T>() {
Ok(v) => Ok(Some(v)),
Err(e) => Err(Error::CliParseFail {
name: name.to_string(),
msg: format!("{}", e),
}),
},
None => Ok(None),
}
}
fn main() -> Result<(), Error> {
let matches = App::new("world-map-gen")
.version("0.1")
.author("rhysd <https://rhysd.github.io>")
.about("Random game world map generator")
.arg(
Arg::with_name("seed")
.short("s")
.long("seed")
.value_name("INTEGER")
.help("Seed for random number generator"),
)
.arg(
Arg::with_name("width")
.short("w")
.long("width")
.value_name("INTEGER")
.help("Board width in number of cells"),
)
.arg(
Arg::with_name("height")
.short("h")
.long("height")
.value_name("INTEGER")
.help("Board height in number of cells"),
)
.arg(
Arg::with_name("resolution")
.short("r")
.long("resolution")
.value_name("STRING")
.possible_values(&["low", "middle", "high"])
.help("Resolution of world map"),
)
.arg(
Arg::with_name("altitude")
.short("a")
.long("altitude")
.help("Show altitude instead of squre as cell mainly for debug"),
)
.arg(
Arg::with_name("json")
.short("j")
.long("json")
.help("Output JSON-serialized result to stdout"),
)
.get_matches();
let seed = parse_opt("seed", matches.value_of("seed"))?;
let width = parse_opt("width", matches.value_of("width"))?;
let height = parse_opt("height", matches.value_of("height"))?;
let resolution = matches.value_of("resolution").map(|s| match s {
"low" => gen::Resolution::Low,
"middle" => gen::Resolution::Middle,
"high" => gen::Resolution::High,
_ => unreachable!(),
});
let board = if let Some(seed) = seed {
gen::RandomBoardGen::from_seed(seed).gen(resolution, width, height)?
} else {
gen::RandomBoardGen::default().gen(resolution, width, height)?
};
if matches.is_present("json") {
draw::draw_json(&mut io::stdout(), &board)?;
} else {
draw::draw_term(&board, matches.is_present("altitude"))?;
}
Ok(())
}