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
|
//! # 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::fmt;
|
||||||
use std::io::{self, Write};
|
use std::io::Write;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum Toplevel {
|
pub enum Toplevel {
|
||||||
@ -94,7 +94,7 @@ impl Css {
|
|||||||
self
|
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 {
|
for block in self.blocks {
|
||||||
match block {
|
match block {
|
||||||
Toplevel::RuleSet(selector, styles) => {
|
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::error::Error;
|
||||||
use std::fmt::{self, Display};
|
use std::fmt::{self, Display};
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::string::FromUtf8Error;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct SassError {
|
pub struct SassError {
|
||||||
@ -16,13 +17,6 @@ impl SassError {
|
|||||||
pos,
|
pos,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn unexpected_eof(pos: Pos) -> Self {
|
|
||||||
SassError {
|
|
||||||
message: String::from("unexpected eof"),
|
|
||||||
pos,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for SassError {
|
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 {
|
impl Error for SassError {
|
||||||
fn description(&self) -> &'static str {
|
fn description(&self) -> &'static str {
|
||||||
"SASS parsing error"
|
"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> {
|
pub(crate) struct PrettyPrinter<W: Write> {
|
||||||
buf: W,
|
buf: W,
|
||||||
@ -14,7 +14,7 @@ impl<W: Write> PrettyPrinter<W> {
|
|||||||
|
|
||||||
/// Pretty print `crate::Stmt`
|
/// Pretty print `crate::Stmt`
|
||||||
/// Throws away super selectors and variables
|
/// 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>();
|
let padding = vec![' '; self.scope * 2].iter().collect::<String>();
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::MultilineComment(s) => writeln!(self.buf, "{}/*{}*/", padding, s)?,
|
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
|
/// The result should be an exact copy of the SCSS input
|
||||||
/// Empty rules are included
|
/// 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 {
|
for rule in &s.0 {
|
||||||
self.pretty_print_stmt(rule)?;
|
self.pretty_print_stmt(rule)?;
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ impl<W: Write> PrettyPrinter<W> {
|
|||||||
|
|
||||||
/// Pretty print `crate::Stmt`
|
/// Pretty print `crate::Stmt`
|
||||||
/// Keeps super selectors
|
/// 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>();
|
let padding = vec![' '; self.scope * 2].iter().collect::<String>();
|
||||||
match stmt {
|
match stmt {
|
||||||
Stmt::MultilineComment(s) => writeln!(self.buf, "/*{}*/", s)?,
|
Stmt::MultilineComment(s) => writeln!(self.buf, "/*{}*/", s)?,
|
||||||
@ -79,7 +79,7 @@ impl<W: Write> PrettyPrinter<W> {
|
|||||||
pub(crate) fn pretty_print_preserve_super_selectors(
|
pub(crate) fn pretty_print_preserve_super_selectors(
|
||||||
&mut self,
|
&mut self,
|
||||||
s: &StyleSheet,
|
s: &StyleSheet,
|
||||||
) -> io::Result<()> {
|
) -> SassResult<()> {
|
||||||
for rule in &s.0 {
|
for rule in &s.0 {
|
||||||
self.pretty_print_stmt_preserve_super_selectors(rule)?;
|
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
|
// 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::{self, Write};
|
use std::io::Write;
|
||||||
use std::iter::{Iterator, Peekable};
|
use std::iter::{Iterator, Peekable};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
@ -209,10 +209,12 @@ impl StyleSheet {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_path<P: AsRef<Path> + Into<String>>(p: P) -> SassResult<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(
|
Ok(StyleSheet(
|
||||||
StyleSheetParser {
|
StyleSheetParser {
|
||||||
global_scope: Scope::new(),
|
global_scope: Scope::new(),
|
||||||
lexer: Lexer::new(&fs::read_to_string(p.as_ref())?).peekable(),
|
lexer: Lexer::new(&s).peekable(),
|
||||||
rules: Vec::new(),
|
rules: Vec::new(),
|
||||||
scope: 0,
|
scope: 0,
|
||||||
file: p.into(),
|
file: p.into(),
|
||||||
@ -241,17 +243,17 @@ impl StyleSheet {
|
|||||||
/// to pure CSS
|
/// to pure CSS
|
||||||
///
|
///
|
||||||
/// Used mainly in debugging, but can at times be useful
|
/// 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)
|
PrettyPrinter::new(buf).pretty_print(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[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)
|
PrettyPrinter::new(buf).pretty_print_preserve_super_selectors(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Write the internal representation as CSS to `buf`
|
/// 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)
|
Css::from_stylesheet(self).pretty_print(buf)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
use clap::{App, Arg};
|
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")
|
let matches = App::new("grass")
|
||||||
.version(env!("CARGO_PKG_VERSION"))
|
.version(env!("CARGO_PKG_VERSION"))
|
||||||
.about("SCSS Compiler in rust")
|
.about("SCSS Compiler in rust")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user