Convert errors to strings for pretty printing
This commit is contained in:
parent
0824a019c2
commit
1cd38f0317
@ -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<W: Write>(self, buf: &mut W) -> io::Result<()> {
|
||||
pub fn pretty_print<W: Write>(self, buf: &mut W) -> SassResult<()> {
|
||||
for block in self.blocks {
|
||||
match block {
|
||||
Toplevel::RuleSet(selector, styles) => {
|
||||
|
23
src/error.rs
23
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<io::Error> for SassError {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<FromUtf8Error> 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<SassError> for String {
|
||||
fn from(error: SassError) -> String {
|
||||
error.message
|
||||
}
|
||||
}
|
||||
|
||||
impl Error for SassError {
|
||||
fn description(&self) -> &'static str {
|
||||
"SASS parsing error"
|
||||
|
@ -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<W: Write> {
|
||||
buf: W,
|
||||
@ -14,7 +14,7 @@ impl<W: Write> PrettyPrinter<W> {
|
||||
|
||||
/// 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::<String>();
|
||||
match stmt {
|
||||
Stmt::MultilineComment(s) => writeln!(self.buf, "{}/*{}*/", padding, s)?,
|
||||
@ -40,7 +40,7 @@ impl<W: Write> PrettyPrinter<W> {
|
||||
///
|
||||
/// 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<W: Write> PrettyPrinter<W> {
|
||||
|
||||
/// 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::<String>();
|
||||
match stmt {
|
||||
Stmt::MultilineComment(s) => writeln!(self.buf, "/*{}*/", s)?,
|
||||
@ -79,7 +79,7 @@ impl<W: Write> PrettyPrinter<W> {
|
||||
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)?;
|
||||
}
|
||||
|
12
src/lib.rs
12
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<P: AsRef<Path> + Into<String>>(p: P) -> SassResult<StyleSheet> {
|
||||
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<W: Write>(&self, buf: W) -> io::Result<()> {
|
||||
pub fn pretty_print<W: Write>(&self, buf: W) -> SassResult<()> {
|
||||
PrettyPrinter::new(buf).pretty_print(self)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn pretty_print_selectors<W: Write>(&self, buf: W) -> io::Result<()> {
|
||||
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`
|
||||
pub fn print_as_css<W: Write>(self, buf: &mut W) -> io::Result<()> {
|
||||
pub fn print_as_css<W: Write>(self, buf: &mut W) -> SassResult<()> {
|
||||
Css::from_stylesheet(self).pretty_print(buf)
|
||||
}
|
||||
}
|
||||
|
@ -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")
|
||||
|
Loading…
x
Reference in New Issue
Block a user