diff --git a/src/lib.rs b/src/lib.rs index f99f084..889839b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,12 +11,11 @@ Spec progress as of 2020-04-21: ## Use as library ``` -use std::io::{BufWriter, stdout}; use grass::{SassResult, StyleSheet}; fn main() -> SassResult<()> { - let mut buf = BufWriter::new(stdout()); - StyleSheet::new("a { color: red; }".to_string(), &mut buf)?; + let sass = StyleSheet::new("a { b { color: &; } }".to_string())?; + assert_eq!(sass, "a b {\n color: a b;\n}\n"); Ok(()) } ``` @@ -82,7 +81,6 @@ grass input.scss #![cfg_attr(feature = "nightly", feature(track_caller))] use std::fmt::{self, Display}; use std::fs; -use std::io::Write; use std::iter::Iterator; use std::path::Path; @@ -207,17 +205,16 @@ impl StyleSheet { /// Write CSS to `buf`, constructed from a string /// /// ``` - /// use std::io::{BufWriter, stdout}; /// use grass::{SassResult, StyleSheet}; /// /// fn main() -> SassResult<()> { - /// let mut buf = BufWriter::new(stdout()); - /// StyleSheet::new("a { color: red; }".to_string(), &mut buf)?; + /// let sass = StyleSheet::new("a { b { color: red; } }".to_string())?; + /// assert_eq!(sass, "a b {\n color: red;\n}\n"); /// Ok(()) /// } /// ``` #[inline] - pub fn new(input: String, buf: &mut W) -> SassResult<()> { + pub fn new(input: String) -> SassResult { let mut map = CodeMap::new(); let file = map.add_file("stdin".into(), input); Css::from_stylesheet(StyleSheet( @@ -231,28 +228,22 @@ impl StyleSheet { .0, )) .map_err(|e| raw_to_parse_error(&map, e))? - .pretty_print(buf) - .map_err(|e| raw_to_parse_error(&map, e))?; - Ok(()) + .pretty_print() + .map_err(|e| raw_to_parse_error(&map, e)) } /// 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)?; + /// let sass = StyleSheet::from_path("input.scss")?; /// Ok(()) /// } /// ``` #[inline] - pub fn from_path + Into + Clone, W: Write>( - p: P, - buf: &mut W, - ) -> SassResult<()> { + pub fn from_path + Into + Clone>(p: P) -> 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( @@ -266,9 +257,8 @@ impl StyleSheet { .0, )) .map_err(|e| raw_to_parse_error(&map, e))? - .pretty_print(buf) - .map_err(|e| raw_to_parse_error(&map, e))?; - Ok(()) + .pretty_print() + .map_err(|e| raw_to_parse_error(&map, e)) } pub(crate) fn export_from_path + Into + Clone>( diff --git a/src/main.rs b/src/main.rs index 64d731f..b176032 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use std::io::{stdout, BufWriter}; +use std::io::{stdout, BufWriter, Write}; use clap::{App, Arg}; @@ -64,10 +64,16 @@ fn main() { let mut stdout = BufWriter::new(stdout()); if let Some(inputs) = matches.values_of("INPUT") { for name in inputs { - StyleSheet::from_path(name, &mut stdout).unwrap_or_else(|e| { - eprintln!("{}", e); - std::process::exit(1) - }); + stdout + .write_all( + StyleSheet::from_path(name) + .unwrap_or_else(|e| { + eprintln!("{}", e); + std::process::exit(1) + }) + .as_bytes(), + ) + .unwrap(); } } } diff --git a/src/output.rs b/src/output.rs index fe850d9..1b48cf7 100644 --- a/src/output.rs +++ b/src/output.rs @@ -123,14 +123,16 @@ impl Css { Ok(self) } - pub fn pretty_print(self, buf: &mut W) -> SassResult<()> { + pub fn pretty_print(self) -> SassResult { let mut string = Vec::new(); self._inner_pretty_print(&mut string, 0)?; if string.iter().any(|s| !s.is_ascii()) { - writeln!(buf, "@charset \"UTF-8\";")?; + return Ok(format!( + "@charset \"UTF-8\";\n{}", + String::from_utf8(string)? + )); } - write!(buf, "{}", String::from_utf8(string).unwrap())?; - Ok(()) + Ok(String::from_utf8(string)?) } fn _inner_pretty_print(self, buf: &mut Vec, nesting: usize) -> SassResult<()> { diff --git a/tests/imports.rs b/tests/imports.rs index f2b1d53..6ac5545 100644 --- a/tests/imports.rs +++ b/tests/imports.rs @@ -11,12 +11,11 @@ macro_rules! test_import { let mut f = Builder::new().rand_bytes(0).prefix("").suffix($name).tempfile_in("").unwrap(); write!(f, $content).unwrap(); )* - let mut buf = Vec::new(); - StyleSheet::new($input.to_string(), &mut buf) + let sass = StyleSheet::new($input.to_string()) .expect(concat!("failed to parse on ", $input)); assert_eq!( String::from($output), - String::from_utf8(buf).expect("produced invalid utf8") + sass ); } } diff --git a/tests/macros.rs b/tests/macros.rs index 25b6d04..87cf072 100644 --- a/tests/macros.rs +++ b/tests/macros.rs @@ -7,14 +7,11 @@ macro_rules! test { #[test] #[allow(non_snake_case)] fn $func() { - let mut buf = Vec::new(); - grass::StyleSheet::new($input.to_string(), &mut buf) + let sass = grass::StyleSheet::new($input.to_string()) .expect(concat!("failed to parse on ", $input)); - // .print_as_css() - // .expect(concat!("failed to pretty print on ", $input)); assert_eq!( String::from($input), - String::from_utf8(buf).expect("produced invalid utf8") + sass ); } }; @@ -23,12 +20,11 @@ macro_rules! test { #[test] #[allow(non_snake_case)] fn $func() { - let mut buf = Vec::new(); - grass::StyleSheet::new($input.to_string(), &mut buf) + let sass = grass::StyleSheet::new($input.to_string()) .expect(concat!("failed to parse on ", $input)); assert_eq!( String::from($output), - String::from_utf8(buf).expect("produced invalid utf8") + sass ); } }; @@ -43,8 +39,7 @@ macro_rules! error { #[test] #[allow(non_snake_case)] fn $func() { - let mut buf = Vec::new(); - match grass::StyleSheet::new($input.to_string(), &mut buf) { + match grass::StyleSheet::new($input.to_string()) { Ok(..) => panic!("did not fail"), Err(e) => assert_eq!($err, e.to_string() .chars()