use crate::{Alphanumeric, Error};
pub type RwyGrad<'a> = Alphanumeric<'a, 6>;
impl<'a> RwyGrad<'a> {
pub fn as_decimal(&self) -> Result<f32, Error> {
let slope = parse_numeric!(5, u32, self.0[1..])? as f32 / 1000.0;
match self.first() {
b'+' => Ok(slope),
b'-' => Ok(-slope),
byte => Err(Error::InvalidCharacter {
field: "Runway Gradient",
byte,
expected: "+ or -",
}),
}
}
}
#[cfg(test)]
mod tests {
use crate::FixedField;
use super::*;
#[test]
fn parse_upwward_gradient() {
assert_eq!(
RwyGrad::from_bytes(b"+10000").and_then(|v| v.as_decimal()),
Ok(10.0)
);
}
#[test]
fn parse_downwward_gradient() {
assert_eq!(
RwyGrad::from_bytes(b"-00450").and_then(|v| v.as_decimal()),
Ok(-0.45)
);
}
}