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 = { 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);Based on Tailwind’s configuration.
Fields§
§safelist: SafelistSafelist configuration.
theme: ThemeTheme configuration.
preflight: PreflightPreflight configuration.
shortcuts: ShortcutsShortcuts configuration.
max_shortcut_depth: MaxShortcutDepthThe maximum depth at which shortcuts will be resolved.
extra: ExtraExtra fields configuration.
scanner: ScannerA custom scanner used to scan content.
This field is skipped when deserializing from a TOML file.
Implementations§
Source§impl Config
impl Config
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_type: VariantType,
)
pub fn register_variant<T: Into<Cow<'static, str>>>( &mut self, variant_name: T, variant_type: VariantType, )
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::VariantType};
use std::borrow::Cow;
let mut config = Config::default();
config.register_variant("headings", VariantType::WrapClass(Cow::Borrowed("& :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) {
--en-text-opacity: 1;
color: rgb(55 65 81 / var(--en-text-opacity));
}"));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.