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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
//! SVG parsing logic.

use std::{
    borrow::Cow,
    error::Error as StdError,
    fmt,
    io::{self, BufRead},
    mem,
    num::ParseIntError,
    str,
};

use quick_xml::{
    events::{attributes::Attributes, Event},
    Reader as XmlReader,
};
use termcolor::WriteColor;

#[cfg(test)]
mod tests;
mod text;

use self::text::TextReadingState;
use crate::{
    test::color_diff::ColorSpan, ExitStatus, Interaction, TermOutput, Transcript, UserInput,
};

/// Parsed terminal output.
#[derive(Debug, Clone, Default)]
pub struct Parsed {
    pub(crate) plaintext: String,
    pub(crate) color_spans: Vec<ColorSpan>,
    pub(crate) html: String,
}

impl Parsed {
    const DEFAULT: Self = Self {
        plaintext: String::new(),
        color_spans: Vec::new(),
        html: String::new(),
    };

    /// Returns the parsed plaintext.
    pub fn plaintext(&self) -> &str {
        &self.plaintext
    }

    /// Writes the parsed text with coloring / styles applied.
    ///
    /// # Errors
    ///
    /// - Returns an I/O error should it occur when writing to `out`.
    #[doc(hidden)] // makes `termcolor` dependency public, which we want to avoid so far
    pub fn write_colorized(&self, out: &mut impl WriteColor) -> io::Result<()> {
        ColorSpan::write_colorized(&self.color_spans, out, &self.plaintext)
    }

    /// Returns the parsed HTML.
    pub fn html(&self) -> &str {
        &self.html
    }

    /// Converts this parsed fragment into text for `UserInput`. This takes into account
    /// that while the first space after prompt is inserted automatically, the further whitespace
    /// may be significant.
    fn into_input_text(self) -> String {
        if self.plaintext.starts_with(' ') {
            self.plaintext[1..].to_owned()
        } else {
            self.plaintext
        }
    }
}

impl TermOutput for Parsed {}

impl Transcript<Parsed> {
    /// Parses a transcript from the provided `reader`, which should point to an SVG XML tree
    /// produced by [`Template::render()`] (possibly within a larger document).
    ///
    /// # Errors
    ///
    /// - Returns an error if the input cannot be parsed, usually because it was not produced
    ///   by `Template::render()`.
    ///
    /// [`Template::render()`]: crate::svg::Template::render()
    #[cfg_attr(feature = "tracing", tracing::instrument(skip_all, err))]
    pub fn from_svg<R: BufRead>(reader: R) -> Result<Self, ParseError> {
        let mut reader = XmlReader::from_reader(reader);
        let mut buffer = vec![];
        let mut state = ParserState::Initialized;
        let mut transcript = Self::new();
        let mut open_tags = 0;

        loop {
            let event = reader.read_event_into(&mut buffer)?;
            match &event {
                Event::Start(_) => {
                    open_tags += 1;
                }
                Event::End(_) => {
                    open_tags -= 1;
                    if open_tags == 0 {
                        break;
                    }
                }
                Event::Eof => break,
                _ => { /* Do nothing. */ }
            }

            if let Some(interaction) = state.process(event)? {
                #[cfg(feature = "tracing")]
                tracing::debug!(
                    ?interaction.input,
                    interaction.output = ?interaction.output.plaintext,
                    ?interaction.exit_status,
                    "parsed interaction"
                );
                transcript.interactions.push(interaction);
            }
        }

        match state {
            ParserState::EncounteredContainer => Ok(transcript),
            ParserState::EncounteredUserInput(interaction) => {
                transcript.interactions.push(interaction);
                Ok(transcript)
            }
            _ => Err(ParseError::UnexpectedEof),
        }
    }
}

