use chumsky::prelude::*;
use crate::parsers::some_whitespace;
use crate::traits::Parsable;
use super::Data;
use super::WindDirection;
use super::WindSpeed;
#[derive(PartialEq, Eq, Copy, Clone, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Wind {
Calm,
Present {
dir: WindDirection,
speed: WindSpeed,
varying: Option<(Data<u32>, Data<u32>)>,
},
}
impl Parsable for Wind {
fn parser<'src>() -> impl Parser<'src, &'src str, Self, extra::Err<crate::MetarError<'src>>> {
choice((
just("CALM")
.map(|_| Wind::Calm)
.then_ignore(some_whitespace()),
group((
WindDirection::parser(),
WindSpeed::parser().then_ignore(some_whitespace()),
choice((
group((WindDirection::parser(), just("V"), WindDirection::parser()))
.map(|(from, _, to)| Some((from.unwrap_heading(), to.unwrap_heading())))
.then_ignore(some_whitespace()),
empty().map(|()| None),
)),
))
.map(|(dir, speed, varying)| Wind::Present {
dir,
speed,
varying,
}),
))
}
}