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
use std::fmt;

pub enum Message {
    Raw(String),
    Info(String),
}

impl fmt::Display for Message {
    /// If a Message is of type Raw, it's inner str is not going
    /// to be altered when using the `format!` macro.
    ///
    /// # Example
    ///
    /// ```
    /// use slate::message::Message;
    ///
    /// let message = Message::Raw("don't touch me".to_string());
    /// print!("{}", message);
    /// //=> "don't touch me"
    /// ```
    ///
    /// In the case the message is of type Info, it will have
    /// an unique eol.
    ///
    /// # Example
    ///
    /// ```
    /// use slate::message::Message;
    ///
    /// let message = Message::Info("hello world".to_string());
    /// print!("{}", message);
    /// //=> "hello world\n"
    /// ```
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        match self {
            &Message::Raw(ref msg) => write!(formatter, "{}", msg),
            &Message::Info(ref msg) => write!(formatter, "{}\n", msg.trim_right()),
        }
    }
}

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

    #[test]
    fn it_does_not_touch_raw_messages() {
        let expected = "don't touch me".to_string();
        let subject = Message::Raw("don't touch me".to_string());
        let actual = format!("{}", subject);

        assert_eq!(expected, actual);
    }

    #[test]
    fn it_adds_eol_to_info_messages() {
        let expected = "you can touch me\n".to_string();
        let subject = Message::Info("you can touch me".to_string());
        let actual = format!("{}", subject);

        assert_eq!(expected, actual);
    }

    #[test]
    fn it_does_not_add_multiple_eol_to_info_messages() {
        let expected = "you can touch me\n".to_string();
        let subject = Message::Info("you can touch me\n".to_string());
        let actual = format!("{}", subject);

        assert_eq!(expected, actual);
    }
}