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
//! Deutsche Bahn is awesome, but often trains are late.
//! There can be many reasons, some of which this library enumerates.
//! Use the function [`get_grund`] to get a random delay reason.
//!
//! This library exists because I was waiting for a train.

use thiserror::Error;

use enum_index_derive::{IndexEnum};
use variant_count::VariantCount;

mod random_grund;

/// Get a random delay reason.
pub fn get_grund() -> Grund {
    rand::random()
}

/// Some of the possible reasons a Deutsche Bahn train could be delayed.
#[non_exhaustive]
#[derive(Debug, Error, VariantCount, IndexEnum)]
pub enum Grund {
    /// Trip cancelled.
    #[error("Fahrt fällt aus")]
    FahrtFaelltAus,

    /// Trip only reaches a certain stop today.
    #[error("Fährt heute nur bis {0}")]
    FaehrtHeuteNurBis(String),

    /// Delays in operations.
    #[error("Verzögerungen im Betriebsablauf")]
    VerzoegerungenImBetriebsablauf,

    /// Change of track.
    #[error("Gleiswechsel")]
    Gleiswechsel,

    /// Trip cancelled, there is replacement trip.
    #[error("Fahrt fällt aus, es verkehrt Ersatzfahrt {0}")]
    FahrtFaelltAusMitErsatzfahrt(String),

    /// Technical difficulties with the train.
    #[error("Technische Störungen am Zug")]
    TechnischeStoerungenAmZug,

    /// Delay of a previous train.
    #[error("Verspätung eines vorausfahrenden Zuges")]
    VerspaetungEinesVorausfahrendenZuges,

    /// Delayed allocation of the train.
    #[error("Verspätete Bereitstellung des Zuges")]
    VerspaeteteBereitstellungDesZuges,

    /// Construction work.
    #[error("Bauarbeiten")]
    Bauarbeiten,

    /// Weather-related difficulties.
    #[error("Witterungsbedingte Störung")]
    WitterungsbedingteStoerung,

    /// Difficulties with a switch.
    #[error("Weichenstörung")]
    WeichenStoerung,

    /// Changes in the journey course.
    #[error("Änderung im Fahrtverlauf")]
    AenderungImFahrtverlauf,

    /// Storm or bad weather.
    #[error("Unwetter")]
    Unwetter,
}

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

    #[test]
    fn bauarbeiten() {
        let formatted = format!("Grund: {}", Grund::Bauarbeiten);
        assert_eq!("Grund: Bauarbeiten", formatted);
    }

    #[test]
    fn get_random_grund() {
        println!("Grund: {}", get_grund());
    }
}