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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
use std::{collections::HashMap, fmt};
use chrono::{DateTime, FixedOffset};
use serde::{de, Deserialize};
/// The response from the `/trains` or `/trains/{:train_id}` endpoint.
#[derive(Debug, Clone)]
pub struct TrainResponse(
/// Each key in the hashmap is the string representation of the
/// [`train_num`] field. The value is a list of trains that have the
/// specified [`train_num`] field. I have not seen an instance where
/// multiple trains have the same [`train_num`] and therefore each list
/// in the map has only one item. It is possible for multiple trains to
/// have the same [`train_num`] so that case must be handled in the
/// client code.
///
/// [`train_num`]: Train::train_num
pub HashMap<String, Vec<Train>>,
);
/// Custom visitor used to deserialize responses from the `/trains` or
/// `/trains/{:train_id}` endpoint.
///
/// On empty data the Amtrak API will serialize an empty vector as `[]`. On
/// normal content responses, the API will instead serialize a dictionary using
/// `{"key1", "<content>"}`. This does not place nicely with serde which
/// (rightfully) expects the type to be the same for every endpoint response. To
/// handle this discrepancy, we implement our own visitor which will handle both
/// response.
struct TrainResponseVisitor;
impl<'de> de::Visitor<'de> for TrainResponseVisitor {
type Value = TrainResponse;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a HashMap or an empty array")
}
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: de::MapAccess<'de>,
{
Ok(TrainResponse(Deserialize::deserialize(
de::value::MapAccessDeserializer::new(map),
)?))
}
fn visit_seq<A>(self, _seq: A) -> Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
Ok(TrainResponse(HashMap::new()))
}
}
impl<'de> Deserialize<'de> for TrainResponse {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_any(TrainResponseVisitor)
}
}
/// Represents an Amtrak train
#[derive(Debug, Deserialize, Clone)]
pub struct Train {
/// The human readable route name of this train.
///
/// # Examples:
/// * "Keystone" (for the Keystone Corridor)
/// * "Northeast Regional" (for the Northeast Corridor)
#[serde(rename = "routeName")]
pub route_name: String,
/// The (possible unique) number identifying the train.
#[serde(rename = "trainNum")]
pub train_num: u32,
/// The concatenation of the [`train_num`] with another number (not sure
/// what exactly) in the format "{:train_num}-{:instance}".
///
/// # Examples:
/// * `6-4`
/// * `93-4`
///
/// [`train_num`]: Self::train_num
#[serde(rename = "trainID")]
pub train_id: String,
/// The current latitude of the train
pub lat: f64,
/// The current longitude of the train
pub lon: f64,
/// The human readable status of the timelyness of the train.
///
/// # Examples:
/// * `X Minutes Early`
/// * `X Hours, Y Minutes Early`
/// * `X Minutes Late`
/// * `X Hours, Y Minutes Late`
/// * `On Time`
/// * `Unknown`
/// * `NaN Minutes Early` (yes really)
#[serde(rename = "trainTimely")]
pub train_timely: String,
/// List of stations that the train will visit. The stations are listed in
/// the same order the train will stop at each.
pub stations: Vec<TrainStation>,
/// The current compass heading of the train.
pub heading: Heading,
/// Unsure of what this field symbolizes.
#[serde(rename = "eventCode")]
pub event_code: String,
/// Unsure of what this field symbolizes.
#[serde(rename = "eventTZ")]
pub event_tz: Option<String>,
/// Unsure of what this field symbolizes.
#[serde(rename = "eventName")]
pub event_name: Option<String>,
/// The station code where the train originated from (aka the first
/// station in this train's route).
///
/// # Examples:
/// * `PHL`
/// * `NYP`
#[serde(rename = "origCode")]
pub origin_code: String,
/// The timezone of the original station
///
/// # Examples:
/// * `America/New_York`
/// * `America/Chicago`
#[serde(rename = "originTZ")]
pub origin_tz: String,
/// The full human readable name of the station where the train originated
/// from (aka the first station in this train's route).
///
/// # Examples:
/// * `Philadelphia 30th Street`
/// * `New York Penn`
#[serde(rename = "origName")]
pub origin_name: String,
/// The station code where the train is heading to (aka the final
/// destination of the train).
///
/// # Examples:
/// * `PHL`
/// * `NYP`
#[serde(rename = "destCode")]
pub destination_code: String,
/// The timezone of destination station
///
/// # Examples:
/// * `America/New_York`
/// * `America/Chicago`
#[serde(rename = "destTZ")]
pub destination_tz: String,
/// The full human readable name of the station where the train is heading
/// (aka the final destination of the train).
///
/// # Examples:
/// * `Philadelphia 30th Street`
/// * `New York Penn`
#[serde(rename = "destName")]
pub destination_name: String,
/// The current state of the train
#[serde(rename = "trainState")]
pub train_state: TrainState,
/// The current velocity (in mph) of the train
pub velocity: f32,
/// A human readable status message.
///
/// # Examples:
/// * ` ` (Empty string, single whitespace)
/// * `SERVICE DISRUPTION`
#[serde(rename = "statusMsg")]
pub status_message: String,
/// The time at which this train entry was created. The entry will have the
/// local timezone as a fixed offset.
///
/// # Examples:
/// * `2023-09-04T07:46:06-04:00`
/// * `2023-09-04T07:00:00-05:00`
#[serde(rename = "createdAt")]
pub created_at: DateTime<FixedOffset>,
/// The time at which this train entry was last updated. The entry will have
/// the local timezone as a fixed offset.
///
/// # Examples:
/// * `2023-09-04T07:46:06-04:00`
/// * `2023-09-04T07:00:00-05:00`
#[serde(rename = "updatedAt")]
pub updated_at: DateTime<FixedOffset>,
/// Unsure of what this field symbolizes.
#[serde(rename = "lastValTS")]
pub last_value: DateTime<FixedOffset>,
/// Unsure of what this field symbolizes.
#[serde(rename = "objectID")]
pub object_id: u32,
}
#[derive(Debug, Deserialize, Clone)]
pub struct TrainStation {
/// The full human readable name of the station.
///
/// # Examples:
/// * `Philadelphia 30th Street`
/// * `New York Penn`
pub name: String,
/// The unique identification code of this station.
///
/// # Examples:
/// * `PHL`
/// * `NYP`
pub code: String,
/// The timezone of this station.
pub tz: String,
pub bus: bool,
/// The scheduled arrival time of this train for the current station.
#[serde(rename = "schArr")]
pub schedule_arrival: DateTime<FixedOffset>,
/// The scheduled departure time of this train for the current station.
#[serde(rename = "schDep")]
pub schedule_departure: DateTime<FixedOffset>,
/// The actual arrival time of this train for the current station specified
/// by [`name`] or [`code`]. When the [`status`] is [`Departed`] this
/// field shows a historical value of how late or early the train
/// arrived. When the [`status`] is [`Enroute`] this field is a
/// prediction on how late or early the train will arrive.
///
/// Examples:
/// `2023-09-05T16:22:00-05:00`
/// `2023-09-05T15:54:00-05:00`
/// `null` or not included in response
///
/// [`name`]: Self::name
/// [`code`]: Self::code
/// [`status`]: Self::status
/// [`Departed`]: TrainStatus::Departed
/// [`Enroute`]: TrainStatus::Enroute
#[serde(rename = "arr", default)]
pub arrival: Option<DateTime<FixedOffset>>,
/// The actual departure time of this train for the current station
/// specified by [`name`] or [`code`]. When the [`status`] is [`Departed`]
/// this field shows a historical value of how late or early the train
/// departed. When the [`status`] is [`Enroute`] this field is a
/// prediction on how late or early the train will depart.
///
/// Examples:
/// `2023-09-05T16:22:00-05:00`
/// `2023-09-05T15:54:00-05:00`
/// `null` or not included in response
///
/// [`name`]: Self::name
/// [`code`]: Self::code
/// [`status`]: Self::status
/// [`Departed`]: TrainStatus::Departed
/// [`Enroute`]: TrainStatus::Enroute
#[serde(rename = "dep", default)]
pub departure: Option<DateTime<FixedOffset>>,
/// A human readable comment on the arrival time of this train for current
/// station specified by [`name`] or [`code`]. When the [`status`] is
/// [`Departed`] this field shows a historical value of how late or
/// early the train arrived. When the [`status`] is [`Enroute`] this
/// field is a prediction on how late or early the train will arrive.
///
/// Examples:
/// `19 Minutes Late`
/// `On Time`
/// `NaN Minutes Early` (Yes really)
///
/// [`name`]: Self::name
/// [`code`]: Self::code
/// [`status`]: Self::status
/// [`Departed`]: TrainStatus::Departed
/// [`Enroute`]: TrainStatus::Enroute
#[serde(rename = "arrCmnt")]
pub arrival_comment: String,
/// A human readable comment on the departure time of this train for the
/// current station specified by [`name`] or [`code`]. When the
/// [`status`] is [`Departed`] this field shows a historical value of
/// how late or early the train departed. When the [`status`] is
/// [`Enroute`] this field is a prediction on how late or early the
/// train will depart.
///
/// Examples:
/// `19 Minutes Late`
/// `On Time`
/// `NaN Minutes Early` (Yes really)
///
/// [`name`]: Self::name
/// [`code`]: Self::code
/// [`status`]: Self::status
/// [`Departed`]: TrainStatus::Departed
/// [`Enroute`]: TrainStatus::Enroute
#[serde(rename = "depCmnt")]
pub departure_comment: String,
/// The current status of this train for the current station specified by
/// [`name`] or [`code`].
pub status: TrainStatus,
}
/// Describes a train's heading using cardinal directions
#[derive(Debug, Deserialize, Copy, Clone, PartialEq, Eq)]
pub enum Heading {
/// North heading
N,
/// Northeast heading
NE,
/// East heading
E,
/// Southeast heading
SE,
/// South heading
S,
/// Southwest heading
SW,
/// West heading
W,
/// Northwest heading
NW,
}
/// Represents the current status of an Amtrak train being tracked in
/// association with a [`Station`].
///
/// This status can only be applied to a combination of a [`Train`] and a
/// [`Station`]. It is referenced in the [`stations`] field.
///
/// [`Station`]: Station
/// [`Train`]: Train
/// [`stations`]: Train::stations
#[derive(Debug, Deserialize, Copy, Clone, PartialEq, Eq)]
pub enum TrainStatus {
/// The train has not yet arrived at the specified station.
Enroute,
/// The train is currently at the specified station.
Station,
/// The train has already arrived at departed from teh specified station.
Departed,
/// The status of the train is unknown
Unknown,
}
#[derive(Debug, Deserialize, Copy, Clone, PartialEq, Eq)]
pub enum TrainState {
/// The train is awaiting departure from its origin station
Predeparture,
/// The train is currently on its route.
Active,
/// The train has completed its journey is not longer servicing its route.
Completed,
}
/// The response from the `/stations` or `/stations/{:station_code}` endpoint.
#[derive(Debug, Clone)]
pub struct StationResponse(
/// Each key in the hashmap is the unique station code which will match the
/// [`code`] field. The value is the [`Station`] structure that is
/// associated with the unique station [`code`].
///
/// [`code`]: Station::code
/// [`Station`]: Station
pub HashMap<String, Station>,
);
/// Custom visitor used to deserialize responses from the `/stations` or
/// `/stations/{:station_code}` endpoint.
///
/// On empty data the Amtrak API will serialize an empty vector as `[]`. On
/// normal content responses, the API will instead serialize a dictionary using
/// `{"key1", "<content>"}`. This does not place nicely with serde which
/// (rightfully) expects the type to be the same for every endpoint response. To
/// handle this discrepancy, we implement our own visitor which will handle both
/// response.
struct StationResponseVisitor;
impl<'de> de::Visitor<'de> for StationResponseVisitor {
type Value = StationResponse;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a HashMap or an empty array")
}
fn visit_map<A>(self, map: A) -> Result<Self::Value, A::Error>
where
A: de::MapAccess<'de>,
{
Ok(StationResponse(Deserialize::deserialize(
de::value::MapAccessDeserializer::new(map),
)?))
}
fn visit_seq<A>(self, _seq: A) -> Result<Self::Value, A::Error>
where
A: de::SeqAccess<'de>,
{
Ok(StationResponse(HashMap::new()))
}
}
impl<'de> Deserialize<'de> for StationResponse {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
deserializer.deserialize_any(StationResponseVisitor)
}
}
/// Represents a unique station that Amtrak services
#[derive(Debug, Deserialize, Clone)]
pub struct Station {
/// The full human readable name of the station.
///
/// # Examples:
/// * `Philadelphia 30th Street`
/// * `New York Penn`
#[serde(default)]
pub name: String,
/// The unique identification code of this station.
///
/// # Examples:
/// * `PHL`
/// * `NYP`
pub code: String,
/// The timezone of the station
///
/// # Examples:
/// * `America/New_York`
/// * `America/Chicago`
#[serde(default)]
pub tz: String,
/// The latitude of the station
pub lat: f64,
/// The longitude of the station
pub lon: f64,
/// The first address line of the stations
///
/// # Examples:
/// * `2955 Market Street`
/// * `351 West 31st Street`
pub address1: String,
/// The second address line of the station
pub address2: String,
/// The city of the station
///
/// # Examples:
/// * `Philadelphia`
/// * `New York`
pub city: String,
/// The two character abbreviation of the state of the station
///
/// # Examples:
/// * `PA`
/// * `NY`
pub state: String,
/// The zip code of the station
///
/// # Examples:
/// * `19104`
/// * `10001`
pub zip: String,
/// A list of current [`train_id`] that have departed from or are enroute to
/// this station.
///
/// [`train_id`]: Train::train_id
pub trains: Vec<String>,
}