| 1 | // Take a look at the license at the top of the repository in the LICENSE file. |
| 2 | |
| 3 | use std::{fmt, io}; |
| 4 | |
| 5 | mod token; |
| 6 | |
| 7 | /// Minifies a given CSS source code. |
| 8 | /// |
| 9 | /// # Example |
| 10 | /// |
| 11 | /// ```rust |
| 12 | /// use minifier::css::minify; |
| 13 | /// |
| 14 | /// let css = r#" |
| 15 | /// .foo > p { |
| 16 | /// color: red; |
| 17 | /// }"# .into(); |
| 18 | /// let css_minified = minify(css).expect("minification failed" ); |
| 19 | /// assert_eq!(&css_minified.to_string(), ".foo>p{color:red;}" ); |
| 20 | /// ``` |
| 21 | pub fn minify(content: &str) -> Result<Minified<'_>, &'static str> { |
| 22 | token::tokenize(content).map(op:Minified) |
| 23 | } |
| 24 | |
| 25 | pub struct Minified<'a>(token::Tokens<'a>); |
| 26 | |
| 27 | impl Minified<'_> { |
| 28 | pub fn write<W: io::Write>(self, w: W) -> io::Result<()> { |
| 29 | self.0.write(w) |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | impl fmt::Display for Minified<'_> { |
| 34 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 35 | self.0.fmt(f) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | #[cfg (test)] |
| 40 | mod tests; |
| 41 | |