Css

Struct Css 

Source
pub struct Css<'a> { /* private fields */ }
Expand description

§CSS Stylesheet.

This struct is used to parse and validate CSS/SCSS content, and optionally minify it for production use.

Note: CSS modules are not supported, however SCSS imports are.

§Use

If you already have the CSS gathered into a string, you can instantiate this object using From<&str> or From<String>.

More often, you’ll want to load the contents directly from a file, which can be done using TryFrom<&Path>. If the file extension is .sass or .scss, the contents will be run through the SCSS interpreter to generate the CSS, otherwise if the file extension is .css, the file contents will be used directly.

Note: SCSS is only supported when read from a file.

Once initialized, you can convert your Css object into an owned string (i.e. browser-ready CSS) using the Css::take or Css::minified methods.

§Examples

use guff_css::Css;
use std::path::Path;

// Load, validate, and return CSS from a file.
let css = Css::try_from(Path::new("../skel/style.css"))
    .unwrap()
    .take()
    .unwrap();

// The same thing, but starting from SCSS, and minifying!
let css = Css::try_from(Path::new("../skel/style.scss"))
    .unwrap()
    .minified(None)
    .unwrap();

Implementations§

Source§

impl Css<'_>

Source

pub fn minified(self, browsers: Option<Browsers>) -> Result<String, GuffError>

§Minify.

This works just like Css::take, except the CSS is aggressively minified before being returned.

Speaking of aggression, the latest and greatest CSS features are sometimes leveraged for additional space savings. The browsers argument can be used to override this behavior, maintaining backward compatibility with the browsers specified.

§Errors

If the CSS cannot be parsed or errors occur during minification, an error will be returned.

Source

pub fn take(self) -> Result<String, GuffError>

§Take.

Validate and return the CSS as an owned string.

This will trim whitespace and UTF-8 BOM markers, but otherwise leave the contents as-were.

§Errors

If the CSS cannot be parsed, an error will be returned.

Trait Implementations§

Source§

impl<'a> Debug for Css<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&str> for Css<'_>

Source§

fn from(src: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for Css<'_>

Source§

fn from(css: String) -> Self

Converts to this type from the input type.
Source§

impl<'a> TryFrom<&'a Path> for Css<'a>

Source§

fn try_from(src: &'a Path) -> Result<Self, Self::Error>

§From File.

This will attempt to load the contents of the given file, using its extension as a filetype hint.

If the file ends with .sass or .scss, it is assumed to be SCSS, and will be run through the SCSS compiler to produce valid CSS.

If the file ends with .css, the contents are presumed to already be valid CSS.

§Errors

If the file is missing, unreadable, or does not end with a CSS or SCSS extension, an error will be returned. For SCSS, processing errors will be bubbled up if encountered.

Source§

type Error = GuffError

The type returned in the event of a conversion error.

Auto Trait Implementations§

§

impl<'a> Freeze for Css<'a>

§

impl<'a> RefUnwindSafe for Css<'a>

§

impl<'a> Send for Css<'a>

§

impl<'a> Sync for Css<'a>

§

impl<'a> Unpin for Css<'a>

§

impl<'a> UnwindSafe for Css<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.