summaryrefslogtreecommitdiff
path: root/ops/attrs.rs
blob: d0182fc6937254407b0f1ca5ee773d0b69d36af1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
use syn::parse::Parse;
use syn::parse::ParseStream;
use syn::Error;
use syn::Ident;
use syn::Result;
use syn::Token;

#[derive(Clone, Debug, Default)]
pub struct Attributes {
  pub is_unstable: bool,
  pub is_v8: bool,
  pub must_be_fast: bool,
  pub deferred: bool,
  pub is_wasm: bool,
  pub relation: Option<Ident>,
}

impl Parse for Attributes {
  fn parse(input: ParseStream) -> Result<Self> {
    let mut self_ = Self::default();
    let mut fast = false;
    while let Ok(v) = input.parse::<Ident>() {
      match v.to_string().as_str() {
        "unstable" => self_.is_unstable = true,
        "v8" => self_.is_v8 = true,
        "fast" => fast = true,
        "deferred" => self_.deferred = true,
        "wasm" => self_.is_wasm = true,
        "slow" => {
          if !fast {
            return Err(Error::new(
              input.span(),
              "relational attributes can only be used with fast attribute",
            ));
          }
          input.parse::<Token![=]>()?;
          self_.relation = Some(input.parse()?);
        }
        _ => {
          return Err(Error::new(
             input.span(),
            "invalid attribute, expected one of: unstable, v8, fast, deferred, wasm",
            ));
        }
      };
      let _ = input.parse::<Token![,]>();
    }

    self_.must_be_fast = self_.is_wasm || fast;

    Ok(self_)
  }
}