pub struct Config {
pub safelist: Safelist,
pub theme: Theme,
pub preflight: Preflight,
pub shortcuts: Shortcuts,
pub max_shortcut_depth: MaxShortcutDepth,
pub extra: Extra,
pub scanner: Scanner,
/* private fields */
}
Expand description
The configuration of the CSS generation done in the generate
function.
You can create a configuration using one of the ways listed below:
- It can be the default one:
use encre_css::Config;
let config = Config::default();
let _generated = encre_css::generate([], &config);
- It can be a customized one:
use encre_css::Config;
let mut config = Config::default();
config.theme.colors.add("flashy", "#ff2d20");
let _generated = encre_css::generate([], &config);
- It can be loaded from a TOML file:
# encre-css.toml
[theme]
dark_mode = { class = ".dark" }
screens = { 3xl = "1600px", lg = "2000px" }
[theme.colors]
primary = "#e5186a"
yellow-400 = "#ffef0e"
use encre_css::Config;
let config = Config::from_file("encre-css.toml")?;
let _generated = encre_css::generate([], &config);
Based on Tailwind v3’s configuration.
Fields§
§safelist: Safelist
Safelist configuration.
theme: Theme
Theme configuration.
preflight: Preflight
Preflight configuration.
shortcuts: Shortcuts
Shortcuts configuration.
max_shortcut_depth: MaxShortcutDepth
The maximum depth at which shortcuts will be resolved.
extra: Extra
Extra fields configuration.
scanner: Scanner
A custom scanner used to scan content.
This field is skipped when deserializing from a TOML file.
Implementations§
Source§impl Config
impl Config
Sourcepub fn last_variant_order(&self) -> usize
pub fn last_variant_order(&self) -> usize
Returns the order of the last variant which can be used when defining a new variant which must be generated after all the other variants.
Note that if you are not the maintainer of a crate providing variants, you can ignore this function.
Sourcepub fn register_plugin<T: Into<Cow<'static, str>>>(
&mut self,
namespace: T,
plugin: &'static (dyn Plugin + Send + Sync),
)
pub fn register_plugin<T: Into<Cow<'static, str>>>( &mut self, namespace: T, plugin: &'static (dyn Plugin + Send + Sync), )
Register a custom plugin which will be used during CSS generation.
Note that if you are not the maintainer of a crate providing plugins, you can ignore this
function, see crate::plugins
.
§Example
use encre_css::{Config, prelude::build_plugin::*};
#[derive(Debug)]
struct Prose;
impl Plugin for Prose {
fn can_handle(&self, context: ContextCanHandle) -> bool {
matches!(context.modifier, Modifier::Builtin { value: "" | "invert", .. })
}
fn handle(&self, context: &mut ContextHandle) {
if let Modifier::Builtin { value, .. } = context.modifier {
match *value {
"" => context.buffer.line("color: #333;"),
"invert" => context.buffer.line("color: #eee;"),
_ => unreachable!(),
}
}
}
}
let mut config = Config::default();
config.register_plugin("prose", &Prose);
let generated = encre_css::generate(
["prose", "prose-invert"],
&config,
);
assert!(generated.ends_with(".prose {
color: #333;
}
.prose-invert {
color: #eee;
}"));
Sourcepub fn register_variant<T: Into<Cow<'static, str>>>(
&mut self,
variant_name: T,
variant: Variant<'static>,
)
pub fn register_variant<T: Into<Cow<'static, str>>>( &mut self, variant_name: T, variant: Variant<'static>, )
Register a custom variant which will be used during CSS generation.
Note that if you are not the maintainer of a crate providing variants, you can ignore this function.
§Example
use encre_css::{Config, selector::Variant};
use std::borrow::Cow;
let mut config = Config::default();
config.register_variant(
"headings",
// Insert the classes having this variant after all the other variants
Variant::new(config.last_variant_order(), "& :where(h1, h2, h3, h4, h5, h6)")
);
let generated = encre_css::generate(
["headings:text-gray-700"],
&config,
);
assert!(generated.ends_with(".headings\\:text-gray-700 :where(h1, h2, h3, h4, h5, h6) {
color: oklch(37.3% .034 259.733);
}"));
You can also make a prefixed variant, that is a variant which has a prefix and an arbitrary
value delimited by square brackets. When defining this kind of variant, you need to call
Variant::with_prefixed
and to insert the placeholder {}
in the variant
template, it will be replaced with the given arbitrary value.
§Example
use encre_css::{Config, selector::Variant};
use std::borrow::Cow;
let mut config = Config::default();
config.register_variant(
"media",
// Insert the classes having this variant after all the other variants
Variant::new(config.last_variant_order(), "@media {}").with_prefixed()
);
let generated = encre_css::generate(
["media-[print]:flex"],
&config,
);
assert!(generated.ends_with(r"@media print {
.media-\[print\]\:flex {
display: flex;
}
}"));
Sourcepub fn from_file<T: AsRef<Path>>(path: T) -> Result<Self>
pub fn from_file<T: AsRef<Path>>(path: T) -> Result<Self>
Deserialize the content of a TOML file to get the configuration.
§Example
# encre-css.toml
[theme]
dark_mode = { type = "class", class = ".dark" }
screens = { 3xl = "1600px", lg = "2000px" }
[theme.colors]
primary = "#e5186a"
yellow-400 = "#ffef0e"
use encre_css::Config;
let config = Config::from_file("encre-css.toml")?;
let _generated = encre_css::generate([], &config);
See Config
for other ways of creating a configuration.
§Errors
Returns Error::ConfigFileNotFound
if the given file does not exist.
Returns Error::ConfigParsing
if the given file could not be parsed.