fn parse_classes(attributes: Attributes<'_>) -> Result<Cow<'_, [u8]>, ParseError> {
    let mut class = None;
    for attr in attributes {
        let attr = attr.map_err(quick_xml::Error::InvalidAttr)?;
        if attr.key.as_ref() == b"class" {
            class = Some(attr.value);
        }
    }
    Ok(class.unwrap_or(Cow::Borrowed(b"")))
}

fn extract_base_class(classes: &[u8]) -> &[u8] {
    let space_idx = classes.iter().position(|&ch| ch == b' ');
    space_idx.map_or(classes.as_ref(), |idx| &classes[..idx])
}

fn parse_exit_status(attributes: Attributes<'_>) -> Result<Option<ExitStatus>, ParseError> {
    let mut exit_status = None;
    for attr in attributes {
        let attr = attr.map_err(quick_xml::Error::InvalidAttr)?;
        if attr.key.as_ref() == b"data-exit-status" {
            let status = str::from_utf8(&attr.value).map_err(|err| ParseError::Xml(err.into()))?;
            let status = status.parse().map_err(ParseError::InvalidExitStatus)?;
            exit_status = Some(ExitStatus(status));
        }
    }
    Ok(exit_status)
}

/// Errors that can occur during parsing SVG transcripts.
#[derive(Debug)]
#[non_exhaustive]
pub enum ParseError {
    /// Unexpected root XML tag; must be `<svg>`.
    UnexpectedRoot(String),
    /// Invalid transcript container.
    InvalidContainer,
    /// Invalid recorded exit status of an executed command.
    InvalidExitStatus(ParseIntError),
    /// Unexpected end of file.
    UnexpectedEof,
    /// Error parsing XML.
    Xml(quick_xml::Error),
}

impl From<quick_xml::Error> for ParseError {
    fn from(err: quick_xml::Error) -> Self {
        Self::Xml(err)
    }
}

impl From<io::Error> for ParseError {
    fn from(err: io::Error) -> Self {
        Self::Xml(err.into())
    }
}

impl fmt::Display for ParseError {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::UnexpectedRoot(tag_name) => write!(
                formatter,
                "unexpected root XML tag: <{tag_name}>; expected <svg>"
            ),
            Self::InvalidContainer => formatter.write_str("invalid transcript container"),
            Self::InvalidExitStatus(err) => write!(formatter, "invalid exit status: {err}"),
            Self::UnexpectedEof => formatter.write_str("unexpected EOF"),
            Self::Xml(err) => write!(formatter, "error parsing XML: {err}"),
        }
    }
}

impl StdError for ParseError {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match self {
            Self::Xml(err) => Some(err),
            Self::InvalidExitStatus(err) => Some(err),
            _ => None,
        }
    }
}

#[derive(Debug)]
struct UserInputState {
    exit_status: Option<ExitStatus>,
    is_hidden: bool,
    text: TextReadingState,
    prompt: Option<Cow<'static, str>>,
    prompt_open_tags: Option<usize>,
}

impl UserInputState {
    fn new(exit_status: Option<ExitStatus>, is_hidden: bool) -> Self {
        Self {
            exit_status,
            is_hidden,
            text: TextReadingState::default(),
            prompt: None,
            prompt_open_tags: None,
        }
    }
}

impl UserInputState {
    /// Can prompt reading be started now?
    fn can_start_prompt(&self) -> bool {
        self.text.is_empty() && self.prompt.is_none() && self.prompt_open_tags.is_none()
    }

    fn can_end_prompt(&self) -> bool {
        self.prompt.is_none()
            && self
                .prompt_open_tags
                .map_or(false, |tags| tags + 1 == self.text.open_tags())
    }

    fn process(&mut self, event: Event<'_>) -> Result<Option<Interaction<Parsed>>, ParseError> {
        let mut is_prompt_end = false;
        if let Event::Start(tag) = &event {
            if self.can_start_prompt() && parse_classes(tag.attributes())?.as_ref() == b"prompt" {
                // Got prompt start.
                self.prompt_open_tags = Some(self.text.open_tags());
            }
        } else if let Event::End(_) = &event {
            if self.can_end_prompt() {
                is_prompt_end = true;
            }
        }

        let maybe_parsed = self.text.process(event)?;
        if is_prompt_end {
            if let Some(parsed) = maybe_parsed {
                // Special case: user input consists of the prompt only.
                let input = UserInput {
                    text: String::new(),
                    prompt: Some(UserInput::intern_prompt(parsed.plaintext)),
                    hidden: self.is_hidden,
                };
                return Ok(Some(Interaction {
                    input,
                    output: Parsed::default(),
                    exit_status: self.exit_status,
                }));
            }
            let text = mem::take(&mut self.text.plaintext_buffer);
            self.prompt = Some(UserInput::intern_prompt(text));
        }

        Ok(maybe_parsed.map(|parsed| {
            let input = UserInput {
                text: parsed.into_input_text(),
                prompt: self.prompt.take(),
                hidden: self.is_hidden,
            };
            Interaction {
                input,
                output: Parsed::default(),
                exit_status: self.exit_status,
            }
        }))
    }
}

/// States of the FSM for parsing SVGs.
#[derive(Debug)]
enum ParserState {
    /// Initial state.
    Initialized,
    /// Encountered `<svg>` tag; searching for `<div class="container">`.
    EncounteredSvgTag,
    /// Encountered `<div class="container">`; searching for `<div class="input">`.
    EncounteredContainer,
    /// Reading user input (`<div class="input">` contents).
    ReadingUserInput(UserInputState),
    /// Finished reading user input; searching for `<div class="output">`.
    EncounteredUserInput(Interaction<Parsed>),
    /// Reading terminal output (`<div class="output">` contents).
    ReadingTermOutput(Interaction<Parsed>, TextReadingState),
}

