Config

Struct Config 

Source
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

Source

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.

Source

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;
}"));
Source

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;
  }
}"));
Source

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.

Trait Implementations§

Source§

impl Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Config

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for Config

Source§

fn default() -> Config

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Config

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl PartialEq for Config

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Config

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Config

§

impl !RefUnwindSafe for Config

§

impl Send for Config

§

impl Sync for Config

§

impl Unpin for Config

§

impl !UnwindSafe for Config

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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

Source§

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> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,