diff --git a/src/css.rs b/src/css.rs index 87247ab..68e3725 100644 --- a/src/css.rs +++ b/src/css.rs @@ -115,7 +115,7 @@ impl Css { } fn parse_stylesheet(mut self, s: StyleSheet) -> Css { - for stmt in s.rules { + for stmt in s.0 { self.parse_stmt(stmt); } self diff --git a/src/format.rs b/src/format.rs index 7090227..568f3eb 100644 --- a/src/format.rs +++ b/src/format.rs @@ -41,7 +41,7 @@ impl PrettyPrinter { /// The result should be an exact copy of the SCSS input /// Empty rules are included pub fn pretty_print(&mut self, s: &StyleSheet) -> io::Result<()> { - for rule in &s.rules { + for rule in &s.0 { self.pretty_print_stmt(rule)?; } Ok(()) @@ -85,7 +85,7 @@ impl PrettyPrinter { &mut self, s: &StyleSheet, ) -> io::Result<()> { - for rule in &s.rules { + for rule in &s.0 { self.pretty_print_stmt_preserve_super_selectors(rule)?; } writeln!(self.buf)?; diff --git a/src/main.rs b/src/main.rs index 773245c..e1319aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -122,9 +122,7 @@ impl Display for TokenKind { /// Represents a parsed SASS stylesheet with nesting #[derive(Debug, Clone, Eq, PartialEq)] -pub struct StyleSheet { - rules: Vec, -} +pub struct StyleSheet(Vec); #[derive(Clone, Debug, Eq, PartialEq)] pub enum Stmt { @@ -178,25 +176,44 @@ enum Expr { impl StyleSheet { pub fn new(input: &str) -> SassResult { - StyleSheetParser { - global_scope: Scope::new(), - lexer: Lexer::new(input).peekable(), - rules: Vec::new(), - scope: 0, - file: String::from("stdin"), - } - .parse_toplevel() + Ok(StyleSheet( + StyleSheetParser { + global_scope: Scope::new(), + lexer: Lexer::new(input).peekable(), + rules: Vec::new(), + scope: 0, + file: String::from("stdin"), + } + .parse_toplevel()? + .0, + )) } pub fn from_path + Into>(p: P) -> SassResult { - StyleSheetParser { + Ok(StyleSheet( + StyleSheetParser { + global_scope: Scope::new(), + lexer: Lexer::new(&fs::read_to_string(p.as_ref())?).peekable(), + rules: Vec::new(), + scope: 0, + file: p.into(), + } + .parse_toplevel()? + .0, + )) + } + + pub(crate) fn export_from_path + Into>( + p: P, + ) -> SassResult<(Vec, Scope)> { + Ok(StyleSheetParser { global_scope: Scope::new(), lexer: Lexer::new(&fs::read_to_string(p.as_ref())?).peekable(), rules: Vec::new(), scope: 0, file: p.into(), } - .parse_toplevel() + .parse_toplevel()?) } /// Print the internal representation of a parsed stylesheet @@ -229,7 +246,7 @@ struct StyleSheetParser<'a> { } impl<'a> StyleSheetParser<'a> { - fn parse_toplevel(mut self) -> SassResult { + fn parse_toplevel(mut self) -> SassResult<(Vec, Scope)> { let mut rules: Vec = Vec::new(); while let Some(Token { kind, .. }) = self.lexer.peek() { match kind.clone() { @@ -296,7 +313,7 @@ impl<'a> StyleSheetParser<'a> { } }; } - Ok(StyleSheet { rules }) + Ok((rules, self.global_scope)) } fn eat_rules(&mut self, super_selector: &Selector, scope: &mut Scope) -> Vec {