use std::error;
use std::fmt;
use std::result;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
pub type Result<T> = result::Result<T, Error>;
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Error {
ExpectedSpeedOnFPL,
ExpectedLevelOnFPL,
UnexpectedRouteToken(String),
UnexpectedRunwayInRoute(String),
UnknownRunwayInRoute { arpt: String, rwy: String },
AmbiguousTerminalArea { wp: String, a: String, b: String },
InvalidA424 { record: Vec<u8>, error: String },
UnexpectedString,
ImplausibleValue,
UnknownLocationIndicator(String),
UnknownIdent(String),
InvalidRWYCC,
#[cfg(feature = "sqlite")]
Database(String),
UnexpectedMassesForStations,
UnexpectedNumberOfFuelStations,
ExceededFuelCapacityOnRamp,
ExceededFuelCapacityAfterLanding,
ExpectedRegistration,
ExpectedEmptyMass,
ExpectedEmptyBalance,
ExpectedFuelType,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::ExpectedSpeedOnFPL => write!(f, "FPL is missing cruise speed"),
Self::ExpectedLevelOnFPL => write!(f, "FPL is missing cruise level"),
Self::UnexpectedRouteToken(e) => write!(f, "invalid token {e} found in route"),
Self::UnexpectedRunwayInRoute(rwy) => {
write!(f, "runway {rwy} should follow an airport")
}
Self::UnknownRunwayInRoute { arpt, rwy } => {
write!(f, "unknown runway {rwy} found for {arpt}")
}
Self::AmbiguousTerminalArea { wp, a, b } => {
write!(f, "waypoint {wp} found in terminal area {a} and {b}")
}
Self::InvalidA424 { record, error } => {
let s = String::from_utf8_lossy(record);
write!(f, "invalid ARINC 424: {error} ({s})")
}
Self::UnexpectedString => write!(f, "unexpected string"),
Self::ImplausibleValue => write!(f, "value seams implausuble"),
Self::UnknownLocationIndicator(code) => write!(
f,
"location {code} should be according to ICAO document no. 7910"
),
Self::UnknownIdent(ident) => write!(f, "unknown ident {ident}"),
Self::InvalidRWYCC => write!(f, "RWYCC should be between 0 and 6"),
#[cfg(feature = "sqlite")]
Self::Database(msg) => write!(f, "database error: {msg}"),
Self::UnexpectedMassesForStations => {
write!(f, "mass should match to aircraft's stations")
}
Self::UnexpectedNumberOfFuelStations => {
write!(f, "fuel stations should match to aircraft's tanks")
}
Self::ExceededFuelCapacityOnRamp => {
write!(f, "fuel should not exceed tank capacity on ramp")
}
Self::ExceededFuelCapacityAfterLanding => {
write!(f, "fuel should not exceed tank capacity after landing")
}
Self::ExpectedRegistration => write!(f, "aircraft should have a registration"),
Self::ExpectedEmptyMass => write!(f, "aircraft should have an empty mass"),
Self::ExpectedEmptyBalance => write!(f, "aircraft should have an empty balance"),
Self::ExpectedFuelType => write!(f, "aircraft should have a fuel type defined"),
}
}
}
impl error::Error for Error {}
#[cfg(feature = "sqlite")]
impl From<rusqlite::Error> for Error {
fn from(err: rusqlite::Error) -> Self {
Self::Database(err.to_string())
}
}
#[cfg(feature = "sqlite")]
impl From<rusqlite_migration::Error> for Error {
fn from(err: rusqlite_migration::Error) -> Self {
Self::Database(err.to_string())
}
}