opensky/
lib.rs

1//! # opensky
2//!
3//! A Rust client for the OpenSky Network Trino database.
4//!
5//! This crate provides access to historical flight data from the OpenSky Network,
6//! allowing you to query ADS-B trajectory data for aircraft worldwide.
7//!
8//! ## Quick Start
9//!
10//! ```rust,no_run
11//! use opensky::{Trino, QueryParams};
12//!
13//! #[tokio::main]
14//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
15//!     // Create a Trino client (reads credentials from ~/.config/opensky/settings.conf)
16//!     let mut trino = Trino::new().await?;
17//!
18//!     // Query flight history
19//!     let params = QueryParams::new()
20//!         .icao24("485a32")
21//!         .time_range("2025-01-01 10:00:00", "2025-01-01 12:00:00");
22//!
23//!     let data = trino.history(params).await?;
24//!     println!("Got {} rows", data.len());
25//!
26//!     // Export to CSV
27//!     data.to_csv("flight.csv")?;
28//!
29//!     Ok(())
30//! }
31//! ```
32//!
33//! ## Configuration
34//!
35//! Credentials are read from a platform-specific config file:
36//! - Linux: `~/.config/opensky/settings.conf`
37//! - macOS: `~/Library/Application Support/opensky/settings.conf`
38//! - Windows: `%LOCALAPPDATA%\opensky\settings.conf`
39//!
40//! ```ini
41//! [default]
42//! username = your_username
43//! password = your_password
44//! ```
45//!
46//! Register for an account at <https://opensky-network.org/>.
47
48pub mod cache;
49pub mod config;
50pub mod query;
51pub mod trino;
52pub mod types;
53
54// Re-export main types for convenience
55pub use cache::{cache_dir, cache_stats, clear_cache, purge_old_cache, CacheStats};
56pub use config::Config;
57pub use query::{build_history_query, build_flightlist_query, build_rawdata_query, build_query_preview, build_query_preview_method};
58pub use trino::{QueryStatus, Trino};
59pub use types::{Bounds, FlightData, OpenSkyError, QueryParams, RawTable, Result, FLIGHT_COLUMNS, FLIGHTLIST_COLUMNS, RAWDATA_COLUMNS};
60
61// Re-export polars DataFrame for convenience
62pub use polars::frame::DataFrame;
63
64use std::path::Path;
65
66/// Write a DataFrame to a CSV file.
67pub fn write_csv(df: &DataFrame, path: impl AsRef<Path>) -> Result<()> {
68    use polars::prelude::*;
69    let mut file = std::fs::File::create(path.as_ref())?;
70    CsvWriter::new(&mut file)
71        .finish(&mut df.clone())
72        .map_err(|e| OpenSkyError::DataConversion(format!("Failed to write CSV: {}", e)))?;
73    Ok(())
74}
75
76/// Write a DataFrame to a Parquet file.
77pub fn write_parquet(df: &DataFrame, path: impl AsRef<Path>) -> Result<()> {
78    use polars::prelude::*;
79    let mut file = std::fs::File::create(path.as_ref())?;
80    ParquetWriter::new(&mut file)
81        .finish(&mut df.clone())
82        .map_err(|e| OpenSkyError::DataConversion(format!("Failed to write Parquet: {}", e)))?;
83    Ok(())
84}