attribute_derive::utils

Enum FlagOrValue

Source
pub enum FlagOrValue<T> {
    None,
    Flag,
    Value(T),
}
Expand description

FromAttr value that can be used both as a flag and with a value.

When parameter is specified both as flag and as value, the value will dominate.

#[derive(FromAttr)]
struct Test {
    param: FlagOrValue<String>,
}

assert_eq!(
    Test::from_args(quote!(param)).unwrap().param,
    FlagOrValue::Flag
);
assert_eq!(
    Test::from_args(quote!(param = "value")).unwrap().param,
    FlagOrValue::Value("value".into())
);
assert_eq!(
    Test::from_args(quote!(param, param = "value", param))
        .unwrap()
        .param,
    FlagOrValue::Value("value".into())
);
assert_eq!(Test::from_args(quote!()).unwrap().param, FlagOrValue::None);

Variants§

§

None

Was not specified.

§

Flag

Was specified as a flag, i.e., without a value.

§

Value(T)

Was specified with a value.

Implementations§

Source§

impl<T> FlagOrValue<T>

Source

pub fn is_none(&self) -> bool

Was not specified.

Source

pub fn is_flag(&self) -> bool

Was specified as a flag, i.e., without a value.

Source

pub fn is_value(&self) -> bool

Was specified with a value.

Source

pub fn into_value(self) -> Option<T>

Returns value if set.

Source

pub fn as_value(&self) -> Option<&T>

Returns value if set.

Source

pub fn map_value<I>(self, map: impl FnOnce(T) -> I) -> FlagOrValue<I>

Maps the value if present.

Trait Implementations§

Source§

impl<T: AttributeBase> AttributeBase for FlagOrValue<T>

Source§

type Partial = Partial<FlagOrValue<<T as AttributeBase>::Partial>>

Partial type for this attribute. In most cases this can be Self, unless the attribute can be parsed in multiple on-its-own-incomplete parts or needs special handling on the conversion.
Source§

impl<T: AttributeValue> AttributeNamed for FlagOrValue<T>

Source§

fn parse_named( name: &'static str, input: ParseStream<'_>, ) -> Result<Option<Named<SpannedValue<Self::Partial>>>>

Parses an attribute containing Self called name. Read more
Source§

const PREFERRED_OPEN_DELIMITER: &'static str = " = "

What open delimiter to use when providing error messages. Read more
Source§

const PREFERRED_CLOSE_DELIMITER: &'static str = ""

What close delimiter to use when providing error messages. Read more
Source§

impl<T: Debug> Debug for FlagOrValue<T>

Source§

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

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

impl<T> Default for FlagOrValue<T>

Source§

fn default() -> FlagOrValue<T>

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

impl<T: FromAttr> FromAttr for FlagOrValue<T>

Source§

fn parse_partial(input: ParseStream<'_>) -> Result<Self::Partial>

Actual implementation for parsing the attribute. This is the only function required to implement in this trait and derived by the FromAttr derive macro.
Source§

fn from_attribute(attr: impl Borrow<Attribute>) -> Result<Self>

Parses from a single attribute. Ignoring the name. Read more
Source§

fn from_input(input: impl Into<TokenStream>) -> Result<Self>

Source§

fn parse_input(input: ParseStream<'_>) -> Result<Self>

Parses input as the complete attribute. Read more
Source§

fn from_attribute_partial(attr: impl Borrow<Attribute>) -> Result<Self::Partial>

Like parse_partial but instead takes an Attribute. Read more
Source§

impl<T: FromPartial<P>, P> FromPartial<Partial<FlagOrValue<P>>> for FlagOrValue<T>

Source§

fn from(partial: Partial<FlagOrValue<P>>) -> Result<Self>

Creates Self from T. Read more
Source§

fn from_option( partial: Option<Partial<FlagOrValue<P>>>, _: &str, ) -> Result<Self>

Creates Self from optional T. Read more
Source§

fn join( first: Option<SpannedValue<Partial<FlagOrValue<P>>>>, second: SpannedValue<Partial<FlagOrValue<P>>>, specified_twice_error: &str, ) -> Result<Option<SpannedValue<Partial<FlagOrValue<P>>>>>

Defines how two arguments for the same parameter should be handled. Read more
Source§

impl<T: Ord> Ord for FlagOrValue<T>

Source§

fn cmp(&self, other: &FlagOrValue<T>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<T: PartialEq> PartialEq for FlagOrValue<T>

Source§

fn eq(&self, other: &FlagOrValue<T>) -> 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<T: PartialOrd> PartialOrd for FlagOrValue<T>

Source§

fn partial_cmp(&self, other: &FlagOrValue<T>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<T> Transpose<Option<FlagOrValue<T>>> for FlagOrValue<Option<T>>

Source§

fn transpose(self) -> Option<FlagOrValue<T>>

Should behave equivalent to the built-in transpose functions available on Result<Option> and Option<Result>.
Source§

impl<T, E> Transpose<Result<FlagOrValue<T>, E>> for FlagOrValue<Result<T, E>>

Source§

fn transpose(self) -> Result<FlagOrValue<T>, E>

Should behave equivalent to the built-in transpose functions available on Result<Option> and Option<Result>.
Source§

impl<T: Eq> Eq for FlagOrValue<T>

Source§

impl<T> StructuralPartialEq for FlagOrValue<T>

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<T> UnwindSafe for FlagOrValue<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.

Source§

impl<P, T> FromPartial<Defaulting<P>> for T
where T: Default + FromPartial<P>,

Source§

fn from(partial: Defaulting<P>) -> Result<T, Error>

Creates Self from T. Read more
Source§

fn from_option(partial: Option<Defaulting<P>>, _error: &str) -> Result<T, Error>

Creates Self from optional T. Read more
Source§

fn join( first: Option<SpannedValue<T>>, second: SpannedValue<T>, specified_twice_error: &str, ) -> Result<Option<SpannedValue<T>>>

Defines how two arguments for the same parameter should be handled. Read more
Source§

impl<T> FromPartial<T> for T

Source§

fn from(partial: T) -> Result<T, Error>

Creates Self from T. Read more
Source§

fn from_option(partial: Option<T>, error_missing: &str) -> Result<Self>

Creates Self from optional T. Read more
Source§

fn join( first: Option<SpannedValue<T>>, second: SpannedValue<T>, specified_twice_error: &str, ) -> Result<Option<SpannedValue<T>>>

Defines how two arguments for the same parameter should be handled. 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.