aboutsummaryrefslogtreecommitdiff
path: root/src/message.rs
blob: 958dd6820da83de0093efe36c98c666493491bf2 (plain)
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
use crate::consts::colors::*;

use sdl2::pixels::Color;

#[derive(Debug)]
pub struct Message {
    pub text: String,
    pub kind: MessageKind,
}

impl Message {
    pub fn new() -> Self {
        Self {
            text: String::new(),
            kind: MessageKind::Info,
        }
    }

    pub fn kind(mut self, kind: MessageKind) -> Self {
        self.kind = kind;
        self
    }

    pub fn text<S: AsRef<str>>(mut self, text: S) -> Self {
        self.text = text.as_ref().into();
        self
    }

    pub fn set_info<S: AsRef<str>>(&mut self, text: S) {
        self.text = text.as_ref().into();
        self.kind = MessageKind::Info;
    }

    pub fn set_error<S: AsRef<str>>(&mut self, text: S) {
        self.text = text.as_ref().into();
        self.kind = MessageKind::Error;
    }

    pub fn set_hint<S: AsRef<str>>(&mut self, text: S) {
        self.text = text.as_ref().into();
        self.kind = MessageKind::Hint;
    }

    pub fn clear(&mut self) {
        self.text.clear();
        self.kind = MessageKind::Info;
    }
}

#[derive(Debug, Copy, Clone)]
pub enum MessageKind {
    Error,
    Info,
    Hint,
    LispResult,
}

impl<T> From<T> for Message
where
    T: AsRef<str>,
{
    fn from(item: T) -> Self {
        return Message {
            text: item.as_ref().into(),
            kind: MessageKind::Info,
        };
    }
}

impl Into<Color> for MessageKind {
    fn into(self) -> Color {
        match self {
            Self::Error => PINK,
            Self::Info => WHITE,
            Self::Hint => CYAN,
            Self::LispResult => GREY,
        }
    }
}