omg_html/
data.rs

1// SPDX-FileCopyrightText: 2022 Declan Rixon <declan.fraser.rixon@gmail.com>
2//
3// SPDX-License-Identifier: GPL-3.0-only
4
5use crate::*;
6use std::fmt::{self, Display, Formatter};
7
8#[derive(Clone)]
9pub enum Data {
10    Rendered(Vec<u8>),
11    Placeholder { indent: Option<usize>, id: ID },
12    LineStart,
13}
14
15impl Display for Data {
16    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
17        use Data::*;
18        match self {
19            Rendered(bytes) => f.write_str(&String::from_utf8_lossy(bytes)),
20            Placeholder { indent, id } => write!(
21                f,
22                "{}{{ {} }}",
23                String::from_utf8_lossy(&INDENTATION.repeat(indent.unwrap_or(0))),
24                id
25            ),
26            LineStart => f.write_str("\n"),
27        }
28    }
29}
30
31impl<T> From<T> for Data
32where
33    Vec<u8>: From<T>,
34{
35    fn from(t: T) -> Data {
36        Data::Rendered(t.into())
37    }
38}