Struct rsass::input::Context

source ·
pub struct Context<Loader> { /* private fields */ }
Expand description

Utility keeping track of loading files.

The context is generic over the Loader. FsContext and CargoContext are type aliases for Context where the loader is a FsLoader or CargoLoader, respectively.

§Examples

The Context here is a FsContext. Input is usually a scss file.

let context = FsContext::for_cwd()
    .with_format(Format { style: Style::Compressed, precision: 2 });
let scss_input = SourceFile::scss_bytes(
    "$gap: 4em / 3;
    \np {\
    \n    margin: $gap 0;
    \n}\n",
    SourceName::root("-")
);
assert_eq!(
    context.transform(scss_input)?,
    b"p{margin:1.33em 0}\n"
);

This method can also be used as a plain css compression.

let css_input = SourceFile::css_bytes(
    "p {\
    \n    margin: 1.333333333em 0;\
    \n}\n",
    SourceName::root("-")
);
assert_eq!(
    context.transform(css_input)?,
    b"p{margin:1.33em 0}\n"
);

Implementations§

source§

impl Context<FsLoader>

source

pub fn for_cwd() -> Self

Create a new Context, loading files based on the current working directory.

source

pub fn for_path(path: &Path) -> Result<(Self, SourceFile), LoadError>

Create a new Context and load a file.

The directory part of path is used as a base directory for the loader.

source

pub fn push_path(&mut self, path: &Path)

Add a path to search for files.

source§

impl Context<CargoLoader>

source

pub fn for_crate() -> Result<Self, LoadError>

Create a new Context, loading files based in the manifest directory of the current crate.

Relative paths will be resolved from the directory containing the manifest of your package. This assumes the program is called by cargo as a build script, so the CARGO_MANIFEST_DIR environment variable is set.

source

pub fn for_path(path: &Path) -> Result<(Self, SourceFile), LoadError>

Create a new Context and load a file.

The directory part of path is used as a base directory for the loader. If path is relative, it will be resolved from the directory containing the manifest of your package.

source

pub fn push_path(&mut self, path: &Path) -> Result<(), LoadError>

Add a path to search for files.

If path is relative, it will be resolved from the directory containing the manifest of your package.

source§

impl<AnyLoader: Loader> Context<AnyLoader>

source

pub fn for_loader(loader: AnyLoader) -> Self

Create a new Context for a given file Loader.

source

pub fn transform(self, file: SourceFile) -> Result<Vec<u8>, Error>

Transform some input source to css.

The css output is returned as a raw byte vector.

source

pub fn with_format(self, format: Format) -> Self

Set the output format for this context.

Note that this resets the scope. If you use both with_format and get_scope, you need to call with_format before get_scope.

source

pub fn get_scope(&mut self) -> ScopeRef

Get the scope for this context.

A ScopeRef dereferences to a crate::Scope, which uses internal mutability. So this can be used for predefining variables, functions, mixins, or modules before transforming some scss input.

Note that if you use both with_format and get_scope, you need to call with_format before get_scope.

source

pub fn find_file( &mut self, url: &str, from: SourceKind ) -> Result<Option<SourceFile>, Error>

Find a file.

This method handles sass file name resolution, but delegates the actual checking for existing files to the Loader.

Given a url like my/util, this method will check for my/util, my/util.scss, my/_util.scss, my/util/index.scss, and my/util/_index.scss. The variants that are not a directory index will also be checked for .css files (and in the future it may also check for .sass files if rsass suports that format).

If from indicates that the loading is for an @import rule, some extra file names are checked.

The Context keeps track of “locked” files (files currently beeing parsed or transformed into css). The source file returned from this function is locked, so the caller of this method need to call Self::unlock_loading after handling it.

source

pub fn unlock_loading(&mut self, file: &SourceFile)

Unlock a file that is locked for input processing.

The lock exists to break circular dependency chains. Each file that is locked (by Self::find_file) needs to be unlocked when processing of it is done.

Trait Implementations§

source§

impl<T: Debug> Debug for Context<T>

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<Loader> Freeze for Context<Loader>
where Loader: Freeze,

§

impl<Loader> RefUnwindSafe for Context<Loader>
where Loader: RefUnwindSafe,

§

impl<Loader> Send for Context<Loader>
where Loader: Send,

§

impl<Loader> Sync for Context<Loader>
where Loader: Sync,

§

impl<Loader> Unpin for Context<Loader>
where Loader: Unpin,

§

impl<Loader> UnwindSafe for Context<Loader>
where Loader: UnwindSafe,

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> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, U> TryFrom<U> for T
where U: Into<T>,

§

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>,

§

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.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more