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

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

Create a new Context and load a file.

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

Add a path to search for files.

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.

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.

Add a path to search for files.

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

Create a new Context for a given file Loader.

Transform some input source to css.

The css output is returned as a raw byte vector.

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.

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.

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.

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

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

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

Calls U::from(self).

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

The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more