grass/src/format.rs

56 lines
1.6 KiB
Rust
Raw Normal View History

use std::fmt::Write;
use crate::atrule::AtRule;
use crate::error::SassResult;
use crate::{RuleSet, Stmt, StyleSheet};
pub(crate) struct PrettyPrinter<W: Write> {
buf: W,
scope: usize,
}
impl<W: Write> PrettyPrinter<W> {
pub(crate) fn new(buf: W) -> Self {
PrettyPrinter { buf, scope: 0 }
}
2020-01-05 12:35:37 -05:00
/// Pretty print `crate::Stmt`
2020-01-05 19:09:46 -05:00
/// Throws away super selectors and variables
fn pretty_print_stmt(&mut self, stmt: &Stmt) -> SassResult<()> {
let padding = vec![' '; self.scope * 2].iter().collect::<String>();
match stmt {
Stmt::MultilineComment(s) => writeln!(self.buf, "{}/*{}*/", padding, s)?,
Stmt::RuleSet(RuleSet {
selector, rules, ..
}) => {
writeln!(self.buf, "{}{} {{", padding, selector)?;
self.scope += 1;
for rule in rules {
self.pretty_print_stmt(rule)?;
}
writeln!(self.buf, "{}}}", padding)?;
self.scope -= 1;
}
Stmt::Style(s) => {
2020-04-12 19:37:12 -04:00
writeln!(self.buf, "{}{}", padding, s.to_string()?)?;
}
Stmt::AtRule(r) => match r {
AtRule::Unknown(..) => todo!("Display @rules properly"),
_ => todo!(),
},
}
Ok(())
}
2020-01-05 12:35:37 -05:00
/// Pretty print SCSS
///
/// The result should be an exact copy of the SCSS input
/// Empty rules are included
pub fn pretty_print(&mut self, s: &StyleSheet) -> SassResult<()> {
2020-01-18 14:57:56 -05:00
for rule in &s.0 {
self.pretty_print_stmt(rule)?;
}
Ok(())
}
}