Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use env feature of cargo #9

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 52 additions & 51 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ license = "MIT"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = { version = "4.5.26", features = ["derive"] }
clap = { version = "4.5.26", features = ["derive", "env"] }
fit_file = "0.6.0"
geo-types = "0.7.14"
gpx = "0.10.0"
Expand Down
14 changes: 3 additions & 11 deletions src/elevation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,13 @@ pub fn needed_tile_coords(wps: &[Waypoint]) -> Vec<(i32, i32)> {
// TODO: docs
pub fn read_needed_tiles(
needs: &[(i32, i32)],
elev_data_dir: Option<impl AsRef<Path>>,
elev_data_dir: impl AsRef<Path>,
) -> Vec<srtm_reader::Tile> {
if needs.is_empty() {
return vec![];
}
let elev_data_dir = elev_data_dir.as_ref();

let elev_data_dir = if let Some(arg_data_dir) = &elev_data_dir {
arg_data_dir.as_ref()
} else if let Some(env_data_dir) = option_env!("ELEV_DATA_DIR") {
Path::new(env_data_dir)
} else if let Some(env_data_dir) = option_env!("elev_data_dir") {
Path::new(env_data_dir)
} else {
panic!("no elevation data dir is passed as an arg nor set as an environment variable: ELEV_DATA_DIR");
};
needs
.par_iter()
.map(|c| srtm_reader::get_filename(*c))
Expand Down Expand Up @@ -79,7 +71,7 @@ pub fn get_all_elev_data<'a>(
/// use fit2gpx::elevation::*;
///
/// let mut fit = fit2gpx::Fit::from_file("evening walk.gpx").unwrap();
/// let elev_data_dir = Some("/home/me/Downloads/srtm_data");
/// let elev_data_dir = "/home/me/Downloads/srtm_data";
/// let needed_tile_coords = needed_tile_coords(&fit.track_segment.points);
/// let needed_tiles = read_needed_tiles(&needed_tile_coords, elev_data_dir);
/// let all_elev_data = get_all_elev_data(&needed_tile_coords, &needed_tiles);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl Fit {

#[cfg(feature = "elevation")]
/// add elevation data to the `fit` file, using srtm data from `elev_data_dir`
pub fn add_elev(fit: &mut Fit, elev_data_dir: Option<impl AsRef<Path>>) {
pub fn add_elev(fit: &mut Fit, elev_data_dir: impl AsRef<Path>) {
use elevation::*;
let needed_tile_coords = needed_tile_coords(&fit.track_segment.points);
let needed_tiles = read_needed_tiles(&needed_tile_coords, elev_data_dir);
Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ use std::path::PathBuf;
struct Args {
pub files: Vec<PathBuf>,
#[cfg(feature = "elevation")]
#[arg(short = 'd', long)]
pub elev_data_dir: Option<PathBuf>,
#[arg(short = 'd', long, env)]
pub elev_data_dir: PathBuf,
#[cfg(feature = "elevation")]
#[arg(short, long, default_value_t = false)]
#[arg(short, long, default_value_t = false, requires = "elev_data_dir")]
pub add_elevation: bool,
#[arg(short, long, default_value_t = false)]
pub overwrite: bool,
Expand Down
Loading