forked from louis-e/arnis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathargs.rs
More file actions
368 lines (323 loc) · 11.6 KB
/
args.rs
File metadata and controls
368 lines (323 loc) · 11.6 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
use crate::coordinate_system::geographic::LLBBox;
use clap::{ArgAction, Parser};
use std::path::PathBuf;
use std::time::Duration;
/// Command-line arguments parser
#[derive(Parser, Debug)]
#[command(author, version, about)]
pub struct Args {
/// Bounding box of the area (min_lat,min_lng,max_lat,max_lng) (required)
#[arg(long, allow_hyphen_values = true, value_parser = LLBBox::from_str)]
pub bbox: LLBBox,
/// JSON file containing OSM data (optional)
#[arg(long, group = "location")]
pub file: Option<String>,
/// JSON file to save OSM data to (optional)
#[arg(long, group = "location")]
pub save_json_file: Option<String>,
/// Output directory for the generated world (required for Java, optional for Bedrock).
/// Use --output-dir (or the deprecated --path alias) to specify where the world is created.
#[arg(long = "output-dir", alias = "path")]
pub path: Option<PathBuf>,
/// Generate a Bedrock Edition world (.mcworld) instead of Java Edition
#[arg(long)]
pub bedrock: bool,
/// Downloader method (requests/curl/wget) (optional)
#[arg(long, default_value = "requests")]
pub downloader: String,
/// World scale to use, in blocks per meter
#[arg(long, default_value_t = 1.0)]
pub scale: f64,
/// Ground level to use in the Minecraft world
#[arg(long, default_value_t = -62)]
pub ground_level: i32,
/// Enable terrain (optional)
#[arg(long)]
pub terrain: bool,
/// Enable interior generation (optional)
#[arg(long, default_value_t = true, action = ArgAction::Set, num_args = 0..=1, default_missing_value = "true")]
pub interior: bool,
/// Enable roof generation (optional)
#[arg(long, default_value_t = true, action = ArgAction::Set, num_args = 0..=1, default_missing_value = "true")]
pub roof: bool,
/// Enable filling ground (optional)
#[arg(long, default_value_t = false)]
pub fillground: bool,
/// Enable land cover classification (optional)
/// When enabled, fetches ESA WorldCover satellite data to classify terrain
/// (forests, deserts, wetlands, built-up areas, etc.) and select appropriate
/// surface blocks. Requires --terrain to be enabled.
#[arg(long = "land-cover", alias = "city-boundaries", default_value_t = true, action = ArgAction::Set, num_args = 0..=1, default_missing_value = "true")]
pub land_cover: bool,
/// Enable debug mode (optional)
#[arg(long)]
pub debug: bool,
/// Set floodfill timeout (seconds) (optional)
#[arg(long, value_parser = parse_duration)]
pub timeout: Option<Duration>,
/// Spawn point latitude (optional, must be within bbox)
#[arg(long, allow_hyphen_values = true)]
pub spawn_lat: Option<f64>,
/// Spawn point longitude (optional, must be within bbox)
#[arg(long, allow_hyphen_values = true)]
pub spawn_lng: Option<f64>,
}
/// Validates CLI arguments after parsing.
/// For Java Edition: `--path` is required and must point to an existing directory
/// where a new world will be created automatically.
/// For Bedrock Edition (`--bedrock`): `--path` is optional (defaults to Desktop output).
pub fn validate_args(args: &Args) -> Result<(), String> {
if args.bedrock {
// Bedrock: path is optional; if provided, it must be an existing directory
if let Some(ref path) = args.path {
if !path.exists() {
return Err(format!("Path does not exist: {}", path.display()));
}
if !path.is_dir() {
return Err(format!("Path is not a directory: {}", path.display()));
}
}
} else {
// Java: path is required and must be an existing directory
match &args.path {
None => {
return Err(
"The --output-dir argument is required for Java Edition. Provide the directory where the world should be created. Use --bedrock for Bedrock Edition output."
.to_string(),
);
}
Some(ref path) => {
if !path.exists() {
return Err(format!("Path does not exist: {}", path.display()));
}
if !path.is_dir() {
return Err(format!("Path is not a directory: {}", path.display()));
}
}
}
}
// Validate spawn point: both or neither must be provided
match (args.spawn_lat, args.spawn_lng) {
(Some(_), None) | (None, Some(_)) => {
return Err("Both --spawn-lat and --spawn-lng must be provided together.".to_string());
}
(Some(lat), Some(lng)) => {
// Validate coordinates are valid lat/lng (rejects NaN, inf, out-of-range)
use crate::coordinate_system::geographic::LLPoint;
let llpoint =
LLPoint::new(lat, lng).map_err(|e| format!("Invalid spawn coordinates: {e}"))?;
// Validate that spawn point is within the bounding box
if !args.bbox.contains(&llpoint) {
return Err(
"Spawn point (--spawn-lat, --spawn-lng) must be within the bounding box."
.to_string(),
);
}
}
_ => {}
}
Ok(())
}
fn parse_duration(arg: &str) -> Result<std::time::Duration, std::num::ParseIntError> {
let seconds = arg.parse()?;
Ok(std::time::Duration::from_secs(seconds))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_flags() {
let tmpdir = tempfile::tempdir().unwrap();
let tmp_path = tmpdir.path().to_str().unwrap();
// Test that terrain/debug are SetTrue
let cmd = [
"arnis",
"--output-dir",
tmp_path,
"--bbox",
"1,2,3,4",
"--terrain",
"--debug",
];
let args = Args::parse_from(cmd.iter());
assert!(args.debug);
assert!(args.terrain);
let cmd = ["arnis", "--output-dir", tmp_path, "--bbox", "1,2,3,4"];
let args = Args::parse_from(cmd.iter());
assert!(!args.debug);
assert!(!args.terrain);
assert!(!args.bedrock);
// interior, roof, land_cover default to true
assert!(args.interior);
assert!(args.roof);
assert!(args.land_cover);
}
#[test]
fn test_bool_flags_can_be_disabled() {
let tmpdir = tempfile::tempdir().unwrap();
let tmp_path = tmpdir.path().to_str().unwrap();
// Test disabling interior/roof/land-cover with =false
let cmd = [
"arnis",
"--output-dir",
tmp_path,
"--bbox",
"1,2,3,4",
"--interior=false",
"--roof=false",
"--land-cover=false",
];
let args = Args::parse_from(cmd.iter());
assert!(!args.interior);
assert!(!args.roof);
assert!(!args.land_cover);
// Test enabling with bare flag (no value)
let cmd = [
"arnis",
"--output-dir",
tmp_path,
"--bbox",
"1,2,3,4",
"--interior",
"--roof",
"--land-cover",
];
let args = Args::parse_from(cmd.iter());
assert!(args.interior);
assert!(args.roof);
assert!(args.land_cover);
// Test backwards compatibility with old --city-boundaries alias
let cmd = [
"arnis",
"--output-dir",
tmp_path,
"--bbox",
"1,2,3,4",
"--city-boundaries=false",
];
let args = Args::parse_from(cmd.iter());
assert!(!args.land_cover);
}
#[test]
fn test_bedrock_flag() {
// Bedrock mode doesn't require --output-dir
let cmd = ["arnis", "--bedrock", "--bbox", "1,2,3,4"];
let args = Args::parse_from(cmd.iter());
assert!(args.bedrock);
assert!(args.path.is_none());
assert!(validate_args(&args).is_ok());
}
#[test]
fn test_java_requires_path() {
let cmd = ["arnis", "--bbox", "1,2,3,4"];
let args = Args::parse_from(cmd.iter());
assert!(!args.bedrock);
assert!(args.path.is_none());
assert!(validate_args(&args).is_err());
}
#[test]
fn test_java_path_must_exist() {
let cmd = [
"arnis",
"--output-dir",
"/nonexistent/path",
"--bbox",
"1,2,3,4",
];
let args = Args::parse_from(cmd.iter());
let result = validate_args(&args);
assert!(result.is_err());
assert!(result.unwrap_err().contains("does not exist"));
}
#[test]
fn test_bedrock_path_must_exist() {
let cmd = [
"arnis",
"--bedrock",
"--output-dir",
"/nonexistent/path",
"--bbox",
"1,2,3,4",
];
let args = Args::parse_from(cmd.iter());
let result = validate_args(&args);
assert!(result.is_err());
assert!(result.unwrap_err().contains("does not exist"));
}
#[test]
fn test_required_options() {
let tmpdir = tempfile::tempdir().unwrap();
let tmp_path = tmpdir.path().to_str().unwrap();
let cmd = ["arnis"];
assert!(Args::try_parse_from(cmd.iter()).is_err());
let cmd = ["arnis", "--output-dir", tmp_path, "--bbox", "1,2,3,4"];
let args = Args::try_parse_from(cmd.iter()).unwrap();
assert!(validate_args(&args).is_ok());
// Verify --path still works as a deprecated alias
let cmd = ["arnis", "--path", tmp_path, "--bbox", "1,2,3,4"];
let args = Args::try_parse_from(cmd.iter()).unwrap();
assert!(validate_args(&args).is_ok());
let cmd = ["arnis", "--output-dir", tmp_path, "--file", ""];
assert!(Args::try_parse_from(cmd.iter()).is_err());
// The --gui flag isn't used here, ugh. TODO clean up main.rs and its argparse usage.
// let cmd = ["arnis", "--gui"];
// assert!(Args::try_parse_from(cmd.iter()).is_ok());
}
#[test]
fn test_spawn_point_both_required() {
let tmpdir = tempfile::tempdir().unwrap();
let tmp_path = tmpdir.path().to_str().unwrap();
// Only spawn-lat without spawn-lng should fail validation
let cmd = [
"arnis",
"--output-dir",
tmp_path,
"--bbox",
"1,2,3,4",
"--spawn-lat",
"2.0",
];
let args = Args::parse_from(cmd.iter());
assert!(validate_args(&args).is_err());
// Only spawn-lng without spawn-lat should fail validation
let cmd = [
"arnis",
"--output-dir",
tmp_path,
"--bbox",
"1,2,3,4",
"--spawn-lng",
"3.0",
];
let args = Args::parse_from(cmd.iter());
assert!(validate_args(&args).is_err());
// Both provided and within bbox should pass
let cmd = [
"arnis",
"--output-dir",
tmp_path,
"--bbox",
"1,2,3,4",
"--spawn-lat",
"2.0",
"--spawn-lng",
"3.0",
];
let args = Args::parse_from(cmd.iter());
assert!(validate_args(&args).is_ok());
// Spawn point outside bbox should fail
let cmd = [
"arnis",
"--output-dir",
tmp_path,
"--bbox",
"1,2,3,4",
"--spawn-lat",
"5.0",
"--spawn-lng",
"3.0",
];
let args = Args::parse_from(cmd.iter());
assert!(validate_args(&args).is_err());
}
}