Skip to content

Commit 444e21e

Browse files
committed
geocode: keep refactoring - still WIP
- improve usage text - embed qsv version in name of default geocode index file - add check_index_file helper fn - correct URL from where to download geocode index file when reset
1 parent 8bdaab0 commit 444e21e

File tree

1 file changed

+41
-27
lines changed

1 file changed

+41
-27
lines changed

src/cmd/geocode.rs

+41-27
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
static USAGE: &str = r#"
22
Geocodes a location against an updatable local copy of the Geonames cities index.
33
4-
It has three subcommands:
4+
It has three major subcommands:
55
* suggest - given a City name, return the closest location coordinate.
66
* reverse - given a location coordinate, return the closest City.
7-
* index - operations to update the Geonames cities index used by the geocode command.
7+
* index-* - operations to update the Geonames cities index used by the geocode command.
8+
(index-check, index-update, index-load & index-reset)
89
910
SUGGEST
1011
Geocodes to the nearest city center point given a location column
@@ -40,12 +41,11 @@ INDEX-<operation>
4041
Updates the Geonames cities index used by the geocode command.
4142
4243
It has four operations:
43-
* check - checks if the local Geonames index is up-to-date.
44+
* check - checks if the local Geonames index is up-to-date.
4445
* update - updates the local Geonames index with the latest changes from the Geonames website.
45-
* reset - resets the local Geonames index to the default Geonames cities index, downloading
46-
it from the qsv GitHub repo for that release.
47-
* load - load a Geonames cities index from a file, making it the default index from that point
48-
forward.
46+
* reset - resets the local Geonames index to the default Geonames cities index, downloading
47+
it from the qsv GitHub repo for that release.
48+
* load - load a Geonames cities index from a file, making it the default index going forward.
4949
5050
Examples:
5151
Update the Geonames cities index with the latest changes.
@@ -160,7 +160,9 @@ struct Args {
160160
flag_progressbar: bool,
161161
}
162162

163-
static DEFAULT_GEOCODE_INDEX_FILENAME: &str = "qsv-geocode-index.bincode";
163+
static QSV_VERSION: &str = env!("CARGO_PKG_VERSION");
164+
static DEFAULT_GEOCODE_INDEX_FILENAME: &str =
165+
concat!("qsv-", env!("CARGO_PKG_VERSION"), "-geocode-index.bincode");
164166

165167
static DEFAULT_CITIES_DB_URL: &str = "https://download.geonames.org/export/dump/cities15000.zip";
166168
static DEFAULT_CITIES_DB_FILENAME: &str = "cities15000.txt";
@@ -214,7 +216,7 @@ pub fn run(argv: &[&str]) -> CliResult<()> {
214216

215217
// main async geocode function that does the actual work
216218
async fn geocode_main(args: Args) -> CliResult<()> {
217-
eprintln!("args: {args:?}");
219+
// eprintln!("args: {args:?}");
218220

219221
let mut index_cmd = true;
220222

@@ -293,6 +295,7 @@ async fn geocode_main(args: Args) -> CliResult<()> {
293295
GeocodeSubCmd::IndexCheck => {
294296
// load geocode engine
295297
winfo!("Checking main Geonames website for updates...");
298+
check_index_file(&geocode_index_file)?;
296299
let engine =
297300
load_engine(geocode_index_file.clone().into(), args.flag_progressbar).await?;
298301

@@ -303,7 +306,11 @@ async fn geocode_main(args: Args) -> CliResult<()> {
303306
}
304307
},
305308
GeocodeSubCmd::IndexUpdate => {
306-
winfo!("Updating Geonames index. This will take a while...");
309+
check_index_file(&geocode_index_file)?;
310+
winfo!(
311+
"Updating Geonames index. This will take a while as we need to download \
312+
~200mb of data and rebuild the index..."
313+
);
307314
let engine = updater.build().await?;
308315
engine.dump_to(geocode_index_file.clone(), EngineDumpFormat::Bincode)?;
309316
winfo!("Updates applied: {geocode_index_file}");
@@ -312,19 +319,7 @@ async fn geocode_main(args: Args) -> CliResult<()> {
312319
// load alternate geocode index file
313320
if let Some(index_file) = args.arg_index_file {
314321
winfo!("Validating alternate Geonames index: {index_file}...");
315-
// check if index_file ends with a .bincode extension
316-
if !index_file.ends_with(".bincode") {
317-
return fail_incorrectusage_clierror!(
318-
"Alternate Geonames index file {index_file} does not have a .bincode \
319-
extension."
320-
);
321-
}
322-
// check if index_file exist
323-
if !Path::new(&index_file).exists() {
324-
return fail_incorrectusage_clierror!(
325-
"Alternate Geonames index file {index_file} does not exist."
326-
);
327-
}
322+
check_index_file(&index_file)?;
328323

329324
let engine = load_engine(index_file.clone().into(), true).await?;
330325
// we successfully loaded the alternate geocode index file, so its valid
@@ -348,6 +343,10 @@ async fn geocode_main(args: Args) -> CliResult<()> {
348343
if Path::new(&geocode_index_file).exists() {
349344
fs::remove_file(&geocode_index_file)?;
350345
}
346+
// loading the engine will download the default geocode index from the qsv GitHub
347+
// repo
348+
let _ = load_engine(geocode_index_file.clone().into(), true).await?;
349+
winfo!("Default Geonames index file {geocode_index_file} reset.");
351350
},
352351
_ => unreachable!("index_cmd is true, so this is unreachable."),
353352
}
@@ -488,6 +487,23 @@ async fn geocode_main(args: Args) -> CliResult<()> {
488487
Ok(wtr.flush()?)
489488
}
490489

490+
// check if index_file exists and ends with a .bincode extension
491+
fn check_index_file(index_file: &String) -> CliResult<()> {
492+
if !index_file.ends_with(".bincode") {
493+
return fail_incorrectusage_clierror!(
494+
"Alternate Geonames index file {index_file} does not have a .bincode extension."
495+
);
496+
}
497+
// check if index_file exist
498+
if !Path::new(index_file).exists() {
499+
return fail_incorrectusage_clierror!(
500+
"Alternate Geonames index file {index_file} does not exist."
501+
);
502+
}
503+
winfo!("Valid: {index_file}");
504+
Ok(())
505+
}
506+
491507
async fn load_engine(geocode_index_file: PathBuf, show_progress: bool) -> CliResult<Engine> {
492508
let index_file = std::path::Path::new(&geocode_index_file);
493509

@@ -500,17 +516,15 @@ async fn load_engine(geocode_index_file: PathBuf, show_progress: bool) -> CliRes
500516
);
501517
}
502518
} else {
503-
let qsv_version = env!("CARGO_PKG_VERSION");
504-
505519
// initial load, download index file from qsv releases
506520
if show_progress {
507521
woutinfo!(
508-
"No local index found. Downloading geocode index from qsv {qsv_version} release..."
522+
"No local index found. Downloading geocode index from qsv {QSV_VERSION} release..."
509523
);
510524
}
511525
util::download_file(
512526
&format!(
513-
"https://github.com/jqnatividad/qsv/releases/tag/{qsv_version}/qsv-geocode-index.bincode"
527+
"https://github.com/jqnatividad/qsv/releases/download/{QSV_VERSION}/qsv-{QSV_VERSION}-geocode-index.bincode"
514528
),
515529
&geocode_index_file.to_string_lossy(),
516530
show_progress,

0 commit comments

Comments
 (0)