Comboios Core
Core library for accessing Portuguese train (CP - Comboios de Portugal) data and schedules.
Overview
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.
Features
- Domain models for stations, trains, and timetables
- Async HTTP client with error handling and timeouts
- JSON deserialization with flexible type handling
- Comprehensive error types
- Logging support with tracing
Data Sources
- CP (Comboios de Portugal) - https://www.cp.pt/sites/spring
- IP (Infraestruturas de Portugal) - https://www.infraestruturasdeportugal.pt
Usage
Add to your Cargo.toml:
[]
= { = "path/to/cp-pt" }
= "0.12"
= { = "1.0", = ["full"] }
Basic Example
use ;
use Client;
async
API Functions
get_stations(client, station_name)
Search for stations by name.
Parameters:
client: reqwest::Client- HTTP client instancestation_name: &str- Station name to search for
Returns: Result<StationResponse, CoreError>
Example:
let stations = get_stations.await?;
get_station_timetable(client, station_id)
Get departure/arrival timetable for a station.
Parameters:
client: reqwest::Client- HTTP client instancestation_id: &str- Station ID from station search
Returns: Result<Vec<Timetable>, CoreError>
Example:
let timetable = get_station_timetable.await?;
get_train_details(client, train_id)
Get detailed information about a specific train.
Parameters:
client: reqwest::Client- HTTP client instancetrain_id: u16- Train number
Returns: Result<Train, CoreError>
Example:
let train = get_train_details.await?;
Error Handling
The library uses a custom CoreError type for HTTP and parsing errors:
use CoreError;
match get_stations.await
Dependencies
- reqwest - Async HTTP client
- serde - JSON serialization/deserialization
- serde_json - JSON support
- anyhow - Error handling
- thiserror - Custom error types
- tracing - Structured logging
Development
# Build
# Run tests
# Check types