/
main.rs
145 lines (127 loc) · 4.46 KB
/
main.rs
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
// Copyright 2017 Hove and/or its affiliates.
//
// This program is free software: you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see
// <http://www.gnu.org/licenses/>.
use chrono::{DateTime, FixedOffset};
use clap::Parser;
use std::path::PathBuf;
use tracing::info;
use tracing_subscriber::{
filter::{EnvFilter, LevelFilter},
layer::SubscriberExt as _,
util::SubscriberInitExt as _,
};
use transit_model::{configuration, Result};
lazy_static::lazy_static! {
pub static ref GIT_VERSION: String = transit_model::binary_full_version(env!("CARGO_PKG_VERSION"));
}
fn get_version() -> &'static str {
&GIT_VERSION
}
#[derive(Debug, Parser)]
#[command(name = "gtfs2netexfr", about = "Convert a GTFS to NeTEx France.", version = get_version())]
struct Opt {
/// Input directory.
#[arg(short, long, default_value = ".")]
input: PathBuf,
/// Output directory.
#[arg(short, long)]
output: PathBuf,
/// JSON file containing additional configuration.
///
/// For more information, see
/// https://github.com/hove-io/transit_model/blob/master/documentation/common_ntfs_rules.md#configuration-of-each-converter
#[arg(short, long)]
config: Option<PathBuf>,
/// Indicates if the input GTFS contains On-Demand Transport (ODT)
/// information.
#[arg(short = 't', long = "on-demand-transport")]
odt: bool,
/// On-Demand Transport GTFS comment
#[arg(long = "odt-comment")]
odt_comment: Option<String>,
/// Name for the participant.
///
/// For more information, see
/// https://github.com/hove-io/transit_model/blob/master/documentation/ntfs_to_netex_france_specs.md#input-parameters
#[arg(short, long)]
participant: String,
/// Code for the provider of stops.
///
/// For more information, see
/// https://github.com/hove-io/transit_model/blob/master/documentation/ntfs_to_netex_france_specs.md#input-parameters
#[arg(short, long)]
stop_provider: Option<String>,
/// Current datetime.
#[arg(
short = 'x',
long,
default_value = &**transit_model::CURRENT_DATETIME
)]
current_datetime: DateTime<FixedOffset>,
}
fn init_logger() {
let default_level = LevelFilter::INFO;
let rust_log =
std::env::var(EnvFilter::DEFAULT_ENV).unwrap_or_else(|_| default_level.to_string());
let env_filter_subscriber = EnvFilter::try_new(rust_log).unwrap_or_else(|e| {
eprintln!(
"invalid {}, falling back to level '{}' - {}",
EnvFilter::DEFAULT_ENV,
default_level,
e,
);
EnvFilter::new(default_level.to_string())
});
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer())
.with(env_filter_subscriber)
.init();
}
fn run(opt: Opt) -> Result<()> {
info!("Launching gtfs2netexfr...");
let (contributor, dataset, feed_infos) = configuration::read_config(opt.config)?;
let configuration = transit_model::gtfs::Configuration {
contributor,
dataset,
feed_infos,
on_demand_transport: opt.odt,
on_demand_transport_comment: opt.odt_comment,
..Default::default()
};
let model = transit_model::gtfs::Reader::new(configuration).parse(opt.input)?;
let mut config = transit_model::netex_france::WriteConfiguration::new(opt.participant)
.current_datetime(opt.current_datetime);
if let Some(stop_provider) = opt.stop_provider {
config = config.stop_provider(stop_provider);
}
match opt.output.extension() {
Some(ext) if ext == "zip" => {
transit_model::netex_france::write_to_zip(&model, opt.output, config)?;
}
_ => {
transit_model::netex_france::write(&model, opt.output, config)?;
}
};
Ok(())
}
fn main() {
init_logger();
if let Err(err) = run(Opt::parse()) {
for cause in err.chain() {
eprintln!("{}", cause);
}
std::process::exit(1);
}
}