aboutsummaryrefslogtreecommitdiff
path: root/src/error.rs
blob: 4d4f4ab2325fb0aeefe46c7310b24195676b3a96 (plain) (blame)
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
use std::{error::Error, fmt::Display};

use crate::app::{MIN_TERM_COL, MIN_TERM_ROW};

#[derive(Debug, Clone)]
pub struct WordTooLongError {
    word: String,
    max_length: u16,
}

impl WordTooLongError {
    pub fn new(word: impl Into<String>, max_length: u16) -> WordTooLongError {
        WordTooLongError {
            word: word.into(),
            max_length,
        }
    }
}

impl Display for WordTooLongError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!(
            "The word \"{}\" is too long for the current terminal size or longer than {} characters.",
            self.word,
            self.max_length,
        ))
    }
}

impl Error for WordTooLongError {}

#[derive(Debug)]
pub struct TerminalTooSmallError;

impl Display for TerminalTooSmallError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!(
            "The terminal size is too small. Min column count is {} and minimum row count is {}.",
            MIN_TERM_COL, MIN_TERM_ROW
        ))
    }
}

#[derive(Debug)]
pub enum TyperError {
    TerminalTooSmallError(TerminalTooSmallError),
    WordTooLongError(WordTooLongError),
}

impl Display for TyperError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let text = match self {
            TyperError::TerminalTooSmallError(e) => e.to_string(),
            TyperError::WordTooLongError(e) => e.to_string(),
        };
        f.write_str(&text)
    }
}
XMR address: 854DmXNrxULU3ZFJVs4Wc8PFhbq29RhqHhY8W6cdWrtFN3qmooKyyeYPcDzZTNRxphhJ5UzASQfAdEMwSteVqymk28aLhqj