diff --git a/src/common.rs b/src/common.rs index 807e31a..361d3d8 100644 --- a/src/common.rs +++ b/src/common.rs @@ -476,7 +476,7 @@ impl Display for Pos { } #[derive(Debug, Clone, Default)] -pub struct Scope { +pub(crate) struct Scope { pub vars: HashMap>, pub mixins: HashMap, } diff --git a/src/css.rs b/src/css.rs index 4ba4e33..d520a1f 100644 --- a/src/css.rs +++ b/src/css.rs @@ -4,14 +4,14 @@ use std::fmt; use std::io::Write; #[derive(Debug, Clone)] -pub enum Toplevel { +enum Toplevel { RuleSet(Selector, Vec), MultilineComment(String), // AtRule(AtRule), } #[derive(Debug, Clone)] -pub enum BlockEntry { +enum BlockEntry { Style(Style), MultilineComment(String), // AtRule(AtRule), diff --git a/src/function.rs b/src/function.rs index 13b8e24..8885f06 100644 --- a/src/function.rs +++ b/src/function.rs @@ -6,10 +6,10 @@ use crate::utils::devour_whitespace; use crate::{Token, TokenKind}; #[derive(Debug, Clone, Eq, PartialEq)] -pub struct FuncArgs(pub Vec); +pub(crate) struct FuncArgs(pub Vec); #[derive(Debug, Clone, Eq, PartialEq)] -pub struct FuncArg { +pub(crate) struct FuncArg { pub name: String, pub default: Option>, } @@ -21,7 +21,7 @@ impl FuncArgs { } #[derive(Debug, Clone, std::default::Default)] -pub struct CallArgs(pub BTreeMap>); +pub(crate) struct CallArgs(pub BTreeMap>); impl CallArgs { pub fn new() -> Self { @@ -37,7 +37,7 @@ impl CallArgs { } } -pub fn eat_func_args>(toks: &mut Peekable) -> FuncArgs { +pub(crate) fn eat_func_args>(toks: &mut Peekable) -> FuncArgs { let mut args: Vec = Vec::new(); devour_whitespace(toks); @@ -112,7 +112,7 @@ pub fn eat_func_args>(toks: &mut Peekable) -> FuncA FuncArgs(args) } -pub fn eat_call_args>(toks: &mut Peekable) -> CallArgs { +pub(crate) fn eat_call_args>(toks: &mut Peekable) -> CallArgs { let mut args: BTreeMap> = BTreeMap::new(); devour_whitespace(toks); let mut name: Option = None; diff --git a/src/imports.rs b/src/imports.rs index 64a95dd..0f2c13c 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -3,7 +3,7 @@ use crate::{SassResult, Stmt, StyleSheet}; use std::ffi::OsStr; use std::path::Path; -pub fn import>(path: P) -> SassResult<(Vec, Scope)> { +pub(crate) fn import>(path: P) -> SassResult<(Vec, Scope)> { let mut rules: Vec = Vec::new(); let mut scope = Scope::new(); let path_buf = path.as_ref().to_path_buf(); diff --git a/src/lexer.rs b/src/lexer.rs index 7daf2f1..ac1f5fa 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -8,7 +8,7 @@ use crate::units::Unit; use crate::{Token, TokenKind, Whitespace}; #[derive(Debug, Clone)] -pub struct Lexer<'a> { +pub(crate) struct Lexer<'a> { tokens: Vec, buf: Peekable>, pos: Pos, diff --git a/src/lib.rs b/src/lib.rs index 9b24c52..8d59885 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,7 +76,7 @@ mod utils; pub type SassResult = Result; #[derive(Clone, Debug, Eq, PartialEq)] -pub struct Token { +pub(crate) struct Token { pos: Pos, pub kind: TokenKind, } @@ -100,10 +100,9 @@ impl IsWhitespace for &Token { } #[derive(Clone, Debug, Eq, PartialEq)] -pub enum TokenKind { +pub(crate) enum TokenKind { Ident(String), Symbol(Symbol), - String(String), AtRule(AtRuleKind), Keyword(Keyword), Number(String), @@ -122,7 +121,6 @@ impl Display for TokenKind { match self { TokenKind::Ident(s) | TokenKind::Number(s) => write!(f, "{}", s), TokenKind::Symbol(s) => write!(f, "{}", s), - TokenKind::String(s) => write!(f, "\"{}\"", s), TokenKind::AtRule(s) => write!(f, "{}", s), TokenKind::Op(s) => write!(f, "{}", s), TokenKind::Unit(s) => write!(f, "{}", s), @@ -143,7 +141,7 @@ impl Display for TokenKind { pub struct StyleSheet(Vec); #[derive(Clone, Debug, Eq, PartialEq)] -pub enum Stmt { +pub(crate) enum Stmt { /// A [`Style`](/grass/style/struct.Style) Style(Style), /// A [`RuleSet`](/grass/struct.RuleSet.html) @@ -163,7 +161,7 @@ pub enum Stmt { /// } /// ``` #[derive(Clone, Debug, Eq, PartialEq)] -pub struct RuleSet { +pub(crate) struct RuleSet { selector: Selector, rules: Vec, // potential optimization: we don't *need* to own the selector @@ -245,16 +243,32 @@ impl StyleSheet { /// /// Used mainly in debugging, but can at times be useful #[inline] - pub fn pretty_print(&self, buf: W) -> SassResult<()> { + #[allow(dead_code)] + 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` + /// + /// ``` + /// use std::io::{BufWriter, stdout}; + /// use grass::{SassResult, StyleSheet}; + /// # use tempfile::Builder; + /// # use std::io::Write; + /// + /// fn main() -> SassResult<()> { + /// # let mut file = Builder::new().prefix("input.scss").tempfile().unwrap(); + /// # write!(file, "a {{\n color: red}}")?; + /// let mut buf = BufWriter::new(stdout()); + /// StyleSheet::from_path("input.scss")?.print_as_css(&mut buf) + /// } + /// ``` #[inline] pub fn print_as_css(self, buf: &mut W) -> SassResult<()> { Css::from_stylesheet(self).pretty_print(buf) @@ -1190,7 +1204,6 @@ mod test_mixins { mod test_imports { use super::*; use tempfile::Builder; - use Write; macro_rules! test_import { ($func:ident, $input:literal => $output:literal | $( $name:literal($content:literal) ),*) => { diff --git a/src/mixin.rs b/src/mixin.rs index 55dc1d0..3e72e7b 100644 --- a/src/mixin.rs +++ b/src/mixin.rs @@ -8,7 +8,7 @@ use crate::utils::devour_whitespace; use crate::{eat_expr, Expr, RuleSet, Stmt, Token, TokenKind}; #[derive(Debug, Clone)] -pub struct Mixin { +pub(crate) struct Mixin { scope: Scope, args: FuncArgs, body: Peekable>, @@ -92,7 +92,7 @@ impl Mixin { self.eval(super_selector) } - pub fn eval(&mut self, super_selector: &Selector) -> Result, (Pos, String)> { + fn eval(&mut self, super_selector: &Selector) -> Result, (Pos, String)> { let mut stmts = Vec::new(); while let Some(expr) = eat_expr(&mut self.body, &self.scope, super_selector)? { match expr { @@ -118,7 +118,7 @@ impl Mixin { } } -pub fn eat_include>( +pub(crate) fn eat_include>( toks: &mut Peekable, scope: &Scope, super_selector: &Selector, diff --git a/src/selector.rs b/src/selector.rs index 812f2be..3665a18 100644 --- a/src/selector.rs +++ b/src/selector.rs @@ -7,7 +7,7 @@ use std::string::ToString; use std::vec::IntoIter; #[derive(Clone, Debug, Eq, PartialEq)] -pub struct Selector(pub Vec); +pub(crate) struct Selector(pub Vec); impl Display for Selector { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -47,7 +47,7 @@ impl Display for Selector { } #[derive(Clone, Debug, Eq, PartialEq)] -pub enum SelectorKind { +pub(crate) enum SelectorKind { /// An element selector: `button` Element(String), /// An id selector: `#footer` diff --git a/src/style.rs b/src/style.rs index 43d9c11..34971f0 100644 --- a/src/style.rs +++ b/src/style.rs @@ -7,7 +7,7 @@ use std::vec::IntoIter; /// A style: `color: red` #[derive(Clone, Debug, Eq, PartialEq)] -pub struct Style { +pub(crate) struct Style { property: String, value: String, } diff --git a/src/units.rs b/src/units.rs index e675769..9025e93 100644 --- a/src/units.rs +++ b/src/units.rs @@ -2,7 +2,7 @@ use std::convert::TryFrom; use std::fmt; #[derive(Copy, Clone, Debug, Eq, PartialEq)] -pub enum Unit { +pub(crate) enum Unit { // Absolute units /// Pixels Px, diff --git a/src/utils.rs b/src/utils.rs index 24e91ac..6c68c4d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -2,11 +2,11 @@ use crate::common::{Pos, Symbol, Whitespace}; use crate::{Scope, Token, TokenKind}; use std::iter::{Iterator, Peekable}; -pub trait IsWhitespace { +pub(crate) trait IsWhitespace { fn is_whitespace(&self) -> bool; } -pub fn devour_whitespace, W: IsWhitespace>(s: &mut Peekable) -> bool { +pub(crate) fn devour_whitespace, W: IsWhitespace>(s: &mut Peekable) -> bool { let mut found_whitespace = false; while let Some(w) = s.peek() { if !w.is_whitespace() { @@ -19,7 +19,7 @@ pub fn devour_whitespace, W: IsWhitespace>(s: &mut Peekabl } #[track_caller] -pub fn deref_variable(name: &str, scope: &Scope) -> Vec { +pub(crate) fn deref_variable(name: &str, scope: &Scope) -> Vec { let mut toks = scope .vars .get(name) @@ -45,7 +45,7 @@ pub fn deref_variable(name: &str, scope: &Scope) -> Vec { val } -pub fn eat_interpolation>( +pub(crate) fn eat_interpolation>( tokens: &mut Peekable, scope: &Scope, ) -> Vec { @@ -63,7 +63,7 @@ pub fn eat_interpolation>( val } -pub fn eat_variable_value>( +pub(crate) fn eat_variable_value>( toks: &mut Peekable, scope: &Scope, ) -> Result, (Pos, String)> {