BREAKING: consolidate StyleSheet::print_as_css

This commit is contained in:
ConnorSkees 2020-04-18 18:53:18 -04:00
parent 8955042359
commit 9a72d9714a

View File

@ -1,30 +1,32 @@
//! # grass /*! # grass
//! An implementation of the sass specification in pure rust. An implementation of the sass specification in pure rust.
//!
//! All functionality is currently exposed through [`StyleSheet`]. All functionality is currently exposed through [`StyleSheet`].
//!
//! Spec progress as of 2020-04-12: Spec progress as of 2020-04-12:
//!
//! | Passing | Failing | Total | | Passing | Failing | Total |
//! |---------|---------|-------| |---------|---------|-------|
//! | 2023 | 3070 | 5093 | | 2023 | 3070 | 5093 |
//!
//! ## Use as library ## Use as library
//! ``` ```
//! use std::io::{BufWriter, stdout}; use std::io::{BufWriter, stdout};
//! use grass::{SassResult, StyleSheet}; use grass::{SassResult, StyleSheet};
//!
//! fn main() -> SassResult<()> { fn main() -> SassResult<()> {
//! let mut buf = BufWriter::new(stdout()); let mut buf = BufWriter::new(stdout());
//! StyleSheet::from_path("input.scss")?.print_as_css(&mut buf) StyleSheet::new("a { color: red; }".to_string(), &mut buf)?;
//! } Ok(())
//! ``` }
//! ```
//! ## Use as binary
//! ```bash ## Use as binary
//! cargo install grass ```bash
//! grass input.scss cargo install grass
//! ``` grass input.scss
```
*/
#![warn( #![warn(
clippy::all, clippy::all,
@ -200,68 +202,7 @@ fn raw_to_parse_error(map: &CodeMap, err: SassError) -> SassError {
} }
impl StyleSheet { impl StyleSheet {
#[inline] /// Write CSS to `buf`, constructed from a string
pub fn new(input: String) -> SassResult<StyleSheet> {
let mut map = CodeMap::new();
let file = map.add_file("stdin".into(), input);
Ok(StyleSheet(
match (StyleSheetParser {
lexer: Lexer::new(&file).peekable(),
nesting: 0,
map: &map,
}
.parse_toplevel())
{
Ok(v) => v,
Err(e) => return Err(raw_to_parse_error(&map, e)),
}
.0,
))
}
#[inline]
pub fn from_path<P: AsRef<Path> + Into<String> + Clone>(p: P) -> SassResult<StyleSheet> {
let mut map = CodeMap::new();
let file = map.add_file(p.clone().into(), String::from_utf8(fs::read(p.as_ref())?)?);
Ok(StyleSheet(
match (StyleSheetParser {
lexer: Lexer::new(&file).peekable(),
nesting: 0,
map: &map,
}
.parse_toplevel())
{
Ok(v) => v,
Err(e) => return Err(raw_to_parse_error(&map, e)),
}
.0,
))
}
pub(crate) fn export_from_path<P: AsRef<Path> + Into<String> + Clone>(
p: P,
) -> SassResult<(Vec<Spanned<Stmt>>, Scope)> {
let mut map = CodeMap::new();
let file = map.add_file(p.clone().into(), String::from_utf8(fs::read(p.as_ref())?)?);
Ok(
match (StyleSheetParser {
lexer: Lexer::new(&file).peekable(),
nesting: 0,
map: &map,
}
.parse_toplevel())
{
Ok(v) => v,
Err(e) => return Err(raw_to_parse_error(&map, e)),
},
)
}
pub(crate) fn from_stmts(s: Vec<Spanned<Stmt>>) -> StyleSheet {
StyleSheet(s)
}
/// Write the internal representation as CSS to `buf`
/// ///
/// ``` /// ```
/// use std::io::{BufWriter, stdout}; /// use std::io::{BufWriter, stdout};
@ -269,12 +210,80 @@ impl StyleSheet {
/// ///
/// fn main() -> SassResult<()> { /// fn main() -> SassResult<()> {
/// let mut buf = BufWriter::new(stdout()); /// let mut buf = BufWriter::new(stdout());
/// StyleSheet::from_path("input.scss")?.print_as_css(&mut buf) /// StyleSheet::new("a { color: red; }".to_string(), &mut buf)?;
/// Ok(())
/// } /// }
/// ``` /// ```
#[inline] #[inline]
pub fn print_as_css<W: Write>(self, buf: &mut W) -> SassResult<()> { pub fn new<W: Write>(input: String, buf: &mut W) -> SassResult<()> {
Css::from_stylesheet(self)?.pretty_print(buf) let mut map = CodeMap::new();
let file = map.add_file("stdin".into(), input);
Css::from_stylesheet(StyleSheet(
StyleSheetParser {
lexer: Lexer::new(&file).peekable(),
nesting: 0,
map: &map,
}
.parse_toplevel()
.map_err(|e| raw_to_parse_error(&map, e))?
.0,
))
.map_err(|e| raw_to_parse_error(&map, e))?
.pretty_print(buf)
.map_err(|e| raw_to_parse_error(&map, e))?;
Ok(())
}
/// Write CSS to `buf`, constructed from a path
///
/// ```
/// use std::io::{BufWriter, stdout};
/// use grass::{SassResult, StyleSheet};
///
/// fn main() -> SassResult<()> {
/// let mut buf = BufWriter::new(stdout());
/// StyleSheet::from_path("input.scss", &mut buf)?;
/// Ok(())
/// }
/// ```
#[inline]
pub fn from_path<P: AsRef<Path> + Into<String> + Clone, W: Write>(
p: P,
buf: &mut W,
) -> SassResult<()> {
let mut map = CodeMap::new();
let file = map.add_file(p.clone().into(), String::from_utf8(fs::read(p.as_ref())?)?);
Css::from_stylesheet(StyleSheet(
StyleSheetParser {
lexer: Lexer::new(&file).peekable(),
nesting: 0,
map: &map,
}
.parse_toplevel()
.map_err(|e| raw_to_parse_error(&map, e))?
.0,
))
.map_err(|e| raw_to_parse_error(&map, e))?
.pretty_print(buf)
.map_err(|e| raw_to_parse_error(&map, e))?;
Ok(())
}
pub(crate) fn export_from_path<P: AsRef<Path> + Into<String> + Clone>(
p: P,
) -> SassResult<(Vec<Spanned<Stmt>>, Scope)> {
let mut map = CodeMap::new();
let file = map.add_file(p.clone().into(), String::from_utf8(fs::read(p.as_ref())?)?);
Ok(StyleSheetParser {
lexer: Lexer::new(&file).peekable(),
nesting: 0,
map: &map,
}
.parse_toplevel()?)
}
pub(crate) fn from_stmts(s: Vec<Spanned<Stmt>>) -> StyleSheet {
StyleSheet(s)
} }
} }
@ -459,7 +468,7 @@ impl<'a> StyleSheetParser<'a> {
AtRule::Warn(ref message) => self.warn(expr.span, message), AtRule::Warn(ref message) => self.warn(expr.span, message),
AtRule::Mixin(..) | AtRule::Function(..) => todo!(), AtRule::Mixin(..) | AtRule::Function(..) => todo!(),
AtRule::Charset => todo!(), AtRule::Charset => todo!(),
r @ AtRule::Unknown(..)=> stmts.push(Spanned { r @ AtRule::Unknown(..) => stmts.push(Spanned {
node: Stmt::AtRule(r), node: Stmt::AtRule(r),
span, span,
}), }),