summaryrefslogtreecommitdiffstats
path: root/src/log/item.rs
blob: fc2af806e7ed6784b3dde39fb44ff2a4261d0656 (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
use anyhow::Result;

#[derive(Debug, PartialEq, Eq, Hash)]
pub enum LogItem {
    /// A line from the log, unmodified
    Line(Vec<u8>),

    /// A progress report
    Progress(usize),

    /// The name of the current phase the process is in
    CurrentPhase(String),

    /// The end-state of the process
    /// Either Ok or Error
    State(Result<(), String>),
}

impl LogItem {
    pub fn display(&self) -> Result<Display> {
        match self {
            LogItem::Line(s)         => Ok(Display(String::from_utf8(s.to_vec())?)),
            LogItem::Progress(u)     => Ok(Display(format!("#BUTIDO:PROGRESS:{}", u))),
            LogItem::CurrentPhase(p) => Ok(Display(format!("#BUTIDO:PHASE:{}", p))),
            LogItem::State(Ok(()))   => Ok(Display(format!("#BUTIDO:STATE:OK"))),
            LogItem::State(Err(s))   => Ok(Display(format!("#BUTIDO:STATE:ERR:{}", s))),
        }
    }
}

pub struct Display(String);

impl std::fmt::Display for Display {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        write!(f, "{}", self.0)
    }
}