#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use super::*;
use crate::error::Error;
use crate::measurements::{Length, Mass};
#[derive(Clone, Eq, PartialEq, Debug, Default)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct AircraftBuilder {
registration: Option<String>,
icao_type: Option<String>,
stations: Vec<Station>,
empty_mass: Option<Mass>,
empty_balance: Option<Length>,
fuel_type: Option<FuelType>,
tanks: Vec<FuelTank>,
cg_envelope: Vec<CGLimit>,
notes: Option<String>,
}
impl AircraftBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn registration(&mut self, registration: String) -> &mut Self {
self.registration = Some(registration);
self
}
pub fn icao_type(&mut self, icao_type: String) -> &mut Self {
self.icao_type = Some(icao_type);
self
}
pub fn stations(&mut self, stations: Vec<Station>) -> &mut Self {
self.stations = stations;
self
}
pub fn empty_mass(&mut self, empty_mass: Mass) -> &mut Self {
self.empty_mass = Some(empty_mass);
self
}
pub fn empty_balance(&mut self, empty_balance: Length) -> &mut Self {
self.empty_balance = Some(empty_balance);
self
}
pub fn fuel_type(&mut self, fuel_type: FuelType) -> &mut Self {
self.fuel_type = Some(fuel_type);
self
}
pub fn tanks(&mut self, tanks: Vec<FuelTank>) -> &mut Self {
self.tanks = tanks;
self
}
pub fn cg_envelope(&mut self, cg_envelope: Vec<CGLimit>) -> &mut Self {
self.cg_envelope = cg_envelope;
self
}
pub fn notes(&mut self, notes: String) -> &mut Self {
self.notes = Some(notes);
self
}
pub fn build(&self) -> Result<Aircraft, Error> {
Ok(Aircraft {
registration: self
.registration
.clone()
.ok_or(Error::ExpectedRegistration)?,
icao_type: self.icao_type.clone().unwrap_or("ZZZZ".to_string()),
stations: self.stations.clone(),
empty_mass: self.empty_mass.ok_or(Error::ExpectedEmptyMass)?,
empty_balance: self.empty_balance.ok_or(Error::ExpectedEmptyBalance)?,
fuel_type: self.fuel_type.ok_or(Error::ExpectedFuelType)?,
tanks: self.tanks.clone(),
cg_envelope: CGEnvelope::new(self.cg_envelope.clone()),
notes: self.notes.clone(),
})
}
}