Struct LazyRegex

Source
pub struct LazyRegex<T = LazyLock<Regex>>(pub T);
Expand description

Transparent wrapper around a type dereferencing to a Regex. Can be used as a separator, or in param validation.

§Why a separate type?

A separate type is necessary to circumvent orphaning rules. We want to implement Split and Validate for any type (e.g., LazyLock) that lazily initializes a Regex, since a Regex on its own cannot be initialized in compile time. Similarly, such a type cannot be dereferenced in compile time, which rules out implementing these traits for &'static Regex.

§Examples

The easiest way to initialize a wrapper is the lazy_regex! macro.

use smart_config::{de::Delimited, pat::{lazy_regex, LazyRegex}};

static NAME_REGEX: LazyRegex = lazy_regex!(r"^[a-z][-a-z0-9]*$");

#[derive(DescribeConfig, DeserializeConfig)]
struct TestConfig {
    #[config(validate(NAME_REGEX))]
    app: String,
    // The macro also can be inlined!
    #[config(with = Delimited::new(lazy_regex!(ref r"\s*,\s*")))]
    numbers: Vec<u64>,
}

Tuple Fields§

§0: T

Trait Implementations§

Source§

impl<T: Debug> Debug for LazyRegex<T>

Source§

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

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

impl<T> Split for &'static LazyRegex<T>
where T: Deref<Target = Regex> + Send + Sync,

Source§

fn split_once<'s>(&self, haystack: &'s str) -> Option<(&'s str, &'s str)>

Splits the given haystack at most once from its start. This generalizes str::split_once().
Source§

fn split<'s>(&self, haystack: &'s str) -> impl Iterator<Item = &'s str>

Splits the given haystack. This generalizes str::split().
Source§

impl<T> Validate<String> for LazyRegex<T>
where T: Deref<Target = Regex> + Send + Sync + 'static,

Validates that the string matches the provided regex.

Don’t forget to surround the regex with ^$ if you want to match it completely.

Source§

fn describe(&self, formatter: &mut Formatter<'_>) -> Result

Describes this validation. Read more
Source§

fn validate(&self, target: &String) -> Result<(), ErrorWithOrigin>

Validates a parameter / config. Read more

Auto Trait Implementations§

§

impl<T> Freeze for LazyRegex<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for LazyRegex<T>
where T: RefUnwindSafe,

§

impl<T> Send for LazyRegex<T>
where T: Send,

§

impl<T> Sync for LazyRegex<T>
where T: Sync,

§

impl<T> Unpin for LazyRegex<T>
where T: Unpin,

§

impl<T> UnwindSafe for LazyRegex<T>
where T: 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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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, 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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more