inline_css/
fmt.rs

1// Copyright (C) 2023 Benjamin Stürz
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program.  If not, see <http://www.gnu.org/licenses/>.
15use std::fmt::{self, Display, Formatter};
16use super::*;
17
18impl Display for CSS {
19    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
20        for rule in &self.0 {
21            writeln!(f, "{rule}")?;
22        }
23        Ok(())
24    }
25}
26
27impl Display for Rule {
28    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
29        writeln!(f, "{} {{", self.selector)?;
30        for decl in &self.declarations {
31            writeln!(f, "\t{decl}")?;
32        }
33        write!(f, "}}")
34    }
35}
36
37impl Display for Selector {
38    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
39        match self {
40            Self::Any => write!(f, "*"),
41            Self::Simple { ty, class, id } => {
42                if let Some(ty) = ty {
43                    write!(f, "{ty}")?;
44                }
45                if let Some(class) = class {
46                    write!(f, ".{class}")?;
47                }
48                if let Some(id) = id {
49                    write!(f, "#{id}")?;
50                }
51                Ok(())
52            },
53            Self::Colon { name, arg: Some(arg) } => write!(f, ":{name}({arg})"),
54            Self::Colon { name, arg: None } => write!(f, ":{name}"),
55            Self::Sub { left, right } if matches!(right.as_ref(), Self::Colon { .. }) => write!(f, "{left}{right}"),
56            Self::Sub { left, right } => write!(f, "{left} {right}"),
57            Self::Gt { left, right } => write!(f, "{left} > {right}"),
58            Self::Plus { left, right } => write!(f, "{left} + {right}"),
59            Self::Tilde { left, right } => write!(f, "{left} ~ {right}"),
60            Self::Comma { left, right } => write!(f, "{left}, {right}"),
61        }
62    }
63}
64
65impl Display for Declaration {
66    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
67        write!(f, "{}:", self.name)?;
68        for v in &self.value {
69            write!(f, " {v}")?;
70        }
71        write!(f, ";")
72    }
73}
74
75impl Display for Value {
76    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
77        match self {
78            Self::ColorCode(cc) => write!(f, "#{cc:06x}"),
79            Self::Ident(i)      => write!(f, "{i}"),
80            Self::Int(i, s)     => write!(f,"{i}{s}"),
81            Self::Float(x, s)   => write!(f,"{x}{s}"),
82            Self::Function { name, args } => {
83                let mut args = args.iter();
84                write!(f, "{name}(")?;
85                if let Some(first) = args.next() {
86                    write!(f, "{first}")?;
87                    for arg in args {
88                        write!(f, ", {arg}")?;
89                    }
90                }
91                write!(f, ")")
92            }
93        }
94    }
95}