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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
use super::types::*;

/// A result with an error case of a 3-tuple containing the start offset, the length and the error
/// information.
type ParserResult<T, E> = Result<T, (usize, usize, E)>;

pub mod errors {
    use std::fmt;

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    /// An error caused whilst parsing the weather station
    pub enum StationError {
        /// The station ID is not the correct length
        IncorrectLength,
        /// A character was found to be not alphabetic
        NonAlphabeticCharacter,
    }

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    /// An error caused when parsing the observation time
    pub enum ObservationTimeError {
        /// The observation time is not the correct length
        IncorrectLength,
        /// The observation date is not valid
        DateNotValid,
        /// The observation hour is not valid
        HourNotValid,
        /// The observation minute is not valid
        MinuteNotValid,
        /// The specified time zone is not within the ICAO METAR standard
        InvalidTimeZone,
    }

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    /// An error caused when parsing the wind
    pub enum WindError {
        /// The wind heading is not valid
        HeadingNotValid,
        /// The wind speed was not valid
        SpeedNotValid,
        /// The wind gusting speed was not valid
        GustingNotValid,
        /// An unknown unit was read
        UnitNotValid,
    }

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    /// An error caused when parsing the wind varying
    pub enum WindVaryingError {
        /// The wind heading is not valid
        HeadingNotValid,
        /// Mostly an internal error - informs the calling function that this is not a wind varying
        /// and should be attempted to be parsed as cloud/visibility information
        NotWindVarying,
    }

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    /// An error caused when parsing the cloud and visibility information
    pub enum CloudVisibilityError {
        /// The data parsing was attempted upon is unknown in type
        UnknownData,
    }

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    /// An error caused when parsing the temperature
    pub enum TemperatureError {
        /// The temperature is not valid
        TemperatureNotValid,
        /// The dewpoint is not valid
        DewpointNotValid,
        /// Mostly an internal error - informs the calling function that this is not a
        /// temperature/dewpoint pair and should be attempted to be parsed as cloud/visibility
        /// information
        NotTemperatureDewpointPair,
    }

    #[derive(Copy, Clone, Debug, PartialEq, Eq)]
    /// An error caused when parsing the pressure
    pub enum PressureError {
        /// The pressure is not valid
        PressureNotValid,
        /// The unit is not valid
        UnitNotValid,
    }

    impl fmt::Display for StationError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::IncorrectLength => write!(f, "The station ID was not the correct length."),
                Self::NonAlphabeticCharacter => write!(f, "Found a non-alphabetic character."),
            }
        }
    }

    impl fmt::Display for ObservationTimeError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::IncorrectLength => write!(f, "The observation time was not the correct length."),
                Self::DateNotValid => write!(f, "The date was invalid."),
                Self::HourNotValid => write!(f, "The hour was invalid."),
                Self::MinuteNotValid => write!(f, "The minute was invalid."),
                Self::InvalidTimeZone => write!(f, "The time zone was invalid (not Zulu)."),
            }
        }
    }

    impl fmt::Display for WindError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::HeadingNotValid => write!(f, "The heading is invalid."),
                Self::SpeedNotValid => write!(f, "The speed is invalid."),
                Self::GustingNotValid => write!(f, "The gusting speed is invalid."),
                Self::UnitNotValid => write!(f, "The unit is not valid."),
            }
        }
    }

    impl fmt::Display for WindVaryingError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::HeadingNotValid => write!(f, "The heading is invalid."),
                Self::NotWindVarying => unreachable!(),
            }
        }
    }

    impl fmt::Display for CloudVisibilityError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::UnknownData => write!(f, "Unknown data for parsing."),
            }
        }
    }

    impl fmt::Display for TemperatureError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::TemperatureNotValid => write!(f, "The temperature is invalid."),
                Self::DewpointNotValid => write!(f, "The dewpoint is invalid."),
                Self::NotTemperatureDewpointPair => unreachable!(),
            }
        }
    }

    impl fmt::Display for PressureError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            match self {
                Self::PressureNotValid => write!(f, "The pressure is invalid."),
                Self::UnitNotValid => write!(f, "The unit is invalid."),
            }
        }
    }
}

use errors::*;

