Skip to main content

ArbitraryDisambiguate

Struct ArbitraryDisambiguate 

Source
pub struct ArbitraryDisambiguate<ArrayMatched> {
    pub matched: ArrayMatched,
    pub separation: ArbitraryDisambiguateSeparation,
}
Expand description

Define a CSS value type which is used to disambiguate the use of the arbitrary plugin.

When two Arbitrary plugins share the same namespace (e.g font_family and font_weight share the same font namespace), you need to fill Arbitrary::disambiguate with an ArbitraryDisambiguate structure.

This structure contains a CSS type which is compared to the CSS type inferred when parsing. If both types match, the arbitrary plugin will be used (e.g font-[Roboto] will have an inferred type of CssType::FontFamilyName which corresponds to font_family because it sets matched: &[CssType::FontFamilyName]).

When an arbitrary value does not have a clear type (e.g font-[var(--font-weight)] or font-[initial]), the user needs to specify an arbitrary hint suffixed by :. This hint string will be parsed and compared with the CSS type of this structure, just like the inferred type.

If Arbitrary::disambiguate is left with the default value, it will always match the value. This behavior can be used to define a plugin which is used by-default when no other plugins match the type of the CSS value, but it should be registered last otherwise all values will be handled by it.

§Example

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

pub(crate) const PLUGIN_ARBITRARY_COLOR: StaticPlugin = Plugin::Arbitrary(Arbitrary {
    namespace: "custom-stroke",
    prop: SingleProp("stroke"),
    disambiguate: Some(ArbitraryDisambiguate {
        matched: &[CssType::Color],
        separation: ArbitraryDisambiguateSeparation::None,
    }),
    ..Arbitrary::default()
});

const PLUGIN_ARBITRARY_WIDTH: StaticPlugin = Plugin::Arbitrary(Arbitrary {
    namespace: "custom-stroke",
    prop: SingleProp("stroke-width"),
    disambiguate: Some(ArbitraryDisambiguate {
        matched: &[CssType::Length, CssType::Percentage, CssType::LineWidth, CssType::Number],
        separation: ArbitraryDisambiguateSeparation::None,
    }),
    ..Arbitrary::default()
});

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

let generated = generate([
    "custom-stroke-[red]", "custom-stroke-[color:var(--stroke-color)]",
    "custom-stroke-[2]", "custom-stroke-[length:var(--stroke-width)]",
], &config);

assert!(generated.ends_with(r".custom-stroke-\[color\:var\(--stroke-color\)\] {
  stroke: var(--stroke-color);
}

.custom-stroke-\[red\] {
  stroke: red;
}

.custom-stroke-\[2\] {
  stroke-width: 2;
}

.custom-stroke-\[length\:var\(--stroke-width\)\] {
  stroke-width: var(--stroke-width);
}"));

§Example in TOML

[[custom_plugins]]

[custom_plugins.Arbitrary]
namespace = "custom-stroke"
prop = "stroke"

[custom_plugins.Arbitrary.disambiguate]
matched = ["Color"]
separation = "None"

[[custom_plugins]]

[custom_plugins.Arbitrary]
namespace = "custom-stroke"
prop = "stroke-width"

[custom_plugins.Arbitrary.disambiguate]
matched = ["Length", "Percentage", "LineWidth", "Number"]
separation = "None"

Fields§

§matched: ArrayMatched

The list of CSS types which are accepted as arbitrary value by this plugin.

§separation: ArbitraryDisambiguateSeparation

Define how values are specified inside the CSS value.

In practice, you can check the values accepted by the CSS property and set this field to

Trait Implementations§

Source§

impl<ArrayMatched: Clone> Clone for ArbitraryDisambiguate<ArrayMatched>

Source§

fn clone(&self) -> ArbitraryDisambiguate<ArrayMatched>

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<ArrayMatched: Debug> Debug for ArbitraryDisambiguate<ArrayMatched>

Source§

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

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

impl<'de, ArrayMatched> Deserialize<'de> for ArbitraryDisambiguate<ArrayMatched>
where ArrayMatched: 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<ArrayMatched> Serialize for ArbitraryDisambiguate<ArrayMatched>
where ArrayMatched: 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<ArrayMatched> Freeze for ArbitraryDisambiguate<ArrayMatched>
where ArrayMatched: Freeze,

§

impl<ArrayMatched> RefUnwindSafe for ArbitraryDisambiguate<ArrayMatched>
where ArrayMatched: RefUnwindSafe,

§

impl<ArrayMatched> Send for ArbitraryDisambiguate<ArrayMatched>
where ArrayMatched: Send,

§

impl<ArrayMatched> Sync for ArbitraryDisambiguate<ArrayMatched>
where ArrayMatched: Sync,

§

impl<ArrayMatched> Unpin for ArbitraryDisambiguate<ArrayMatched>
where ArrayMatched: Unpin,

§

impl<ArrayMatched> UnsafeUnpin for ArbitraryDisambiguate<ArrayMatched>
where ArrayMatched: UnsafeUnpin,

§

impl<ArrayMatched> UnwindSafe for ArbitraryDisambiguate<ArrayMatched>
where ArrayMatched: 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.