Trait attribute_derive::parsing::AttributeValue
source · pub trait AttributeValue: AttributeBase {
const EXPECTED: &'static str = "expected `=` or `(`";
const PREFERRED_OPEN_DELIMITER: &'static str = " = ";
const PREFERRED_CLOSE_DELIMITER: &'static str = "";
// Required method
fn parse_value(
input: ParseStream<'_>,
) -> Result<SpannedValue<Self::Partial>>;
// Provided methods
fn parse_value_meta(
input: ParseStream<'_>,
) -> Result<SpannedValue<Self::Partial>> { ... }
fn parse_value_eq(
input: ParseStream<'_>,
) -> Result<SpannedValue<Self::Partial>> { ... }
}
Expand description
Any values that can be parsed in an attribute input.
This is probably the trait you want to implement when you created a custom
type for field inside #[derive(FromAttr)]
, as it will provide
implementations for FromAttr
, AttributeNamed
and, if you implement
the marker trait PositionalValue
, AttributePositional
as well.
For named attributes by default it will support both <name> = <value>
and
<function>(<like>)
, though this can be tweaked in the implementation.
Provided Associated Constants§
sourceconst EXPECTED: &'static str = "expected `=` or `(`"
const EXPECTED: &'static str = "expected `=` or `(`"
Printed when not encountering a (
or =
respectively while trying to
parse a AttributeNamed
.
sourceconst PREFERRED_OPEN_DELIMITER: &'static str = " = "
const PREFERRED_OPEN_DELIMITER: &'static str = " = "
What open delimiter to use when providing error messages.
For <name> = <value>
, this is " = "
, for <function>(<like>)
, it is
"("
.
As named attributes can allow both <name> = <value>
and
name(<value>)
, this might not be the only way this attribute can be
used.
sourceconst PREFERRED_CLOSE_DELIMITER: &'static str = ""
const PREFERRED_CLOSE_DELIMITER: &'static str = ""
What close delimiter to use when providing error messages.
For <name> = <value>
, this is ""
, for <function>(<like>)
, it is
")"
.
As named attributes can allow both <name> = <value>
and
<name>(<value>)
, this might not be the only way this attribute can be
used.
Required Methods§
sourcefn parse_value(input: ParseStream<'_>) -> Result<SpannedValue<Self::Partial>>
fn parse_value(input: ParseStream<'_>) -> Result<SpannedValue<Self::Partial>>
Parses the plain attribute value without leading =
or enclosing
parenthesis.
Note: this input includes potentially a trailing ,
and following
arguments.
attribute = value, ...
^^^^^^^^^^
For simple syntax this is the only function needed to implement, as the
default implementations for parse_value_meta
and parse_value_eq
.
Provided Methods§
sourcefn parse_value_meta(
input: ParseStream<'_>,
) -> Result<SpannedValue<Self::Partial>>
fn parse_value_meta( input: ParseStream<'_>, ) -> Result<SpannedValue<Self::Partial>>
Parses the attribute value when parentheses ((
) were peeked.
Note: this is the input with the parentheses, and potentially following arguments.
attribute(value), ...
^^^^^^^^^^^^
In the default implementation this calls through to
parse_value
after removing the parentheses.
sourcefn parse_value_eq(input: ParseStream<'_>) -> Result<SpannedValue<Self::Partial>>
fn parse_value_eq(input: ParseStream<'_>) -> Result<SpannedValue<Self::Partial>>
Parses the attribute value when an equals (=
) was peeked.
Note: this is the input with the equals, and potentially following arguments.
attribute = value, ...
^^^^^^^^^^^^
In the default implementation this calls through to
parse_value
after removing the =
.