pub enum CloudVisibilityInfo {
    VerticalVisibility(VertVisibility),
    Visibility(Visibility),
    // TODO: Fully add RVRs
    RVR(),
    Weather(Weather),
    Clouds(Clouds),
    CloudLayer(CloudLayer),
}

pub fn parse_station<'a>(s: &'a str) -> ParserResult<&'a str, StationError> {
    if s.len() != 4 {
        // Not correct length
        return Err((0, s.len(), StationError::IncorrectLength));
    }

    let chs: Vec<_> = s.chars().collect();
    for i in 0..chs.len() {
        let c = chs[i];
        if !c.is_alphabetic() {
            return Err((i, 1, StationError::NonAlphabeticCharacter));
        }
    }
    Ok(s)
}

pub fn parse_obs_time<'a>(s: &'a str) -> ParserResult<Time, ObservationTimeError> {
    let mut time = Time {
        date: 0,
        hour: 0,
        minute: 0,
    };

    if s.len() != 7 {
        // Not correct length
        return Err((0, s.len(), ObservationTimeError::IncorrectLength));
    }

    let chs: Vec<_> = s.chars().collect();

    if !chs[0].is_digit(10) {
        // Not valid digit
        return Err((0, 1, ObservationTimeError::DateNotValid));
    } else if !chs[1].is_digit(10) {
        // Not valid digit
        return Err((1, 1, ObservationTimeError::DateNotValid));
    } else {
        // Date in range
        let date = s[0..2].parse().unwrap();
        if date > 31 {
            return Err((0, 2, ObservationTimeError::DateNotValid));
        }
        time.date = date;
    }

    if !chs[2].is_digit(10) {
        // Not valid digit
        return Err((2, 1, ObservationTimeError::HourNotValid));
    } else if !chs[3].is_digit(10) {
        // Not valid digit
        return Err((3, 1, ObservationTimeError::HourNotValid));
    } else {
        // Hour in range
        let hour = s[2..4].parse().unwrap();
        if hour > 23 {
            return Err((2, 2, ObservationTimeError::HourNotValid));
        }
        time.hour = hour;
    }

    if !chs[4].is_digit(10) {
        // Minute valid digit
        return Err((4, 1, ObservationTimeError::MinuteNotValid));
    } else if !chs[5].is_digit(10) {
        // Minute valid digit
        return Err((5, 1, ObservationTimeError::MinuteNotValid));
    } else {
        // Minute in range
        let minute = s[4..6].parse().unwrap();
        if minute > 59 {
            return Err((4, 2, ObservationTimeError::MinuteNotValid));
        }
        time.minute = minute;
    }

    if chs[6] != 'Z' {
        return Err((6, 1, ObservationTimeError::InvalidTimeZone));
    }

    Ok(time)
}

pub fn parse_wind<'a>(s: &'a str) -> ParserResult<Wind, WindError> {
    let mut wind = Wind {
        dir: WindDirection::Heading(0),
        speed: WindSpeed::Knot(0),
        varying: None,
        gusting: None,
    };

    let chs: Vec<_> = s.chars().collect();

    if &s[0..3] == "VRB" {
        wind.dir = WindDirection::Variable;
    } else if &s[0..3] == "ABV" {
        wind.dir = WindDirection::Above;
    } else if !chs[0].is_digit(10) {
        return Err((0, 1, WindError::HeadingNotValid));
    } else if !chs[1].is_digit(10) {
        return Err((1, 1, WindError::HeadingNotValid));
    } else if !chs[2].is_digit(10) {
        return Err((2, 1, WindError::HeadingNotValid));
    } else {
        let heading = s[0..3].parse().unwrap();
        if heading > 360 {
            return Err((0, 3, WindError::HeadingNotValid));
        }
        wind.dir = WindDirection::Heading(heading);
    }

    if !chs[3].is_digit(10) {
        return Err((3, 1, WindError::SpeedNotValid));
    } else if !chs[4].is_digit(10) {
        return Err((4, 1, WindError::SpeedNotValid));
    } else {
        let speed = s[3..5].parse().unwrap();

        if chs[5] == 'G' {
            if !chs[6].is_digit(10) {
                return Err((6, 1, WindError::GustingNotValid));
            } else if !chs[7].is_digit(10) {
                return Err((7, 1, WindError::GustingNotValid));
            }
            let g_speed = s[6..8].parse().unwrap();

            let unit = &s[8..];
            if unit == "KT" {
                wind.speed = WindSpeed::Knot(speed);
                wind.gusting = Some(WindSpeed::Knot(g_speed));
            } else if unit == "MPS" {
                wind.speed = WindSpeed::MetresPerSecond(speed);
                wind.gusting = Some(WindSpeed::MetresPerSecond(g_speed));
            } else {
                return Err((8, unit.len(), WindError::UnitNotValid));
            }
        } else {
            let unit = &s[5..];
            if unit == "KT" {
                wind.speed = WindSpeed::Knot(speed);
            } else if unit == "MPS" {
                wind.speed = WindSpeed::MetresPerSecond(speed);
            } else {
                return Err((5, unit.len(), WindError::UnitNotValid));
            }
        }
    }

    Ok(wind)
}

