pub enum ValueSyntax {
Eq,
Paren,
}
Available on crate feature
attr_parse
only.Expand description
Syntax used for providing a value
Variants§
Implementations§
Source§impl ValueSyntax
impl ValueSyntax
Sourcepub fn from_stream(parse: &ParseBuffer<'_>) -> Option<ValueSyntax>
pub fn from_stream(parse: &ParseBuffer<'_>) -> Option<ValueSyntax>
Peek the stream without moving the cursor and attempt to construct self based on the next token.
§Example
struct Foo {
peek: Option<ValueSyntax>,
rest: TokenStream,
}
impl Parse for Foo {
fn parse(input: ParseStream) -> syn::Result<Self> {
let peek = ValueSyntax::from_stream(input);
let rest = input.parse()?;
Ok(Self { peek, rest })
}
}
let v: Foo = syn::parse2(quote!(= 123)).unwrap();
assert_eq!(v.peek, Some(ValueSyntax::Eq));
assert_eq!(v.rest.to_string(), "= 123");
let v: Foo = syn::parse2(quote!((456))).unwrap();
assert_eq!(v.peek, Some(ValueSyntax::Paren));
assert_eq!(v.rest.to_string(), "(456)");
let v: Foo = syn::parse2(quote!(none)).unwrap();
assert_eq!(v.peek, None);
assert_eq!(v.rest.to_string(), "none");
Sourcepub fn parse_token(
self,
input: &ParseBuffer<'_>,
) -> Result<Option<ParseBuffer<'_>>, Error>
pub fn parse_token( self, input: &ParseBuffer<'_>, ) -> Result<Option<ParseBuffer<'_>>, Error>
Parse whatever tokens need to be parsed based on the resolved syntax.
Returns a ParseBuffer
you should continue parsing if the syntax is
Paren
.
§Example
/// `=` implementation
struct Wrapper1(syn::LitStr);
impl Parse for Wrapper1 {
fn parse(input: ParseStream) -> syn::Result<Self> {
let inner = ValueSyntax::Eq.parse_token(input)?;
assert!(inner.is_none());
Ok(Self(input.parse()?))
}
}
/// `(value)` implementation
struct Wrapper2(syn::LitStr);
impl Parse for Wrapper2 {
fn parse(input: ParseStream) -> syn::Result<Self> {
let inner = ValueSyntax::Paren.parse_token(input)?.expect("expected inner buffer");
Ok(Self(inner.parse()?))
}
}
let v: Wrapper1 = syn::parse2(quote!(= "foo")).unwrap();
assert_eq!(v.0.value(), "foo");
let v: Wrapper2 = syn::parse2(quote!(("bar"))).unwrap();
assert_eq!(v.0.value(), "bar");
Sourcepub fn parse<P>(self, input: &ParseBuffer<'_>) -> Result<P, Error>where
P: Parse,
pub fn parse<P>(self, input: &ParseBuffer<'_>) -> Result<P, Error>where
P: Parse,
Parse whatever tokens need to be parsed based on the resolved syntax and
then parse the referenced value as P
.
§Example
/// `=` implementation
struct Wrapper1(syn::LitStr);
impl Parse for Wrapper1 {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(Self(ValueSyntax::Eq.parse(input)?))
}
}
/// `(value)` implementation
struct Wrapper2(syn::LitStr);
impl Parse for Wrapper2 {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(Self(ValueSyntax::Paren.parse(input)?))
}
}
let v: Wrapper1 = syn::parse2(quote!(= "foo")).unwrap();
assert_eq!(v.0.value(), "foo");
let v: Wrapper2 = syn::parse2(quote!(("bar"))).unwrap();
assert_eq!(v.0.value(), "bar");
Trait Implementations§
Source§impl Clone for ValueSyntax
impl Clone for ValueSyntax
Source§fn clone(&self) -> ValueSyntax
fn clone(&self) -> ValueSyntax
Returns a copy of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moreSource§impl Debug for ValueSyntax
impl Debug for ValueSyntax
Source§impl PartialEq for ValueSyntax
impl PartialEq for ValueSyntax
impl Copy for ValueSyntax
impl Eq for ValueSyntax
impl StructuralPartialEq for ValueSyntax
Auto Trait Implementations§
impl Freeze for ValueSyntax
impl RefUnwindSafe for ValueSyntax
impl Send for ValueSyntax
impl Sync for ValueSyntax
impl Unpin for ValueSyntax
impl UnwindSafe for ValueSyntax
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more