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)]
pub fn name(self) -> &'static str {
match self {

View File

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

View File

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

View File

@ -1,8 +1,9 @@
use std::ffi::OsStr;
use std::path::Path;
use crate::common::Scope;
use crate::error::SassResult;
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)> {
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> {
pub fn new(buf: &'a str) -> Lexer<'a> {
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 {
self.buf.next();
self.pos.next_char();

View File

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

View File

@ -1,6 +1,7 @@
use clap::{App, Arg};
use std::io::{stdout, BufWriter};
use clap::{App, Arg};
use grass::StyleSheet;
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::error::SassResult;
use crate::lexer::Lexer;
@ -6,9 +10,6 @@ use crate::utils::{
parse_quoted_string, IsWhitespace,
};
use crate::{Token, TokenKind};
use std::fmt::{self, Display, Write};
use std::iter::Peekable;
use std::string::ToString;
#[derive(Clone, Debug, Eq, PartialEq)]
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::error::SassResult;
use crate::selector::Selector;
use crate::utils::{devour_whitespace, parse_interpolation, parse_quoted_string};
use crate::value::Value;
use crate::{Expr, Token, TokenKind};
use std::fmt::{self, Display};
use std::iter::Peekable;
/// A style: `color: red`
#[derive(Clone, Debug, Eq, PartialEq)]

View File

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

View File

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

View File

@ -22,7 +22,16 @@ impl Add for Value {
_ => Value::Ident(format!("{}", other), QuoteKind::None),
},
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) => {
let quotes = match q {
QuoteKind::Double | QuoteKind::Single => QuoteKind::Double,
@ -99,7 +108,16 @@ impl Sub for Value {
Ok(match self {
Self::Null => todo!(),
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!(),
},
// Self::List(..) => todo!(),
@ -181,7 +199,22 @@ impl Mul for Value {
Ok(match self {
Self::Null => todo!(),
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(
format!("Undefined operation \"{}{} * {}\".", num, unit, other).into(),

View File

@ -75,7 +75,7 @@ impl Value {
super_selector: &Selector,
) -> SassResult<Self> {
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() {
Some(x) => x,
None => return Ok(left),

View File

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