pub fn parse_wind_varying<'a>(s: &'a str) -> ParserResult<(u32, u32), WindVaryingError> {
    let chs: Vec<_> = s.chars().collect();

    if s.len() != 7 {
        return Err((0, s.len(), WindVaryingError::NotWindVarying));
    }

    if chs[3] != 'V' {
        return Err((3, 1, WindVaryingError::NotWindVarying));
    }

    if !chs[0].is_digit(10) {
        return Err((0, 1, WindVaryingError::HeadingNotValid));
    } else if !chs[1].is_digit(10) {
        return Err((1, 1, WindVaryingError::HeadingNotValid));
    } else if !chs[2].is_digit(10) {
        return Err((2, 1, WindVaryingError::HeadingNotValid));
    } else if !chs[4].is_digit(10) {
        return Err((4, 1, WindVaryingError::HeadingNotValid));
    } else if !chs[5].is_digit(10) {
        return Err((5, 1, WindVaryingError::HeadingNotValid));
    } else if !chs[6].is_digit(10) {
        return Err((6, 1, WindVaryingError::HeadingNotValid));
    } else {
        let heading_from = s[0..3].parse().unwrap();
        let heading_to = s[4..7].parse().unwrap();
        if heading_from > 360 {
            return Err((0, 3, WindVaryingError::HeadingNotValid));
        }
        if heading_to > 360 {
            return Err((4, 3, WindVaryingError::HeadingNotValid));
        }
        return Ok((heading_from, heading_to));
    }
}

