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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! Log related logic.
//!
//! The logic contained within this file relates to using a log id to extract and parse the log's
//! information from the elf.

use core::fmt;

use serde::Deserialize;
use serde_repr::Deserialize_repr;

use crate::{
    format::{FormatStringFragment, FormatStringFragmentIterator, ParameterPosition},
    var::Var,
};

#[derive(Clone, Copy, Debug, Deserialize_repr)]
#[repr(u8)]
pub enum Level {
    Error,
    Warning,
    Info,
    Debug,
    Verbose,
}

impl fmt::Display for Level {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad(&format!("{:?}", self))
    }
}

#[derive(Clone, Debug, Deserialize)]
pub(crate) struct Schema {
    pub schema: u32,
}

#[derive(Clone, Debug, Deserialize)]
pub(crate) struct MetadataV1 {
    #[serde(skip)]
    pub id: usize,
    pub counter: usize,
    pub level: Level,
    pub file: String,
    pub line: usize,
    #[serde(rename = "message")]
    pub format_string: String,
}

#[derive(Clone, Debug)]
pub struct Log {
    metadata: MetadataV1,
    args: Vec<Var>,
}

impl Log {
    pub(crate) fn new(metadata: MetadataV1, args: Vec<Var>) -> Self {
        Self { metadata, args }
    }

    pub fn get_level(&self) -> Level {
        self.metadata.level
    }

    pub fn get_file(&self) -> &str {
        &self.metadata.file
    }

    pub fn get_line(&self) -> usize {
        self.metadata.line
    }

    pub fn get_args(&self) -> &[Var] {
        &self.args
    }

    fn get_format_fragments(&self) -> FormatStringFragmentIterator {
        FormatStringFragmentIterator::new(&self.metadata.format_string)
    }
}

impl std::fmt::Display for Log {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut index = 0;

        for fragment in self.get_format_fragments() {
            match fragment {
                FormatStringFragment::Parameter(parameter) => {
                    if !self.args.is_empty() {
                        let var = match parameter.position {
                            Some(ParameterPosition::Positional(position)) => {
                                &self.args[position % self.args.len()]
                            }
                            Some(ParameterPosition::Named(_)) => todo!(),
                            None => {
                                let res = &self.args[index];
                                index += 1;
                                index %= self.args.len();
                                res
                            }
                        };
                        write!(f, "{}", var.format(&parameter.hint))?
                    }
                }
                FormatStringFragment::Error(literal, error) => {
                    write!(f, "{} ({})", literal, error)?
                }
                FormatStringFragment::Escaped(character) => write!(f, "{}", character)?,
                FormatStringFragment::Literal(literal) => write!(f, "{}", literal)?,
            }
        }

        Ok(())
    }
}