Skip to main content

ValueSyntax

Enum ValueSyntax 

Source
pub enum ValueSyntax {
    Eq,
    Paren,
}
Expand description

Syntax used for providing a value

Variants§

§

Eq

= contents

§

Paren

(contents)

Implementations§

Source§

impl ValueSyntax

Source

pub const fn is_eq(self) -> bool

Returns true if the syntax is Eq.

§Example
assert!(ValueSyntax::Eq.is_eq());
assert!(!ValueSyntax::Paren.is_eq());
Source

pub const fn is_paren(self) -> bool

Returns true if the syntax is Paren.

§Example
assert!(ValueSyntax::Paren.is_paren());
assert!(!ValueSyntax::Eq.is_paren());
Source

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");
Source

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");
Source

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

Source§

fn clone(&self) -> ValueSyntax

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 Copy for ValueSyntax

Source§

impl Debug for ValueSyntax

Source§

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

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

impl Eq for ValueSyntax

Source§

impl PartialEq for ValueSyntax

Source§

fn eq(&self, other: &ValueSyntax) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for ValueSyntax

Auto Trait Implementations§

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> 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 = 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.