pub fn parse_cloud_visibility_info<'a>(s: &'a str) -> ParserResult<CloudVisibilityInfo, CloudVisibilityError> {
    if s == "CAVOK" {
        return Ok(CloudVisibilityInfo::Visibility(Visibility::CavOK));
    }

    // Simple Cloud States
    if s == "CLR"
        || s == "SKC"{

        return Ok(CloudVisibilityInfo::Clouds(Clouds::SkyClear));
    }
    if s == "NCD" {
        return Ok(CloudVisibilityInfo::Clouds(Clouds::NoCloudDetected));
    }
    if s == "NSC" {
        return Ok(CloudVisibilityInfo::Clouds(Clouds::NoSignificantCloud));
    }

    let chs: Vec<_> = s.chars().collect();

    // Cloud layers
    if s.len() >= 6 {
        if &s[0..3] == "FEW"
            || &s[0..3] == "SCT"
            || &s[0..3] == "BKN"
            || &s[0..3] == "OVC"
            || &s[0..3] == "///" {

            let mut cloud_type = CloudType::Normal;
            if s.len() > 6 {
                let t = &s[6..];
                if t == "TCU" {
                    cloud_type = CloudType::ToweringCumulus;
                } else if t == "CB" {
                    cloud_type = CloudType::Cumulonimbus;
                } else if t == "///" {
                    cloud_type = CloudType::Unknown;
                }
            }

            let mut cloud_floor = None;
            if let Ok(floor) = s[3..6].parse() {
                cloud_floor = Some(floor);
            }

            let cl;
            if &s[0..3] == "FEW" {
                cl = CloudLayer::Few(cloud_type, cloud_floor);
            } else if &s[0..3] == "SCT" {
                cl = CloudLayer::Scattered(cloud_type, cloud_floor);
            } else if &s[0..3] == "BKN" {
                cl = CloudLayer::Broken(cloud_type, cloud_floor);
            } else if &s[0..3] == "OVC" {
                cl = CloudLayer::Overcast(cloud_type, cloud_floor);
            } else {
                cl = CloudLayer::Unknown(cloud_type, cloud_floor);
            }

            return Ok(CloudVisibilityInfo::CloudLayer(cl));
        }
    }

    // RVR
    if chs[0] == 'R'
        && chs[1].is_digit(10) {
        return Ok(CloudVisibilityInfo::RVR());
    }

    // Vertical visibility
    if chs[0] == 'V' && chs[1] == 'V' {
        if chs[2].is_digit(10) && chs[3].is_digit(10) {
            return Ok(CloudVisibilityInfo::VerticalVisibility(VertVisibility::Distance(s[2..3].parse().unwrap())));
        } else {
            return Ok(CloudVisibilityInfo::VerticalVisibility(VertVisibility::ReducedByUnknownAmount));
        }
    }

    // Visibility
    if chs[0].is_digit(10)
        && chs[1].is_digit(10)
        && chs[2].is_digit(10)
        && chs[3].is_digit(10) {

        return Ok(CloudVisibilityInfo::Visibility(Visibility::Metres(s[0..4].parse().unwrap())));
    }
    if chs[0].is_digit(10)
        && chs[1].is_digit(10)
        && chs[2] == 'S'
        && chs[3] == 'M' {

        return Ok(CloudVisibilityInfo::Visibility(Visibility::StatuteMiles(s[0..2].parse().unwrap())));
    }
    if chs[0].is_digit(10)
        && chs[1] == 'S'
        && chs[2] == 'M' {

        return Ok(CloudVisibilityInfo::Visibility(Visibility::StatuteMiles(s[0..1].parse().unwrap())));
    }

    if s.len() < 2 {
        // Not long enough for any weather.
        return Err((0, s.len(), CloudVisibilityError::UnknownData));
    }

    // Weather
    let intensity;
    let mut i = 0;
    if chs[0] == '+' {
        intensity = WeatherIntensity::Heavy;
        i += 1;
    } else if chs[0] == '-' {
        intensity = WeatherIntensity::Light;
        i += 1;
    } else if chs[0] == 'V'
        && chs[1] == 'C' {
        // Vicinity
        intensity = WeatherIntensity::InVicinity;
        i += 2;
    } else {
        intensity = WeatherIntensity::Moderate;
    }
    let mut conditions = Vec::new();
    loop {
        if s.len() < i + 2 {
            break;
        }
        let mut s = String::new();
        s.push(chs[i]);
        s.push(chs[i + 1]);

        if s == "MI" {
            conditions.push(WeatherCondition::Shallow);
        } else if s == "PR" {
            conditions.push(WeatherCondition::Partial);
        } else if s == "BC" {
            conditions.push(WeatherCondition::Patches);
        } else if s == "DR" {
            conditions.push(WeatherCondition::LowDrifting);
        } else if s == "BL" {
            conditions.push(WeatherCondition::Blowing);
        } else if s == "SH" {
            conditions.push(WeatherCondition::Showers);
        } else if s == "TS" {
            conditions.push(WeatherCondition::Thunderstorm);
        } else if s == "FZ" {
            conditions.push(WeatherCondition::Freezing);
        } else if s == "RA" {
            conditions.push(WeatherCondition::Rain);
        } else if s == "DZ" {
            conditions.push(WeatherCondition::Drizzle);
        } else if s == "SN" {
            conditions.push(WeatherCondition::Snow);
        } else if s == "SG" {
            conditions.push(WeatherCondition::SnowGrains);
        } else if s == "IC" {
            conditions.push(WeatherCondition::IceCrystals);
        } else if s == "PL" {
            conditions.push(WeatherCondition::IcePellets);
        } else if s == "GR" {
            conditions.push(WeatherCondition::Hail);
        } else if s == "GS" {
            conditions.push(WeatherCondition::SnowPelletsOrSmallHail);
        } else if s == "UP" {
            conditions.push(WeatherCondition::UnknownPrecipitation);
        } else if s == "FG" {
            conditions.push(WeatherCondition::Fog);
        } else if s == "VA" {
            conditions.push(WeatherCondition::VolcanicAsh);
        } else if s == "BR" {
            conditions.push(WeatherCondition::Mist);
        } else if s == "HZ" {
            conditions.push(WeatherCondition::Haze);
        } else if s == "DU" {
            conditions.push(WeatherCondition::WidespreadDust);
        } else if s == "FU" {
            conditions.push(WeatherCondition::Smoke);
        } else if s == "SA" {
            conditions.push(WeatherCondition::Sand);
        } else if s == "PY" {
            conditions.push(WeatherCondition::Spray);
        } else if s == "SQ" {
            conditions.push(WeatherCondition::Squall);
        } else if s == "PO" {
            conditions.push(WeatherCondition::Dust);
        } else if s == "DS" {
            conditions.push(WeatherCondition::Duststorm);
        } else if s == "SS" {
            conditions.push(WeatherCondition::Sandstorm);
        } else if s == "FC" {
            conditions.push(WeatherCondition::FunnelCloud);
        } else {
            return Err((i, 2, CloudVisibilityError::UnknownData));
        }

        i += 2;
    }

    if conditions.len() < 1 {
        // Need at least one weather condition
        return Err((0, s.len(), CloudVisibilityError::UnknownData));
    }

    let wx = Weather {
        intensity,
        conditions,
    };
    Ok(CloudVisibilityInfo::Weather(wx))
}

