Make everything private except StyleSheet

This commit is contained in:
ConnorSkees 2020-01-20 13:15:47 -05:00
parent c0e82ba0a8
commit 25b4bebdf8
11 changed files with 43 additions and 30 deletions

View File

@ -476,7 +476,7 @@ impl Display for Pos {
} }
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct Scope { pub(crate) struct Scope {
pub vars: HashMap<String, Vec<Token>>, pub vars: HashMap<String, Vec<Token>>,
pub mixins: HashMap<String, Mixin>, pub mixins: HashMap<String, Mixin>,
} }

View File

@ -4,14 +4,14 @@ use std::fmt;
use std::io::Write; use std::io::Write;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum Toplevel { enum Toplevel {
RuleSet(Selector, Vec<BlockEntry>), RuleSet(Selector, Vec<BlockEntry>),
MultilineComment(String), MultilineComment(String),
// AtRule(AtRule), // AtRule(AtRule),
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub enum BlockEntry { enum BlockEntry {
Style(Style), Style(Style),
MultilineComment(String), MultilineComment(String),
// AtRule(AtRule), // AtRule(AtRule),

View File

@ -6,10 +6,10 @@ use crate::utils::devour_whitespace;
use crate::{Token, TokenKind}; use crate::{Token, TokenKind};
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct FuncArgs(pub Vec<FuncArg>); pub(crate) struct FuncArgs(pub Vec<FuncArg>);
#[derive(Debug, Clone, Eq, PartialEq)] #[derive(Debug, Clone, Eq, PartialEq)]
pub struct FuncArg { pub(crate) struct FuncArg {
pub name: String, pub name: String,
pub default: Option<Vec<Token>>, pub default: Option<Vec<Token>>,
} }
@ -21,7 +21,7 @@ impl FuncArgs {
} }
#[derive(Debug, Clone, std::default::Default)] #[derive(Debug, Clone, std::default::Default)]
pub struct CallArgs(pub BTreeMap<String, Vec<Token>>); pub(crate) struct CallArgs(pub BTreeMap<String, Vec<Token>>);
impl CallArgs { impl CallArgs {
pub fn new() -> Self { pub fn new() -> Self {
@ -37,7 +37,7 @@ impl CallArgs {
} }
} }
pub fn eat_func_args<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> FuncArgs { pub(crate) fn eat_func_args<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> FuncArgs {
let mut args: Vec<FuncArg> = Vec::new(); let mut args: Vec<FuncArg> = Vec::new();
devour_whitespace(toks); devour_whitespace(toks);
@ -112,7 +112,7 @@ pub fn eat_func_args<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> FuncA
FuncArgs(args) FuncArgs(args)
} }
pub fn eat_call_args<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> CallArgs { pub(crate) fn eat_call_args<I: Iterator<Item = Token>>(toks: &mut Peekable<I>) -> CallArgs {
let mut args: BTreeMap<String, Vec<Token>> = BTreeMap::new(); let mut args: BTreeMap<String, Vec<Token>> = BTreeMap::new();
devour_whitespace(toks); devour_whitespace(toks);
let mut name: Option<String> = None; let mut name: Option<String> = None;

View File

@ -3,7 +3,7 @@ use crate::{SassResult, Stmt, StyleSheet};
use std::ffi::OsStr; use std::ffi::OsStr;
use std::path::Path; use std::path::Path;
pub fn import<P: AsRef<Path>>(path: P) -> SassResult<(Vec<Stmt>, Scope)> { pub(crate) fn import<P: AsRef<Path>>(path: P) -> SassResult<(Vec<Stmt>, Scope)> {
let mut rules: Vec<Stmt> = Vec::new(); let mut rules: Vec<Stmt> = Vec::new();
let mut scope = Scope::new(); let mut scope = Scope::new();
let path_buf = path.as_ref().to_path_buf(); let path_buf = path.as_ref().to_path_buf();

View File

@ -8,7 +8,7 @@ use crate::units::Unit;
use crate::{Token, TokenKind, Whitespace}; use crate::{Token, TokenKind, Whitespace};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Lexer<'a> { pub(crate) struct Lexer<'a> {
tokens: Vec<Token>, tokens: Vec<Token>,
buf: Peekable<Chars<'a>>, buf: Peekable<Chars<'a>>,
pos: Pos, pos: Pos,

View File

@ -76,7 +76,7 @@ mod utils;
pub type SassResult<T> = Result<T, SassError>; pub type SassResult<T> = Result<T, SassError>;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct Token { pub(crate) struct Token {
pos: Pos, pos: Pos,
pub kind: TokenKind, pub kind: TokenKind,
} }
@ -100,10 +100,9 @@ impl IsWhitespace for &Token {
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum TokenKind { pub(crate) enum TokenKind {
Ident(String), Ident(String),
Symbol(Symbol), Symbol(Symbol),
String(String),
AtRule(AtRuleKind), AtRule(AtRuleKind),
Keyword(Keyword), Keyword(Keyword),
Number(String), Number(String),
@ -122,7 +121,6 @@ impl Display for TokenKind {
match self { match self {
TokenKind::Ident(s) | TokenKind::Number(s) => write!(f, "{}", s), TokenKind::Ident(s) | TokenKind::Number(s) => write!(f, "{}", s),
TokenKind::Symbol(s) => write!(f, "{}", s), TokenKind::Symbol(s) => write!(f, "{}", s),
TokenKind::String(s) => write!(f, "\"{}\"", s),
TokenKind::AtRule(s) => write!(f, "{}", s), TokenKind::AtRule(s) => write!(f, "{}", s),
TokenKind::Op(s) => write!(f, "{}", s), TokenKind::Op(s) => write!(f, "{}", s),
TokenKind::Unit(s) => write!(f, "{}", s), TokenKind::Unit(s) => write!(f, "{}", s),
@ -143,7 +141,7 @@ impl Display for TokenKind {
pub struct StyleSheet(Vec<Stmt>); pub struct StyleSheet(Vec<Stmt>);
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum Stmt { pub(crate) enum Stmt {
/// A [`Style`](/grass/style/struct.Style) /// A [`Style`](/grass/style/struct.Style)
Style(Style), Style(Style),
/// A [`RuleSet`](/grass/struct.RuleSet.html) /// A [`RuleSet`](/grass/struct.RuleSet.html)
@ -163,7 +161,7 @@ pub enum Stmt {
/// } /// }
/// ``` /// ```
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct RuleSet { pub(crate) struct RuleSet {
selector: Selector, selector: Selector,
rules: Vec<Stmt>, rules: Vec<Stmt>,
// potential optimization: we don't *need* to own the selector // 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 /// Used mainly in debugging, but can at times be useful
#[inline] #[inline]
pub fn pretty_print<W: Write>(&self, buf: W) -> SassResult<()> { #[allow(dead_code)]
fn pretty_print<W: Write>(&self, buf: W) -> SassResult<()> {
PrettyPrinter::new(buf).pretty_print(self) PrettyPrinter::new(buf).pretty_print(self)
} }
#[inline]
#[allow(dead_code)] #[allow(dead_code)]
fn pretty_print_selectors<W: Write>(&self, buf: W) -> SassResult<()> { fn pretty_print_selectors<W: Write>(&self, buf: W) -> SassResult<()> {
PrettyPrinter::new(buf).pretty_print_preserve_super_selectors(self) PrettyPrinter::new(buf).pretty_print_preserve_super_selectors(self)
} }
/// Write the internal representation as CSS to `buf` /// 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] #[inline]
pub fn print_as_css<W: Write>(self, buf: &mut W) -> SassResult<()> { pub fn print_as_css<W: Write>(self, buf: &mut W) -> SassResult<()> {
Css::from_stylesheet(self).pretty_print(buf) Css::from_stylesheet(self).pretty_print(buf)
@ -1190,7 +1204,6 @@ mod test_mixins {
mod test_imports { mod test_imports {
use super::*; use super::*;
use tempfile::Builder; use tempfile::Builder;
use Write;
macro_rules! test_import { macro_rules! test_import {
($func:ident, $input:literal => $output:literal | $( $name:literal($content:literal) ),*) => { ($func:ident, $input:literal => $output:literal | $( $name:literal($content:literal) ),*) => {

View File

@ -8,7 +8,7 @@ use crate::utils::devour_whitespace;
use crate::{eat_expr, Expr, RuleSet, Stmt, Token, TokenKind}; use crate::{eat_expr, Expr, RuleSet, Stmt, Token, TokenKind};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Mixin { pub(crate) struct Mixin {
scope: Scope, scope: Scope,
args: FuncArgs, args: FuncArgs,
body: Peekable<IntoIter<Token>>, body: Peekable<IntoIter<Token>>,
@ -92,7 +92,7 @@ impl Mixin {
self.eval(super_selector) self.eval(super_selector)
} }
pub fn eval(&mut self, super_selector: &Selector) -> Result<Vec<Stmt>, (Pos, String)> { fn eval(&mut self, super_selector: &Selector) -> Result<Vec<Stmt>, (Pos, String)> {
let mut stmts = Vec::new(); let mut stmts = Vec::new();
while let Some(expr) = eat_expr(&mut self.body, &self.scope, super_selector)? { while let Some(expr) = eat_expr(&mut self.body, &self.scope, super_selector)? {
match expr { match expr {
@ -118,7 +118,7 @@ impl Mixin {
} }
} }
pub fn eat_include<I: Iterator<Item = Token>>( pub(crate) fn eat_include<I: Iterator<Item = Token>>(
toks: &mut Peekable<I>, toks: &mut Peekable<I>,
scope: &Scope, scope: &Scope,
super_selector: &Selector, super_selector: &Selector,

View File

@ -7,7 +7,7 @@ use std::string::ToString;
use std::vec::IntoIter; use std::vec::IntoIter;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct Selector(pub Vec<SelectorKind>); pub(crate) struct Selector(pub Vec<SelectorKind>);
impl Display for Selector { impl Display for Selector {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
@ -47,7 +47,7 @@ impl Display for Selector {
} }
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub enum SelectorKind { pub(crate) enum SelectorKind {
/// An element selector: `button` /// An element selector: `button`
Element(String), Element(String),
/// An id selector: `#footer` /// An id selector: `#footer`

View File

@ -7,7 +7,7 @@ use std::vec::IntoIter;
/// A style: `color: red` /// A style: `color: red`
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub struct Style { pub(crate) struct Style {
property: String, property: String,
value: String, value: String,
} }

View File

@ -2,7 +2,7 @@ use std::convert::TryFrom;
use std::fmt; use std::fmt;
#[derive(Copy, Clone, Debug, Eq, PartialEq)] #[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Unit { pub(crate) enum Unit {
// Absolute units // Absolute units
/// Pixels /// Pixels
Px, Px,

View File

@ -2,11 +2,11 @@ use crate::common::{Pos, Symbol, Whitespace};
use crate::{Scope, Token, TokenKind}; use crate::{Scope, Token, TokenKind};
use std::iter::{Iterator, Peekable}; use std::iter::{Iterator, Peekable};
pub trait IsWhitespace { pub(crate) trait IsWhitespace {
fn is_whitespace(&self) -> bool; fn is_whitespace(&self) -> bool;
} }
pub fn devour_whitespace<I: Iterator<Item = W>, W: IsWhitespace>(s: &mut Peekable<I>) -> bool { pub(crate) fn devour_whitespace<I: Iterator<Item = W>, W: IsWhitespace>(s: &mut Peekable<I>) -> bool {
let mut found_whitespace = false; let mut found_whitespace = false;
while let Some(w) = s.peek() { while let Some(w) = s.peek() {
if !w.is_whitespace() { if !w.is_whitespace() {
@ -19,7 +19,7 @@ pub fn devour_whitespace<I: Iterator<Item = W>, W: IsWhitespace>(s: &mut Peekabl
} }
#[track_caller] #[track_caller]
pub fn deref_variable(name: &str, scope: &Scope) -> Vec<Token> { pub(crate) fn deref_variable(name: &str, scope: &Scope) -> Vec<Token> {
let mut toks = scope let mut toks = scope
.vars .vars
.get(name) .get(name)
@ -45,7 +45,7 @@ pub fn deref_variable(name: &str, scope: &Scope) -> Vec<Token> {
val val
} }
pub fn eat_interpolation<I: Iterator<Item = Token>>( pub(crate) fn eat_interpolation<I: Iterator<Item = Token>>(
tokens: &mut Peekable<I>, tokens: &mut Peekable<I>,
scope: &Scope, scope: &Scope,
) -> Vec<Token> { ) -> Vec<Token> {
@ -63,7 +63,7 @@ pub fn eat_interpolation<I: Iterator<Item = Token>>(
val val
} }
pub fn eat_variable_value<I: Iterator<Item = Token>>( pub(crate) fn eat_variable_value<I: Iterator<Item = Token>>(
toks: &mut Peekable<I>, toks: &mut Peekable<I>,
scope: &Scope, scope: &Scope,
) -> Result<Vec<Token>, (Pos, String)> { ) -> Result<Vec<Token>, (Pos, String)> {