3 个版本 (重大更新)

0.2.0 2024年5月10日
0.1.0 2023年9月6日
0.0.1 2023年9月1日

网页编程 中排名第 664

Download history 8/week @ 2024-04-26 82/week @ 2024-05-03 249/week @ 2024-05-10 85/week @ 2024-05-17 39/week @ 2024-05-24 57/week @ 2024-05-31 69/week @ 2024-06-07 31/week @ 2024-06-14 23/week @ 2024-06-21 15/week @ 2024-06-28 35/week @ 2024-07-05 94/week @ 2024-07-12 52/week @ 2024-07-19 46/week @ 2024-07-26 86/week @ 2024-08-02 31/week @ 2024-08-09

每月下载量 228
4 个 Crates 中使用 (通过 amtrak-gtfs-rt)

MIT 许可证

35KB
262

AMTRAK-API crates.io

本项目为开发者提供了一种标准且易用的 Rust API,用于调用 Amtrak 火车 API 中的各种端点。它处理请求和响应的序列化和反序列化,并允许开发者使用提供的良好定义的数据类型。它还处理 API 中的一些混乱部分(每个车站的多个序列化、奇特的端点响应、多个日期时间格式等)。

示例用法

//! # Example: Filter Trains
//!
//! This example shows how to filter trains based on the route name and then
//! determine what station the train is currently in route to.
use amtrak_api::{Client, TrainStatus};
use chrono::{Local, Utc};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    Client::new()
        .trains()
        .await?
        .into_iter()
        .flat_map(|(_, trains)| {
            trains
                .into_iter()
                .filter(|train| train.route_name == "Keystone")
        })
        .map(|train| {
            let enroute_information = train
                .stations
                .iter()
                .find(|station| station.status == TrainStatus::Enroute)
                .map(|station| (station.name.clone(), station.arrival));

            (train, enroute_information)
        })
        .for_each(|(train, enroute_information)| {
            if let Some((station_name, arrival)) = enroute_information {
                let time_till_arrival = if let Some(arrival) = arrival {
                    let local_now = Local::now().with_timezone(&Utc);
                    let arrival_utc = arrival.with_timezone(&Utc);

                    format!(
                        "{} minutes",
                        arrival_utc.signed_duration_since(local_now).num_minutes()
                    )
                } else {
                    "N/A".to_string()
                };

                println!(
                    "{} train is heading to {}, currently enroute to {} with an ETA of {}",
                    train.train_id, train.destination_name, station_name, time_till_arrival
                );
            } else {
                println!(
                    "{} train is heading to {}",
                    train.train_id, train.destination_code
                );
            }
        });
    Ok(())
}

示例输出

664-27 train is heading to New York Penn, currently enroute to Harrisburg with an ETA of 14 minutes
663-27 train is heading to Harrisburg, currently enroute to Newark Penn with an ETA of 10 minutes
660-27 train is heading to New York Penn, currently enroute to Philadelphia 30th Street with an ETA of -6 minutes
611-27 train is heading to Harrisburg, currently enroute to Harrisburg with an ETA of -1 minutes
661-27 train is heading to Harrisburg, currently enroute to Ardmore with an ETA of 4 minutes
662-27 train is heading to New York Penn, currently enroute to Parkesburg with an ETA of 2 minutes

作者

Stefan Bossbaly

许可证

本项目采用 MIT 许可证 - 详细信息请参阅 LICENSE 文件

致谢

依赖项

~5–19MB
~233K SLoC