1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
mod parser;

pub use parser::weather_reports::metar;

/// Convenience function for converting a parsing error
/// into a [codespan_reporting::diagnostic::Diagnostic] for displaying to a user.
#[cfg(feature = "codespan_helpers")]
pub fn into_diagnostic(
    err: &peg::error::ParseError<peg::str::LineCol>,
) -> codespan_reporting::diagnostic::Diagnostic<()> {
    let expected_count = err.expected.tokens().count();
    let label_msg = if expected_count == 0 {
        "unclear cause".to_string()
    } else if expected_count == 1 {
        format!("expected {}", err.expected.tokens().next().unwrap())
    } else {
        let tokens = {
            let mut tokens = err.expected.tokens().collect::<Vec<_>>();
            tokens.sort_unstable();
            tokens
        };
        let mut acc = "expected one of ".to_string();
        for token in tokens.iter().take(expected_count - 1) {
            acc += token;
            acc += ", ";
        }
        acc += "or ";
        acc += tokens.last().unwrap();
        acc
    };
    codespan_reporting::diagnostic::Diagnostic::error()
        .with_message("could not parse report")
        .with_labels(vec![codespan_reporting::diagnostic::Label::primary(
            (),
            err.location.offset..err.location.offset,
        )
        .with_message(label_msg)])
}

#[cfg(test)]
mod tests {
    use super::parser::weather_reports::*;

    #[test]
    fn parse_icao_identifier() {
        for val in ["KSEA", "A302"] {
            icao_identifier(val).expect(val);
        }
    }

    #[test]
    fn parse_observation_time() {
        for val in ["251453Z"] {
            observation_time(val).expect(val);
        }
    }

    #[test]
    fn parse_wind() {
        for val in ["1804KT", "VRB04G19KT", "09015G25KT"] {
            wind(val).expect(val);
        }
    }

    #[test]
    fn parse_prevailing_visibility() {
        for val in ["1/2SM", "10SM"] {
            visibility(val).expect(val);
        }
    }

    #[test]
    fn parse_runway_visibility() {
        for val in ["R40/3000FT", "R01L/3500VP6000FT", "R06/0600N", "R31///////"] {
            runway_visibility(val).expect(val);
        }
    }

    #[test]
    fn parse_weather() {
        for val in ["-RA", "BR", "MIFG"] {
            weather(val).expect(val);
        }
    }

    #[test]
    fn parse_cloud_cover() {
        for val in ["FEW025", "SCT250"] {
            cloud_cover(val).expect(val);
        }
    }

    #[test]
    fn parse_temperatures() {
        for val in ["14/09", "24/M01", "14/"] {
            temperatures(val).expect(val);
        }
    }

    #[test]
    fn parse_pressure() {
        for val in ["A3002"] {
            pressure(val).expect(val);
        }
    }
}