Skip to main content

ListValues

Struct ListValues 

Source
pub struct ListValues<Str, ArrayStr, MapStr> {
    pub prop: PropertyName<Str, ArrayStr>,
    pub values: MapStr,
    pub namespace: Option<Str>,
    pub extra_rule_css: Option<ArrayStr>,
    pub extra_css: Option<MapStr>,
    pub extra_class: Option<Str>,
    pub extra_slash: Option<ExtraSlash<Str, MapStr>>,
}
Expand description

Define a plugin using a map between utility classes and the values of a single CSS property.

If you instead need to generate several CSS properties or to have more control on the CSS lines generated, use ListProperties.

§Example

use encre_css::{Config, generate};
use encre_css::prelude::build_plugin::*;

const PLUGIN: StaticPlugin = Plugin::ListValues(ListValues {
    prop: SingleProp("width"),
    values: map! {
        "w-fit" => "fit-content",
        "w-max" => "max-content",
        "w-min" => "min-content",
    },
    ..ListValues::default()
});

let mut config = Config::default();
config.register_plugin(&PLUGIN);

let generated = generate(["w-max"], &config);

assert!(generated.ends_with(r".w-max {
  width: max-content;
}"));

§Example in TOML

[[custom_plugins]]

[custom_plugins.ListValues]
prop = "width"

[custom_plugins.ListValues.values]
w-fit = "fit-content"
w-max = "max-content"
w-min = "min-content"

Fields§

§prop: PropertyName<Str, ArrayStr>

The CSS property name of the generated CSS rule.

It can be a single property using PropertyName::SingleProp or a list of properties using PropertyName::MultipleProps, in which case the value will be copied for all properties.

This field should be assigned separately after calling ListValues::default (or ListValues::default_dynamic).

§values: MapStr

The map between utility classes and the CSS values of the property ListValues::prop.

This field should be assigned separately after calling ListValues::default (or ListValues::default_dynamic).

§namespace: Option<Str>

Define a namespace (i.e a prefix) common to all utility classes declared in the map keys.

The last dash character (-) should be omitted due to the way the parsing of utility classes work (e.g in the example below, overflow is correct while overflow- is incorrect).

§Example
use encre_css::{Config, generate};
use encre_css::prelude::build_plugin::*;

const PLUGIN: StaticPlugin = Plugin::ListValues(ListValues {
    namespace: Some("w"),
    prop: SingleProp("width"),
    values: map! {
        "fit" => "fit-content",
        "max" => "max-content",
        "min" => "min-content",
    },
    ..ListValues::default()
});

let mut config = Config::default();
config.register_plugin(&PLUGIN);

let generated = generate(["w-max"], &config);

assert!(generated.ends_with(r".w-max {
  width: max-content;
}"));
§extra_rule_css: Option<ArrayStr>

Add one or several extra CSS line(s) inside the CSS rule generated for the utility class.

This field takes an array which represents the CSS lines that will be properly indented and added, one after another, in the order they are defined, to the CSS rule.

§Example
use encre_css::{Config, generate};
use encre_css::prelude::build_plugin::*;

const PLUGIN: StaticPlugin = Plugin::Spacing(Spacing {
    namespace: "custom-translate-x",
    prop: SingleProp("--translate-x"),
    extra_rule_css: Some(&["transform: translate(var(--translate-x), 12px);"]),
    ..Spacing::default()
});

let mut config = Config::default();
config.register_plugin(&PLUGIN);

let generated = generate(["custom-translate-x-8"], &config);

assert!(generated.ends_with(".custom-translate-x-8 {
  --translate-x: 2rem;
  transform: translate(var(--translate-x), 12px);
}"));
§Example in TOML
[[custom_plugins]]

[custom_plugins.Spacing]
namespace = "custom-translate-x"
prop = "--translate-x"
extra_rule_css = ["transform: translate(var(--translate-x), 12px);"]
§extra_css: Option<MapStr>

Add one or several extra CSS line(s) outside the CSS rule generated for the utility class.

The argument is a map which allows choosing the added CSS based on the modifier value.

§Example
use encre_css::{Config, generate};
use encre_css::prelude::build_plugin::*;

const SPIN_ANIMATION: &str = "@keyframes anim-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}\n\n";

const FADE_IN_ANIMATION: &str = "@keyframes anim-fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}\n\n";

const PLUGIN: StaticPlugin = Plugin::ListValues(ListValues {
    prop: SingleProp("animation"),
    values: map! {
        "custom-animate-spin" => "anim-spin",
        "custom-animate-fade-in" => "anim-fade-in",
    },
    extra_css: Some(map! {
        "custom-animate-spin" => SPIN_ANIMATION,
        "custom-animate-fade-in" => FADE_IN_ANIMATION,
    }),
    ..ListValues::default()
});

let mut config = Config::default();
config.register_plugin(&PLUGIN);

let generated = generate(["custom-animate-spin"], &config);

assert!(generated.ends_with("@keyframes anim-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

.custom-animate-spin {
  animation: anim-spin;
}"));
§Example in TOML
[[custom_plugins]]

[custom_plugins.ListValues]
prop = "animation"

[custom_plugins.ListValues.values]
custom-animate-spin = "anim-spin"
custom-animate-fade-in = "anim-fade-in"

[custom_plugins.ListValues.extra_css]
custom-animate-spin = """@keyframes anim-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}\n\n"""
custom-animate-fade-in = """@keyframes anim-fade-in {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}\n\n"""
§extra_class: Option<Str>

Add a suffix string to the class selector of the generated CSS rule.

§Example
use encre_css::{Config, generate};
use encre_css::prelude::build_plugin::*;

pub(crate) const PLUGIN: StaticPlugin = Plugin::Spacing(Spacing {
    namespace: "custom-divide",
    prop: SingleProp("margin-inline"),
    extra_class: Some(" > :not(:last-child)"),
    ..Spacing::default()
});

let mut config = Config::default();
config.register_plugin(&PLUGIN);

let generated = generate(["custom-divide-2"], &config);

assert!(generated.ends_with(".custom-divide-2 > :not(:last-child) {
  margin-inline: 0.5rem;
}"));
§Example in TOML
[[custom_plugins]]

[custom_plugins.Spacing]
namespace = "custom-divide"
prop = "margin-inline"
extra_class = " > :not(:last-child)"
§extra_slash: Option<ExtraSlash<Str, MapStr>>

Add support for an extra slash (/) followed by a value for the utility classes handled by this plugin.

In practice, you give it a map with the accepted values after the slash as keys and the values you need to insert into the CSS value as values, as well as a default value in case no slash is found. Then, you add the placeholder {/} to your CSS values (or templates) and it will be replaced with the specified value.

§Example
use encre_css::{Config, generate};
use encre_css::prelude::build_plugin::*;

pub(crate) const PLUGIN: StaticPlugin = Plugin::ListValues(ListValues {
    prop: SingleProp("background-image"),
    values: map! {
        "custom-bg-img" => "linear-gradient(to top in {/}, #00aaff, #00ffaa)",
    },
    extra_slash: Some(ExtraSlash {
        values: map! {
            "increasing" => "oklch increasing hue",
            "oklab" => "oklab",
        },
        default: "oklab",
    }),
    ..ListValues::default()
});

let mut config = Config::default();
config.register_plugin(&PLUGIN);

let generated = generate(["custom-bg-img", "custom-bg-img/increasing"], &config);

assert!(generated.ends_with(".custom-bg-img {
  background-image: linear-gradient(to top in oklab, #00aaff, #00ffaa);
}

.custom-bg-img\\/increasing {
  background-image: linear-gradient(to top in oklch increasing hue, #00aaff, #00ffaa);
}"));
§Example in TOML
[[custom_plugins]]

[custom_plugins.ListValues]
prop = "background-image"

[custom_plugins.ListValues.values]
custom-bg-img = "linear-gradient(to top in {/}, #00aaff, #00ffaa)"

[custom_plugins.ListValues.extra_slash]
default = "oklab"

[custom_plugins.ListValues.extra_slash.values]
increasing = "oklch increasing hue"
oklab = "oklab"

Implementations§

Source§

impl<ArrayStr> ListValues<&'static str, ArrayStr, Map<&'static str, &'static str>>

Source

pub const fn default() -> Self

Make a default ListValues plugin kind.

All required fields are initialized with empty values and optional fields are initialized with None.

You should at least set ListValues::prop and ListValues::values after calling this function.

This function is intended to be used as an automatic filler for default values using the struct update syntax.

The difference with ListValues::default_dynamic is that this function can only be used to build a plugin using static structures like &[]s, &'static strs.

§Example
use encre_css::prelude::build_plugin::*;

const PLUGIN: StaticPlugin = Plugin::ListValues(ListValues {
    prop: SingleProp("width"),
    values: map! {
        "w-fit" => "fit-content",
        "w-max" => "max-content",
        "w-min" => "min-content",
    },
    ..ListValues::default()
});
Source§

impl<ArrayStr> ListValues<String, ArrayStr, HashMap<String, String>>

Source

pub fn default_dynamic() -> Self

Make a default ListValues plugin kind.

All required fields are initialized with empty values and optional fields are initialized with None.

You should at least set ListValues::prop and ListValues::values after calling this function.

This function is intended to be used as an automatic filler for default values using the struct update syntax.

The difference with ListProperties::default is that this function can only be used to build a plugin using heap-allocated structures like Strings, Vecs.

§Example
use std::collections::HashMap;
use encre_css::prelude::build_plugin::*;

fn main() {
    let values = HashMap::from_iter(
        ["fit", "max", "min"].iter().map(|v| {
            (format!("w-{v}"), format!("width: {v}-content;"))
        })
    );

    // Note: the DynamicPlugin type hint is required to help the compiler
    // find the concrete types of type parameters
    let _plugin: DynamicPlugin = Plugin::ListValues(ListValues {
        prop: SingleProp("width".to_string()),
        values,
        ..ListValues::default_dynamic()
    });
}

This example is equivalent to the one of ListValues::default.

Trait Implementations§

Source§

impl<Str: Clone, ArrayStr: Clone, MapStr: Clone> Clone for ListValues<Str, ArrayStr, MapStr>

Source§

fn clone(&self) -> ListValues<Str, ArrayStr, MapStr>

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<Str: Debug, ArrayStr: Debug, MapStr: Debug> Debug for ListValues<Str, ArrayStr, MapStr>

Source§

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

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

impl<'de, Str, ArrayStr, MapStr> Deserialize<'de> for ListValues<Str, ArrayStr, MapStr>
where Str: Deserialize<'de>, ArrayStr: Deserialize<'de>, MapStr: Deserialize<'de>,

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<Str, ArrayStr, MapStr> Serialize for ListValues<Str, ArrayStr, MapStr>
where Str: Serialize, ArrayStr: Serialize, MapStr: Serialize,

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<Str, ArrayStr, MapStr> Freeze for ListValues<Str, ArrayStr, MapStr>
where PropertyName<Str, ArrayStr>: Freeze, MapStr: Freeze, Option<Str>: Freeze, Option<ArrayStr>: Freeze, Option<MapStr>: Freeze, Option<ExtraSlash<Str, MapStr>>: Freeze,

§

impl<Str, ArrayStr, MapStr> RefUnwindSafe for ListValues<Str, ArrayStr, MapStr>
where PropertyName<Str, ArrayStr>: RefUnwindSafe, MapStr: RefUnwindSafe, Option<Str>: RefUnwindSafe, Option<ArrayStr>: RefUnwindSafe, Option<MapStr>: RefUnwindSafe, Option<ExtraSlash<Str, MapStr>>: RefUnwindSafe,

§

impl<Str, ArrayStr, MapStr> Send for ListValues<Str, ArrayStr, MapStr>
where PropertyName<Str, ArrayStr>: Send, MapStr: Send, Option<Str>: Send, Option<ArrayStr>: Send, Option<MapStr>: Send, Option<ExtraSlash<Str, MapStr>>: Send,

§

impl<Str, ArrayStr, MapStr> Sync for ListValues<Str, ArrayStr, MapStr>
where PropertyName<Str, ArrayStr>: Sync, MapStr: Sync, Option<Str>: Sync, Option<ArrayStr>: Sync, Option<MapStr>: Sync, Option<ExtraSlash<Str, MapStr>>: Sync,

§

impl<Str, ArrayStr, MapStr> Unpin for ListValues<Str, ArrayStr, MapStr>
where PropertyName<Str, ArrayStr>: Unpin, MapStr: Unpin, Option<Str>: Unpin, Option<ArrayStr>: Unpin, Option<MapStr>: Unpin, Option<ExtraSlash<Str, MapStr>>: Unpin,

§

impl<Str, ArrayStr, MapStr> UnsafeUnpin for ListValues<Str, ArrayStr, MapStr>
where PropertyName<Str, ArrayStr>: UnsafeUnpin, MapStr: UnsafeUnpin, Option<Str>: UnsafeUnpin, Option<ArrayStr>: UnsafeUnpin, Option<MapStr>: UnsafeUnpin, Option<ExtraSlash<Str, MapStr>>: UnsafeUnpin,

§

impl<Str, ArrayStr, MapStr> UnwindSafe for ListValues<Str, ArrayStr, MapStr>
where PropertyName<Str, ArrayStr>: UnwindSafe, MapStr: UnwindSafe, Option<Str>: UnwindSafe, Option<ArrayStr>: UnwindSafe, Option<MapStr>: UnwindSafe, Option<ExtraSlash<Str, MapStr>>: UnwindSafe,

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

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 = !

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.