refactor and cleaning

This commit is contained in:
ConnorSkees 2020-03-01 14:53:52 -05:00
parent d7b22a41a6
commit 3181d58441
14 changed files with 64 additions and 44 deletions

View File

@ -454,6 +454,7 @@ impl ListSeparator {
} }
} }
// Used in currently unimplemented builtin function
#[allow(dead_code)] #[allow(dead_code)]
pub fn name(self) -> &'static str { pub fn name(self) -> &'static str {
match self { match self {

View File

@ -1,11 +1,12 @@
//! # Convert from SCSS AST to CSS //! # Convert from SCSS AST to CSS
use std::fmt;
use std::io::Write;
use std::sync::atomic::Ordering;
use crate::atrule::AtRule; use crate::atrule::AtRule;
use crate::error::SassResult; use crate::error::SassResult;
use crate::lexer::IS_UTF8; use crate::lexer::IS_UTF8;
use crate::{RuleSet, Selector, Stmt, Style, StyleSheet}; use crate::{RuleSet, Selector, Stmt, Style, StyleSheet};
use std::fmt;
use std::io::Write;
use std::sync::atomic::Ordering;
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum Toplevel { enum Toplevel {
@ -24,8 +25,8 @@ enum BlockEntry {
impl fmt::Display for BlockEntry { impl fmt::Display for BlockEntry {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
BlockEntry::Style(s) => writeln!(f, " {}", s), BlockEntry::Style(s) => writeln!(f, "{}", s),
BlockEntry::MultilineComment(s) => writeln!(f, " /*{}*/", s), BlockEntry::MultilineComment(s) => writeln!(f, "/*{}*/", s),
} }
} }
} }
@ -112,9 +113,9 @@ impl Css {
} else { } else {
self.blocks.push(Toplevel::Newline); self.blocks.push(Toplevel::Newline);
} }
}
self.blocks.extend(v); self.blocks.extend(v);
} }
}
self self
} }
@ -133,7 +134,7 @@ impl Css {
has_written = true; has_written = true;
writeln!(buf, "{}{} {{", padding, selector)?; writeln!(buf, "{}{} {{", padding, selector)?;
for style in styles { for style in styles {
write!(buf, "{}{}", padding, style)?; write!(buf, "{} {}", padding, style)?;
} }
writeln!(buf, "{}}}", padding)?; writeln!(buf, "{}}}", padding)?;
} }
@ -148,7 +149,7 @@ impl Css {
} else { } else {
writeln!(buf, "{}@{} {} {{", padding, u.name, u.params)?; writeln!(buf, "{}@{} {} {{", padding, u.name, u.params)?;
} }
Css::from_stylesheet(StyleSheet::from_stmts(u.body.clone())) Css::from_stylesheet(StyleSheet::from_stmts(u.body))
.pretty_print(buf, nesting + 1)?; .pretty_print(buf, nesting + 1)?;
writeln!(buf, "{}}}", padding)?; writeln!(buf, "{}}}", padding)?;
} }

View File

@ -1,9 +1,10 @@
use crate::common::Pos;
use std::error::Error; use std::error::Error;
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use std::io; use std::io;
use std::string::FromUtf8Error; use std::string::FromUtf8Error;
use crate::common::Pos;
pub type SassResult<T> = Result<T, SassError>; pub type SassResult<T> = Result<T, SassError>;
#[derive(Debug)] #[derive(Debug)]

View File

@ -1,8 +1,9 @@
use std::ffi::OsStr;
use std::path::Path;
use crate::common::Scope; use crate::common::Scope;
use crate::error::SassResult; use crate::error::SassResult;
use crate::{Stmt, StyleSheet}; use crate::{Stmt, StyleSheet};
use std::ffi::OsStr;
use std::path::Path;
pub(crate) 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();

View File