impl ParserState {
    const DUMMY_INTERACTION: Interaction<Parsed> = Interaction {
        input: UserInput {
            text: String::new(),
            prompt: None,
            hidden: false,
        },
        output: Parsed::DEFAULT,
        exit_status: None,
    };

    #[cfg_attr(feature = "tracing", tracing::instrument(level = "debug"))]
    fn set_state(&mut self, new_state: Self) {
        *self = new_state;
    }

    #[cfg_attr(feature = "tracing", tracing::instrument(level = "trace", err))]
    fn process(&mut self, event: Event<'_>) -> Result<Option<Interaction<Parsed>>, ParseError> {
        match self {
            Self::Initialized => {
                if let Event::Start(tag) = event {
                    if tag.name().as_ref() == b"svg" {
                        *self = Self::EncounteredSvgTag;
                    } else {
                        let tag_name = String::from_utf8_lossy(tag.name().as_ref()).into_owned();
                        return Err(ParseError::UnexpectedRoot(tag_name));
                    }
                }
            }

            Self::EncounteredSvgTag => {
                if let Event::Start(tag) = event {
                    if tag.name().as_ref() == b"div" {
                        Self::verify_container_attrs(tag.attributes())?;
                        self.set_state(Self::EncounteredContainer);
                    }
                }
            }

            Self::EncounteredContainer => {
                if let Event::Start(tag) = event {
                    let classes = parse_classes(tag.attributes())?;
                    if Self::is_input_class(extract_base_class(&classes)) {
                        let is_hidden = classes
                            .split(|byte| *byte == b' ')
                            .any(|chunk| chunk == b"input-hidden");
                        let exit_status = parse_exit_status(tag.attributes())?;
                        self.set_state(Self::ReadingUserInput(UserInputState::new(
                            exit_status,
                            is_hidden,
                        )));
                    }
                }
            }

            Self::ReadingUserInput(state) => {
                if let Some(interaction) = state.process(event)? {
                    self.set_state(Self::EncounteredUserInput(interaction));
                }
            }

            Self::EncounteredUserInput(interaction) => {
                if let Event::Start(tag) = event {
                    let classes = parse_classes(tag.attributes())?;
                    let base_class = extract_base_class(&classes);

                    if Self::is_output_class(base_class) {
                        let interaction = mem::replace(interaction, Self::DUMMY_INTERACTION);
                        self.set_state(Self::ReadingTermOutput(
                            interaction,
                            TextReadingState::default(),
                        ));
                    } else if Self::is_input_class(base_class) {
                        let interaction = mem::replace(interaction, Self::DUMMY_INTERACTION);
                        let exit_status = parse_exit_status(tag.attributes())?;
                        let is_hidden = classes
                            .split(|byte| *byte == b' ')
                            .any(|chunk| chunk == b"input-hidden");
                        self.set_state(Self::ReadingUserInput(UserInputState::new(
                            exit_status,
                            is_hidden,
                        )));
                        return Ok(Some(interaction));
                    }
                }
            }

            Self::ReadingTermOutput(interaction, text_state) => {
                if let Some(term_output) = text_state.process(event)? {
                    let mut interaction = mem::replace(interaction, Self::DUMMY_INTERACTION);
                    interaction.output = term_output;
                    self.set_state(Self::EncounteredContainer);
                    return Ok(Some(interaction));
                }
            }
        }
        Ok(None)
    }

    fn is_input_class(class_name: &[u8]) -> bool {
        class_name == b"input" || class_name == b"user-input"
    }

    fn is_output_class(class_name: &[u8]) -> bool {
        class_name == b"output" || class_name == b"term-output"
    }

    #[cfg_attr(
        feature = "tracing",
        tracing::instrument(level = "debug", skip_all, err)
    )]
    fn verify_container_attrs(attributes: Attributes<'_>) -> Result<(), ParseError> {
        const HTML_NS: &[u8] = b"http://www.w3.org/1999/xhtml";

        let mut has_ns_attribute = false;
        let mut has_class_attribute = false;

        for attr in attributes {
            let attr = attr.map_err(quick_xml::Error::InvalidAttr)?;
            match attr.key.as_ref() {
                b"xmlns" => {
                    if attr.value.as_ref() != HTML_NS {
                        return Err(ParseError::InvalidContainer);
                    }
                    has_ns_attribute = true;
                }
                b"class" => {
                    if attr.value.as_ref() != b"container" {
                        return Err(ParseError::InvalidContainer);
                    }
                    has_class_attribute = true;
                }
                _ => { /* Do nothing. */ }
            }
        }

        if has_ns_attribute && has_class_attribute {
            Ok(())
        } else {
            Err(ParseError::InvalidContainer)
        }
    }
}