| Crates.io | comboios |
| lib.rs | comboios |
| version | 0.1.0 |
| created_at | 2025-06-13 21:07:42.954409+00 |
| updated_at | 2025-06-13 21:07:42.954409+00 |
| description | Unofficial Rust client for Comboios de Portugal (CP) APIs to search stations, get timetables, and retrieve train information |
| homepage | https://github.com/caiocdcs/comboios-rs |
| repository | https://github.com/caiocdcs/comboios-rs |
| max_upload_size | |
| id | 1711896 |
| size | 58,695 |
Core library for accessing Portuguese train (CP - Comboios de Portugal) data and schedules.
This is the foundational library that provides domain models, HTTP client functionality, and error handling for interacting with Portuguese train APIs. This crate was built as part of learning Rust and serves as the backbone for all other components in the CP-PT Rust project.
Add to your Cargo.toml:
[dependencies]
cp-pt = { path = "path/to/cp-pt" }
reqwest = "0.12"
tokio = { version = "1.0", features = ["full"] }
use cp_pt::client::{get_stations, get_station_timetable, get_train_details};
use reqwest::Client;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
// Search for stations
let stations = get_stations(client.clone(), "Lisboa").await?;
println!("Found {} stations", stations.data.len());
// Get timetable for a station
if let Some(station) = stations.data.first() {
let timetable = get_station_timetable(client.clone(), &station.node_id).await?;
println!("Found {} departures", timetable.len());
// Get details for a specific train
if let Some(departure) = timetable.first() {
let train = get_train_details(client, departure.train_number).await?;
println!("Train {} goes to {}", train.number, train.destination);
}
}
Ok(())
}
Search for stations by name.
Parameters:
client: reqwest::Client - HTTP client instancestation_name: &str - Station name to search forReturns: Result<StationResponse, CoreError>
Example:
let stations = get_stations(client, "Porto").await?;
Get departure/arrival timetable for a station.
Parameters:
client: reqwest::Client - HTTP client instancestation_id: &str - Station ID from station searchReturns: Result<Vec<Timetable>, CoreError>
Example:
let timetable = get_station_timetable(client, "94001").await?;
Get detailed information about a specific train.
Parameters:
client: reqwest::Client - HTTP client instancetrain_id: u16 - Train numberReturns: Result<Train, CoreError>
Example:
let train = get_train_details(client, 123).await?;
The library uses a custom CoreError type for HTTP and parsing errors:
use cp_pt::error::CoreError;
match get_stations(client, "InvalidStation").await {
Ok(stations) => println!("Found stations: {:?}", stations),
Err(CoreError::HttpError(e)) => eprintln!("Network error: {}", e),
Err(CoreError::ParseError(e)) => eprintln!("Parsing error: {}", e),
Err(e) => eprintln!("Other error: {}", e),
}
# Build
cargo build -p cp-pt
# Run tests
cargo test -p cp-pt
# Check types
cargo check -p cp-pt