@ -150,11 +150,6 @@ impl<'a> Iterator for Lexer<'a> {
} }
} }
#[allow(dead_code)]
fn is_whitespace(c: char) -> bool {
c == ' ' || c == '\n' || c == '\r' || c == FORM_FEED
}
impl<'a> Lexer<'a> { impl<'a> Lexer<'a> {
pub fn new(buf: &'a str) -> Lexer<'a> { pub fn new(buf: &'a str) -> Lexer<'a> {
Lexer { Lexer {
@ -193,20 +188,6 @@ impl<'a> Lexer<'a> {
} }
} }
#[allow(dead_code)]
fn devour_whitespace(&mut self) -> bool {
let mut found_whitespace = false;
while let Some(c) = self.buf.peek() {
if !is_whitespace(*c) {
return found_whitespace;
}
found_whitespace = true;
self.buf.next();
self.pos.next_char();
}
found_whitespace
}
fn lex_at_rule(&mut self) -> TokenKind { fn lex_at_rule(&mut self) -> TokenKind {
self.buf.next(); self.buf.next();
self.pos.next_char(); self.pos.next_char();

View File

@ -78,7 +78,6 @@
clippy::or_fun_call, clippy::or_fun_call,
)] )]
#![cfg_attr(feature = "nightly", feature(track_caller))] #![cfg_attr(feature = "nightly", feature(track_caller))]
// todo! handle erroring on styles at the toplevel
use std::fmt::{self, Display}; use std::fmt::{self, Display};
use std::fs; use std::fs;
use std::io::Write; use std::io::Write;

View File

@ -1,6 +1,7 @@
use clap::{App, Arg};
use std::io::{stdout, BufWriter}; use std::io::{stdout, BufWriter};
use clap::{App, Arg};
use grass::StyleSheet; use grass::StyleSheet;
fn main() { fn main() {

View File

@ -1,3 +1,7 @@
use std::fmt::{self, Display, Write};
use std::iter::Peekable;
use std::string::ToString;
use crate::common::{Scope, Symbol, Whitespace}; use crate::common::{Scope, Symbol, Whitespace};
use crate::error::SassResult; use crate::error::SassResult;
use crate::lexer::Lexer; use crate::lexer::Lexer;
@ -6,9 +10,6 @@ use crate::utils::{
parse_quoted_string, IsWhitespace, parse_quoted_string, IsWhitespace,
}; };
use crate::{Token, TokenKind}; use crate::{Token, TokenKind};
use std::fmt::{self, Display, Write};
use std::iter::Peekable;
use std::string::ToString;
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]
pub(crate) struct Selector(pub Vec<SelectorKind>); pub(crate) struct Selector(pub Vec<SelectorKind>);

View File

@ -1,11 +1,12 @@
use std::fmt::{self, Display};
use std::iter::Peekable;
use crate::common::{Pos, QuoteKind, Scope, Symbol}; use crate::common::{Pos, QuoteKind, Scope, Symbol};
use crate::error::SassResult; use crate::error::SassResult;
use crate::selector::Selector; use crate::selector::Selector;
use crate::utils::{devour_whitespace, parse_interpolation, parse_quoted_string}; use crate::utils::{devour_whitespace, parse_interpolation, parse_quoted_string};
use crate::value::Value; use crate::value::Value;
use crate::{Expr, Token, TokenKind}; use crate::{Expr, Token, TokenKind};
use std::fmt::{self, Display};
use std::iter::Peekable;
/// A style: `color: red` /// A style: `color: red`
#[derive(Clone, Debug, Eq, PartialEq)] #[derive(Clone, Debug, Eq, PartialEq)]

View File

@ -1,10 +1,11 @@
use std::iter::{Iterator, Peekable};
use crate::common::{Keyword, QuoteKind, Symbol}; use crate::common::{Keyword, QuoteKind, Symbol};
use crate::error::SassResult; use crate::error::SassResult;
use crate::lexer::Lexer; use crate::lexer::Lexer;
use crate::selector::Selector; use crate::selector::Selector;
use crate::value::Value; use crate::value::Value;
use crate::{Scope, Token, TokenKind}; use crate::{Scope, Token, TokenKind};
use std::iter::{Iterator, Peekable};
pub(crate) trait IsWhitespace { pub(crate) trait IsWhitespace {
fn is_whitespace(&self) -> bool; fn is_whitespace(&self) -> bool;

View File

@ -1,4 +1,3 @@
#![allow(dead_code, unused_variables)]
use std::fmt::{self, Display, Write}; use std::fmt::{self, Display, Write};
use std::iter::Iterator; use std::iter::Iterator;

View File

@ -22,7 +22,16 @@ impl Add for Value {
_ => Value::Ident(format!("{}", other), QuoteKind::None), _ => Value::Ident(format!("{}", other), QuoteKind::None),
}, },
Self::Dimension(num, unit) => match other { Self::Dimension(num, unit) => match other {
Self::Dimension(num2, unit2) => Value::Dimension(num + num2, unit), Self::Dimension(num2, unit2) => {
if !unit.comparable(&unit2) {
return Err(format!("Incompatible units {} and {}.", unit2, unit).into());
}
if unit == unit2 {
Value::Dimension(num + num2, unit)
} else {
todo!("unit conversions")
}
}
Self::Ident(s, q) => { Self::Ident(s, q) => {
let quotes = match q { let quotes = match q {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double, QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
@ -99,7 +108,16 @@ impl Sub for Value {
Ok(match self { Ok(match self {
Self::Null => todo!(), Self::Null => todo!(),
Self::Dimension(num, unit) => match other { Self::Dimension(num, unit) => match other {
Self::Dimension(num2, unit2) => Value::Dimension(num - num2, unit), Self::Dimension(num2, unit2) => {
if !unit.comparable(&unit2) {
return Err(format!("Incompatible units {} and {}.", unit2, unit).into());
}
if unit == unit2 {
Value::Dimension(num - num2, unit)
} else {
todo!("unit conversions")
}
}
_ => todo!(), _ => todo!(),
}, },
// Self::List(..) => todo!(), // Self::List(..) => todo!(),
@ -181,7 +199,22 @@ impl Mul for Value {
Ok(match self { Ok(match self {
Self::Null => todo!(), Self::Null => todo!(),
Self::Dimension(num, unit) => match other { Self::Dimension(num, unit) => match other {
Self::Dimension(num2, unit2) => Value::Dimension(num * num2, unit), Self::Dimension(num2, unit2) => {
if unit2 != Unit::None && unit != Unit::None {
return Err(format!(
"{}{}*{} isn't a valid CSS value.",
num * num2,
unit,
unit2
)
.into());
}
if unit == unit2 {
Value::Dimension(num * num2, unit)
} else {
todo!("unit conversions")
}
}
_ => { _ => {
return Err( return Err(
format!("Undefined operation \"{}{} * {}\".", num, unit, other).into(), format!("Undefined operation \"{}{} * {}\".", num, unit, other).into(),

View File

@ -75,7 +75,7 @@ impl Value {
super_selector: &Selector, super_selector: &Selector,
) -> SassResult<Self> { ) -> SassResult<Self> {
let left = Self::_from_tokens(toks, scope, super_selector)?; let left = Self::_from_tokens(toks, scope, super_selector)?;
let whitespace = devour_whitespace_or_comment(toks); devour_whitespace_or_comment(toks);
let next = match toks.peek() { let next = match toks.peek() {
Some(x) => x, Some(x) => x,
None => return Ok(left), None => return Ok(left),

View File

@ -1,6 +1,6 @@
#![cfg(test)] #![cfg(test)]
#[allow(unused_macros)] #[macro_export]
macro_rules! test { macro_rules! test {
($func:ident, $input:literal) => { ($func:ident, $input:literal) => {
#[test] #[test]