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)]
pub struct Scope {
pub(crate) struct Scope {
pub vars: HashMap<String, Vec<Token>>,
pub mixins: HashMap<String, Mixin>,
}

View File

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

View File

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

View File

@ -3,7 +3,7 @@ use crate::{SassResult, Stmt, StyleSheet};
use std::ffi::OsStr;
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 scope = Scope::new();
let path_buf = path.as_ref().to_path_buf();

View File

@ -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<Token>,
buf: Peekable<Chars<'a>>,
pos: Pos,

View File

@ -76,7 +76,7 @@ mod utils;
pub type SassResult<T> = Result<T, SassError>;
#[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<Stmt>);
#[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<Stmt>,
// 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<W: Write>(&self, buf: W) -> SassResult<()> {
#[allow(dead_code)]
fn pretty_print<W: Write>(&self, buf: W) -> SassResult<()> {
PrettyPrinter::new(buf).pretty_print(self)
}
#[inline]
#[allow(dead_code)]
fn pretty_print_selectors<W: Write>(&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<W: Write>(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) ),*) => {

View File

@ -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<IntoIter<Token>>,
@ -92,7 +92,7 @@ impl Mixin {
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();
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<I: Iterator<Item = Token>>(
pub(crate) fn eat_include<I: Iterator<Item = Token>>(
toks: &mut Peekable<I>,
scope: &Scope,
super_selector: &Selector,

View File

@ -7,7 +7,7 @@ use std::string::ToString;
use std::vec::IntoIter;
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Selector(pub Vec<SelectorKind>);
pub(crate) struct Selector(pub Vec<SelectorKind>);
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`

View File

@ -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,
}

View File

@ -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,

View File

@ -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<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;
while let Some(w) = s.peek() {
if !w.is_whitespace() {
@ -19,7 +19,7 @@ pub fn devour_whitespace<I: Iterator<Item = W>, W: IsWhitespace>(s: &mut Peekabl
}
#[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
.vars
.get(name)
@ -45,7 +45,7 @@ pub fn deref_variable(name: &str, scope: &Scope) -> Vec<Token> {
val
}
pub fn eat_interpolation<I: Iterator<Item = Token>>(
pub(crate) fn eat_interpolation<I: Iterator<Item = Token>>(
tokens: &mut Peekable<I>,
scope: &Scope,
) -> Vec<Token> {
@ -63,7 +63,7 @@ pub fn eat_interpolation<I: Iterator<Item = Token>>(
val
}
pub fn eat_variable_value<I: Iterator<Item = Token>>(
pub(crate) fn eat_variable_value<I: Iterator<Item = Token>>(
toks: &mut Peekable<I>,
scope: &Scope,
) -> Result<Vec<Token>, (Pos, String)> {