|
| 1 | +use std::path::Path; |
| 2 | + |
| 3 | +use chrono::TimeDelta; |
| 4 | +use clap::{Parser, Subcommand}; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | + |
| 7 | +/// command line tool providing GBFS processing scripts |
| 8 | +#[derive(Parser)] |
| 9 | +#[command(author, version, about, long_about = None)] |
| 10 | +#[command(propagate_version = true)] |
| 11 | +pub struct GbfsCliArguments { |
| 12 | + /// select the GBFS operation to run |
| 13 | + #[command(subcommand)] |
| 14 | + pub op: GbfsOperation, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, Clone, Serialize, Deserialize, Subcommand)] |
| 18 | +pub enum GbfsOperation { |
| 19 | + /// runs a GBFS download, writing data from some source URL |
| 20 | + /// to an output directory. |
| 21 | + Download { |
| 22 | + /// a GBFS API URL |
| 23 | + #[arg(short, long)] |
| 24 | + gbfs_url: String, |
| 25 | + /// output directory path. |
| 26 | + #[arg(short, long, default_value_t = String::from("."))] |
| 27 | + output_directory: String, |
| 28 | + /// duration to collect data rows. provide in human-readable time values |
| 29 | + /// 2m, 30s, 2h, 2days... |
| 30 | + #[arg(short, long, value_parser = parse_duration, default_value = "10m")] |
| 31 | + collect_duration: TimeDelta, |
| 32 | + }, |
| 33 | +} |
| 34 | + |
| 35 | +impl GbfsOperation { |
| 36 | + pub fn run(&self) -> Result<(), String> { |
| 37 | + match self { |
| 38 | + GbfsOperation::Download { |
| 39 | + gbfs_url, |
| 40 | + output_directory, |
| 41 | + collect_duration, |
| 42 | + } => crate::app::download::run_gbfs_download( |
| 43 | + gbfs_url, |
| 44 | + Path::new(output_directory), |
| 45 | + collect_duration, |
| 46 | + ), |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +fn parse_duration(s: &str) -> Result<chrono::TimeDelta, String> { |
| 52 | + let std_duration = |
| 53 | + humantime::parse_duration(s).map_err(|e| format!("Invalid duration: {}", e))?; |
| 54 | + chrono::TimeDelta::from_std(std_duration).map_err(|e| format!("TimeDelta out of range: {}", e)) |
| 55 | +} |
0 commit comments