Skip to content

Commit d35b586

Browse files
committed
Introduce OverpassApiEndpoint enum for CLI
1 parent fd3a539 commit d35b586

File tree

5 files changed

+77
-15
lines changed

5 files changed

+77
-15
lines changed

AGENTS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,8 @@ This includes:
3636
- Choosing between two possible architectural approaches
3737
- Choosing between two libraries to use
3838
...and similar situations.
39+
40+
## Conversational Style
41+
42+
Don't summarize changes at the end of a prompt that you make as a direct result
43+
to instructions.

README.md

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,10 @@ Download charging station data with default settings:
5959

6060
evmap-osm-loader
6161

62-
**Note 1:** The API query may take multiple minutes. The default timeout is set
62+
**Note:** The API query may take multiple minutes. The default timeout is set
6363
to 15 minutes, but depending on the load on the API endpoint, this may not be
6464
sufficient.
6565

66-
**Note 2:** By default, the script uses the Overpass API interpreter at
67-
<https://overpass.osm.ch/api/interpreter>, which returns only data within
68-
Switzerland. This is great for testing with a reduced data set. For a worldwide
69-
result set, you can specify the URL `https://overpass-api.de/api/interpreter`
70-
using the `--overpass-url` option.
71-
7266
Show usage:
7367

7468
evmap-osm-loader --help
@@ -83,9 +77,12 @@ Options:
8377
Keep intermediate raw response file
8478
[default: false]
8579
86-
--overpass-url <OVERPASS_URL>
87-
Overpass API interpreter URL
88-
[default: https://overpass.osm.ch/api/interpreter]
80+
--overpass-api-endpoint <OVERPASS_API_ENDPOINT>
81+
Overpass API to use ('switzerland', 'world', or a custom URL)
82+
83+
The following hardcoded endpoints are supported:
84+
- switzerland (https://overpass.osm.ch/api/interpreter)
85+
- world (https://overpass-api.de/api/interpreter)
8986
9087
--timeout-seconds <TIMEOUT_SECONDS>
9188
Timeout in seconds for the Overpass query

src/cli.rs

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,43 @@
11
use clap::Parser;
22

3+
#[derive(Debug, Clone)]
4+
pub enum OverpassApiEndpoint {
5+
/// Use the API from overpass.osm.ch (which only includes Switzerland, good for quick testing)
6+
Switzerland,
7+
/// Use the API from overpass-api.de (which includes the whole world)
8+
World,
9+
/// Use a custom Overpass API URL
10+
Custom(String),
11+
}
12+
13+
impl OverpassApiEndpoint {
14+
/// Parse a string into an OverpassApiScope
15+
fn parse(s: &str) -> Result<Self, String> {
16+
match s {
17+
"switzerland" => Ok(OverpassApiEndpoint::Switzerland),
18+
"world" => Ok(OverpassApiEndpoint::World),
19+
url if url.starts_with("http://") || url.starts_with("https://") => {
20+
Ok(OverpassApiEndpoint::Custom(url.to_string()))
21+
}
22+
_ => Err(format!(
23+
"Invalid value '{}'. Expected 'switzerland', 'world', or a URL starting with http:// or https://",
24+
s
25+
)),
26+
}
27+
}
28+
}
29+
30+
impl OverpassApiEndpoint {
31+
/// Get the Overpass API URL for this scope
32+
pub fn get_url(&self) -> &str {
33+
match self {
34+
OverpassApiEndpoint::Switzerland => "https://overpass.osm.ch/api/interpreter",
35+
OverpassApiEndpoint::World => "https://overpass-api.de/api/interpreter",
36+
OverpassApiEndpoint::Custom(url) => url,
37+
}
38+
}
39+
}
40+
341
/// Download worldwide charging station data from Overpass API and write them to
442
/// a compressed JSON file.
543
#[derive(Parser, Debug)]
@@ -9,9 +47,14 @@ pub struct Args {
947
#[arg(long, value_name = "BOOL", default_value = "false")]
1048
pub keep_intermediate: bool,
1149

12-
/// Overpass API interpreter URL
13-
#[arg(long, default_value = "https://overpass.osm.ch/api/interpreter")]
14-
pub overpass_url: String,
50+
/// Overpass API to use ('switzerland', 'world', or a custom URL)
51+
///
52+
/// The following hardcoded endpoints are supported:
53+
///
54+
/// switzerland (https://overpass.osm.ch/api/interpreter),
55+
/// world (https://overpass-api.de/api/interpreter)
56+
#[arg(long, value_parser = OverpassApiEndpoint::parse)]
57+
pub overpass_api_endpoint: OverpassApiEndpoint,
1558

1659
/// Timeout in seconds for the Overpass query
1760
#[arg(long, default_value = "900")]
@@ -25,3 +68,10 @@ pub struct Args {
2568
#[arg(long, default_value = "charging-stations-osm.json.gz")]
2669
pub outfile_compressed: String,
2770
}
71+
72+
impl Args {
73+
/// Get the appropriate Overpass API URL based on the scope
74+
pub fn get_overpass_url(&self) -> &str {
75+
self.overpass_api_endpoint.get_url()
76+
}
77+
}

src/main.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod utils;
1010

1111
use cli::Args;
1212
use overpass::{download_data, parse_response, process_elements};
13-
use utils::{get_file_size_human, log_error, log_info};
13+
use utils::{get_file_size_human, log_debug, log_error, log_info};
1414

1515
/// Extra seconds to add to HTTP client timeout beyond the Overpass query timeout
1616
/// This provides buffer for network latency and response processing
@@ -39,12 +39,18 @@ async fn main() -> Result<()> {
3939
.context("Failed to build HTTP client")?;
4040

4141
// Step 1: Download data
42+
let overpass_api_endpoint = args.get_overpass_url();
4243
log_info(&format!(
4344
"1: Downloading data through Overpass API (this may take up to {} seconds...)",
4445
args.timeout_seconds
4546
));
47+
log_debug(&format!(
48+
"-> Using overpass API endpoint: {}",
49+
overpass_api_endpoint
50+
));
4651

47-
let response_bytes = download_data(&client, &args.overpass_url, args.timeout_seconds).await?;
52+
let response_bytes =
53+
download_data(&client, overpass_api_endpoint, args.timeout_seconds).await?;
4854

4955
// Parse the JSON response
5056
let overpass_response = parse_response(&response_bytes)?;

src/utils.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ pub fn log_info(msg: &str) {
44
println!("\x1b[32m{}\x1b[0m", msg);
55
}
66

7+
pub fn log_debug(msg: &str) {
8+
println!("{}", msg);
9+
}
10+
711
pub fn log_error(msg: &str) {
812
eprintln!("\x1b[31m{}\x1b[0m", msg);
913
}

0 commit comments

Comments
 (0)