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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
use core::{convert::Infallible, fmt::Debug};

use embedded_io::{Error, ErrorType, Write};
use ufmt::uWrite;

use crate::codes;

pub struct Writer<'a, W: Write<Error = E>, E: Error> {
    last_bytes: [u8; 2],
    dirty: bool,
    writer: &'a mut W,
}

impl<'a, W: Write<Error = E>, E: Error> Debug for Writer<'a, W, E> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        f.debug_struct("Writer")
            .field("last_bytes", &self.last_bytes)
            .field("dirty", &self.dirty)
            .finish()
    }
}

impl<'a, W: Write<Error = E>, E: Error> Writer<'a, W, E> {
    pub fn new(writer: &'a mut W) -> Self {
        Self {
            last_bytes: [0; 2],
            dirty: false,
            writer,
        }
    }

    pub(crate) fn is_dirty(&self) -> bool {
        self.dirty
            && (self.last_bytes[0] != codes::CARRIAGE_RETURN
                || self.last_bytes[1] != codes::LINE_FEED)
    }

    pub fn write_str(&mut self, mut text: &str) -> Result<(), E> {
        while !text.is_empty() {
            if let Some(pos) = text.as_bytes().iter().position(|&b| b == codes::LINE_FEED) {
                // SAFETY: pos is inside text slice
                let line = unsafe { text.get_unchecked(..pos) };

                self.writer.write_str(line)?;
                self.writer.write_str(codes::CRLF)?;
                // SAFETY: pos is index of existing element so pos + 1 in worst case will be
                // outside of slice by 1, which is safe (will give empty slice as result)
                text = unsafe { text.get_unchecked(pos + 1..) };
                self.dirty = false;
                self.last_bytes = [0; 2];
            } else {
                self.writer.write_str(text)?;
                self.dirty = true;

                if text.len() > 1 {
                    self.last_bytes[0] = text.as_bytes()[text.len() - 2];
                    self.last_bytes[1] = text.as_bytes()[text.len() - 1];
                } else {
                    self.last_bytes[0] = self.last_bytes[1];
                    self.last_bytes[1] = text.as_bytes()[text.len() - 1];
                }
                break;
            }
        }
        Ok(())
    }

    pub fn writeln_str(&mut self, text: &str) -> Result<(), E> {
        self.writer.write_str(text)?;
        self.writer.write_str(codes::CRLF)?;
        self.dirty = false;
        Ok(())
    }

    pub fn write_list_element(
        &mut self,
        name: &str,
        description: &str,
        longest_name: usize,
    ) -> Result<(), E> {
        self.write_str("  ")?;
        self.write_str(name)?;
        if name.len() < longest_name {
            for _ in 0..longest_name - name.len() {
                self.write_str(" ")?;
            }
        }
        self.write_str("  ")?;
        self.writeln_str(description)?;

        Ok(())
    }

    pub fn write_title(&mut self, title: &str) -> Result<(), E> {
        //TODO: add formatting
        self.write_str(title)?;
        Ok(())
    }
}

impl<'a, W: Write<Error = E>, E: Error> uWrite for Writer<'a, W, E> {
    type Error = E;

    fn write_str(&mut self, s: &str) -> Result<(), E> {
        self.write_str(s)
    }
}

impl<'a, W: Write<Error = E>, E: Error> core::fmt::Write for Writer<'a, W, E> {
    fn write_str(&mut self, s: &str) -> core::fmt::Result {
        self.write_str(s).map_err(|_| core::fmt::Error)?;
        Ok(())
    }
}

pub(crate) trait WriteExt: ErrorType {
    /// Write and flush all given bytes
    fn flush_bytes(&mut self, bytes: &[u8]) -> Result<(), Self::Error>;

    fn flush_str(&mut self, text: &str) -> Result<(), Self::Error>;

    fn write_str(&mut self, text: &str) -> Result<(), Self::Error>;
}

impl<W: Write> WriteExt for W {
    fn flush_bytes(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
        self.write_all(bytes)?;
        self.flush()
    }

    fn flush_str(&mut self, text: &str) -> Result<(), Self::Error> {
        self.flush_bytes(text.as_bytes())
    }

    fn write_str(&mut self, text: &str) -> Result<(), Self::Error> {
        self.write_all(text.as_bytes())
    }
}

#[derive(Debug)]
pub struct EmptyWriter;

impl ErrorType for EmptyWriter {
    type Error = Infallible;
}

impl Write for EmptyWriter {
    fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
        Ok(buf.len())
    }

    fn flush(&mut self) -> Result<(), Self::Error> {
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use crate::writer::{EmptyWriter, Writer};

    #[test]
    fn detect_dirty() {
        let mut writer = EmptyWriter;
        let mut writer = Writer::new(&mut writer);

        assert!(!writer.is_dirty());

        writer.write_str("abc").unwrap();
        assert!(writer.is_dirty());

        writer.write_str("\r").unwrap();
        assert!(writer.is_dirty());

        writer.write_str("\n").unwrap();
        assert!(!writer.is_dirty());

        writer.write_str("abc\r\n").unwrap();
        assert!(!writer.is_dirty());
    }
}