pub fn parse_temperatures<'a>(s: &'a str) -> ParserResult<(i32, i32), TemperatureError> {
    let chs: Vec<_> = s.chars().collect();

    if s.contains("///") {
        return Err((0, s.len(), TemperatureError::NotTemperatureDewpointPair));
    }
    if !s.contains('/') {
        return Err((0, s.len(), TemperatureError::NotTemperatureDewpointPair));
    }
    if s.contains('R') {
        // To protect against RVRs being interpreted as temperatures
        return Err((0, s.len(), TemperatureError::NotTemperatureDewpointPair));
    }

    let temp;
    let dewp;

    let mut i = 0;
    if chs[i] == 'M' {
        if !chs[i + 1].is_digit(10) {
            return Err((i + 1, 1, TemperatureError::TemperatureNotValid));
        }
        if !chs[i + 2].is_digit(10) {
            return Err((i + 2, 1, TemperatureError::TemperatureNotValid));
        }
        temp = -1 * s[i + 1 .. i + 3].parse::<i32>().unwrap();
        i += 4;
    } else {
        if !chs[i].is_digit(10) {
            return Err((i, 1, TemperatureError::TemperatureNotValid));
        }
        if !chs[i + 1].is_digit(10) {
            return Err((i + 1, 1, TemperatureError::TemperatureNotValid));
        }
        temp = s[i .. i + 2].parse().unwrap();
        i += 3;
    }

    if chs[i] == 'M' {
        if !chs[i + 1].is_digit(10) {
            return Err((i + 1, 1, TemperatureError::DewpointNotValid));
        }
        if !chs[i + 2].is_digit(10) {
            return Err((i + 2, 1, TemperatureError::DewpointNotValid));
        }
        dewp = -1 * s[i + 1 .. i + 3].parse::<i32>().unwrap();
    } else {
        if !chs[i].is_digit(10) {
            return Err((i, 1, TemperatureError::DewpointNotValid));
        }
        if !chs[i + 1].is_digit(10) {
            return Err((i + 1, 1, TemperatureError::DewpointNotValid));
        }
        dewp = s[i .. i + 2].parse().unwrap();
    }

    Ok((temp, dewp))
}

pub fn parse_pressure<'a>(s: &'a str) -> ParserResult<Pressure, PressureError> {
    let chs: Vec<_> = s.chars().collect();

    if !chs[1].is_digit(10) {
        return Err((1, 1, PressureError::PressureNotValid));
    }
    if !chs[2].is_digit(10) {
        return Err((2, 1, PressureError::PressureNotValid));
    }
    if !chs[3].is_digit(10) {
        return Err((3, 1, PressureError::PressureNotValid));
    }
    if !chs[4].is_digit(10) {
        return Err((4, 1, PressureError::PressureNotValid));
    }

    let pressure = s[1..5].parse().unwrap();

    if chs[0] == 'Q' {
        return Ok(Pressure::Hectopascals(pressure));
    } else if chs[0] == 'A' {
        return Ok(Pressure::InchesMercury(pressure));
    } else {
        return Err((0, 1, PressureError::UnitNotValid));
    }
}