diff --git a/src/css.rs b/src/css.rs index 5191b37..4ba4e33 100644 --- a/src/css.rs +++ b/src/css.rs @@ -1,7 +1,7 @@ //! # Convert from SCSS AST to CSS -use crate::{RuleSet, Selector, Stmt, Style, StyleSheet}; +use crate::{RuleSet, SassResult, Selector, Stmt, Style, StyleSheet}; use std::fmt; -use std::io::{self, Write}; +use std::io::Write; #[derive(Debug, Clone)] pub enum Toplevel { @@ -94,7 +94,7 @@ impl Css { self } - pub fn pretty_print(self, buf: &mut W) -> io::Result<()> { + pub fn pretty_print(self, buf: &mut W) -> SassResult<()> { for block in self.blocks { match block { Toplevel::RuleSet(selector, styles) => { diff --git a/src/error.rs b/src/error.rs index 438aae6..cd4879a 100644 --- a/src/error.rs +++ b/src/error.rs @@ -2,6 +2,7 @@ use crate::common::Pos; use std::error::Error; use std::fmt::{self, Display}; use std::io; +use std::string::FromUtf8Error; #[derive(Debug)] pub struct SassError { @@ -16,13 +17,6 @@ impl SassError { pos, } } - - pub fn unexpected_eof(pos: Pos) -> Self { - SassError { - message: String::from("unexpected eof"), - pos, - } - } } impl Display for SassError { @@ -40,6 +34,21 @@ impl From for SassError { } } +impl From for SassError { + fn from(error: FromUtf8Error) -> Self { + SassError { + pos: Pos::new(), + message: format!("Invalid UTF-8 character \"\\x{:X?}\"", error.as_bytes()[0]), + } + } +} + +impl From for String { + fn from(error: SassError) -> String { + error.message + } +} + impl Error for SassError { fn description(&self) -> &'static str { "SASS parsing error" diff --git a/src/format.rs b/src/format.rs index c3c7d07..1e6b420 100644 --- a/src/format.rs +++ b/src/format.rs @@ -1,6 +1,6 @@ -use std::io::{self, Write}; +use std::io::Write; -use crate::{RuleSet, Stmt, StyleSheet}; +use crate::{RuleSet, SassResult, Stmt, StyleSheet}; pub(crate) struct PrettyPrinter { buf: W, @@ -14,7 +14,7 @@ impl PrettyPrinter { /// Pretty print `crate::Stmt` /// Throws away super selectors and variables - fn pretty_print_stmt(&mut self, stmt: &Stmt) -> io::Result<()> { + fn pretty_print_stmt(&mut self, stmt: &Stmt) -> SassResult<()> { let padding = vec![' '; self.scope * 2].iter().collect::(); match stmt { Stmt::MultilineComment(s) => writeln!(self.buf, "{}/*{}*/", padding, s)?, @@ -40,7 +40,7 @@ impl PrettyPrinter { /// /// The result should be an exact copy of the SCSS input /// Empty rules are included - pub fn pretty_print(&mut self, s: &StyleSheet) -> io::Result<()> { + pub fn pretty_print(&mut self, s: &StyleSheet) -> SassResult<()> { for rule in &s.0 { self.pretty_print_stmt(rule)?; } @@ -49,7 +49,7 @@ impl PrettyPrinter { /// Pretty print `crate::Stmt` /// Keeps super selectors - fn pretty_print_stmt_preserve_super_selectors(&mut self, stmt: &Stmt) -> io::Result<()> { + fn pretty_print_stmt_preserve_super_selectors(&mut self, stmt: &Stmt) -> SassResult<()> { let padding = vec![' '; self.scope * 2].iter().collect::(); match stmt { Stmt::MultilineComment(s) => writeln!(self.buf, "/*{}*/", s)?, @@ -79,7 +79,7 @@ impl PrettyPrinter { pub(crate) fn pretty_print_preserve_super_selectors( &mut self, s: &StyleSheet, - ) -> io::Result<()> { + ) -> SassResult<()> { for rule in &s.0 { self.pretty_print_stmt_preserve_super_selectors(rule)?; } diff --git a/src/lib.rs b/src/lib.rs index cf2c0e3..7dd0c32 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,7 +43,7 @@ // todo! handle erroring on styles at the toplevel use std::fmt::{self, Display}; use std::fs; -use std::io::{self, Write}; +use std::io::Write; use std::iter::{Iterator, Peekable}; use std::path::Path; @@ -209,10 +209,12 @@ impl StyleSheet { } pub fn from_path + Into>(p: P) -> SassResult { + let s = String::from_utf8(fs::read(p.as_ref())?)?; + dbg!(&s); Ok(StyleSheet( StyleSheetParser { global_scope: Scope::new(), - lexer: Lexer::new(&fs::read_to_string(p.as_ref())?).peekable(), + lexer: Lexer::new(&s).peekable(), rules: Vec::new(), scope: 0, file: p.into(), @@ -241,17 +243,17 @@ impl StyleSheet { /// to pure CSS /// /// Used mainly in debugging, but can at times be useful - pub fn pretty_print(&self, buf: W) -> io::Result<()> { + pub fn pretty_print(&self, buf: W) -> SassResult<()> { PrettyPrinter::new(buf).pretty_print(self) } #[allow(dead_code)] - fn pretty_print_selectors(&self, buf: W) -> io::Result<()> { + 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` - pub fn print_as_css(self, buf: &mut W) -> io::Result<()> { + pub fn print_as_css(self, buf: &mut W) -> SassResult<()> { Css::from_stylesheet(self).pretty_print(buf) } } diff --git a/src/main.rs b/src/main.rs index eb1ddd9..da91851 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,9 @@ use clap::{App, Arg}; -use std::io::{BufWriter, stdout}; +use std::io::{stdout, BufWriter}; -use grass::{SassResult, StyleSheet}; +use grass::StyleSheet; -fn main() -> SassResult<()> { +fn main() -> Result<(), String> { let matches = App::new("grass") .version(env!("CARGO_PKG_VERSION")) .about("SCSS Compiler in rust")