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
sourceimpl Context<FsLoader>
impl Context<FsLoader>
sourceimpl Context<CargoLoader>
impl Context<CargoLoader>
sourcepub fn for_crate() -> Result<Self, LoadError>
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.
sourceimpl<AnyLoader: Loader> Context<AnyLoader>
impl<AnyLoader: Loader> Context<AnyLoader>
sourcepub fn for_loader(loader: AnyLoader) -> Self
pub fn for_loader(loader: AnyLoader) -> Self
Create a new Context
for a given file Loader
.
sourcepub fn transform(self, file: SourceFile) -> Result<Vec<u8>, Error>
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.
sourcepub fn with_format(self, format: Format) -> Self
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
.
sourcepub fn get_scope(&mut self) -> ScopeRef
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
.
sourcepub fn find_file(
&mut self,
url: &str,
from: SourceKind
) -> Result<Option<SourceFile>, Error>
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.
sourcepub fn unlock_loading(&mut self, file: &SourceFile)
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.