From dafd1f3e0d1387255a596d9e4641ad9815e267ae Mon Sep 17 00:00:00 2001 From: ConnorSkees <39542938+ConnorSkees@users.noreply.github.com> Date: Fri, 14 Feb 2020 14:55:21 -0500 Subject: [PATCH] Refactor internal printing of StyleSheet --- src/error.rs | 9 ++++++++ src/format.rs | 63 ++++++--------------------------------------------- src/lib.rs | 31 +++++++++++-------------- 3 files changed, 29 insertions(+), 74 deletions(-) diff --git a/src/error.rs b/src/error.rs index f97876e..ec37bcd 100644 --- a/src/error.rs +++ b/src/error.rs @@ -34,6 +34,15 @@ impl From for SassError { } } +impl From for SassError { + fn from(error: std::fmt::Error) -> Self { + SassError { + pos: Pos::new(), + message: format!("{}", error), + } + } +} + impl From for SassError { fn from(error: FromUtf8Error) -> Self { SassError { diff --git a/src/format.rs b/src/format.rs index a3e3b6a..5c42d88 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,4 +1,4 @@ -use std::io::Write; +use std::fmt::Write; use crate::{RuleSet, SassResult, Stmt, StyleSheet}; @@ -49,49 +49,6 @@ impl PrettyPrinter { } Ok(()) } - - /// Pretty print `crate::Stmt` - /// Keeps super selectors - fn pretty_print_stmt_preserve_super_selectors(&mut self, stmt: &Stmt) -> SassResult<()> { - let padding = vec![' '; self.scope * 2].iter().collect::(); - match stmt { - Stmt::MultilineComment(s) => writeln!(self.buf, "/*{}*/", s)?, - Stmt::RuleSet(RuleSet { - selector, - rules, - super_selector, - }) => { - writeln!(self.buf, "{}{} {{", padding, super_selector.zip(selector))?; - self.scope += 1; - for rule in rules { - self.pretty_print_stmt(rule)?; - } - writeln!(self.buf, "{}}}", padding)?; - self.scope -= 1; - } - Stmt::Style(s) => { - writeln!(self.buf, "{}{}", padding, s)?; - } - Stmt::AtRule(r) => { - writeln!(self.buf, "{}{}", padding, r)?; - } - } - Ok(()) - } - - /// Pretty print a special form of SCSS that shows what the full selectors are for children - /// Meant for debugging - /// Empty rules are included - pub(crate) fn pretty_print_preserve_super_selectors( - &mut self, - s: &StyleSheet, - ) -> SassResult<()> { - for rule in &s.0 { - self.pretty_print_stmt_preserve_super_selectors(rule)?; - } - writeln!(self.buf)?; - Ok(()) - } } #[cfg(test)] @@ -101,28 +58,22 @@ mod test_scss { ($func:ident, $input:literal) => { #[test] fn $func() { - let mut buf = Vec::new(); - StyleSheet::new($input) - .expect(concat!("failed to parse on ", $input)) - .pretty_print(&mut buf) - .expect(concat!("failed to pretty print on ", $input)); assert_eq!( String::from($input), - String::from_utf8(buf).expect("produced invalid utf8") + StyleSheet::new($input) + .expect(concat!("failed to parse on ", $input)) + .to_string() ); } }; ($func:ident, $input:literal, $output:literal) => { #[test] fn $func() { - let mut buf = Vec::new(); - StyleSheet::new($input) - .expect(concat!("failed to parse on ", $input)) - .pretty_print(&mut buf) - .expect(concat!("failed to pretty print on ", $input)); assert_eq!( String::from($output), - String::from_utf8(buf).expect("produced invalid utf8") + StyleSheet::new($input) + .expect(concat!("failed to parse on ", $input)) + .to_string() ); } }; diff --git a/src/lib.rs b/src/lib.rs index a402a5d..39ed23a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -222,6 +222,19 @@ enum Expr { // FuncCall(String, Vec), } +/// Print the internal representation of a parsed stylesheet +/// +/// Very closely resembles the original SASS, but contains only things translatable +/// to pure CSS: functions, variables, values, and mixins have all been evaluated. +/// +/// Use `StyleSheet::print_as_css` to properly convert to CSS. +impl Display for StyleSheet { + #[inline] + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + Ok(PrettyPrinter::new(f).pretty_print(self).unwrap()) + } +} + impl StyleSheet { #[inline] pub fn new(input: &str) -> SassResult { @@ -266,24 +279,6 @@ impl StyleSheet { .parse_toplevel()?) } - /// Print the internal representation of a parsed stylesheet - /// - /// Very closely resembles the origin SASS, but contains only things translatable - /// to pure CSS - /// - /// Used mainly in debugging, but can at times be useful - #[inline] - #[allow(dead_code)] - pub fn pretty_print(&self, buf: W) -> SassResult<()> { - PrettyPrinter::new(buf).pretty_print(self) - } - - #[inline] - #[allow(dead_code)] - fn pretty_print_selectors(&self, buf: W) -> SassResult<()> { - PrettyPrinter::new(buf).pretty_print_preserve_super_selectors(self) - } - /// Write the internal representation as CSS to `buf` /// /// ```