turbo_tasks_macros_shared/
value_trait_arguments.rs

1use proc_macro2::Span;
2use syn::{
3    parse::{Parse, ParseStream},
4    punctuated::Punctuated,
5    spanned::Spanned,
6    Meta, Token,
7};
8
9/// Arguments to the `#[turbo_tasks::value_trait]` attribute macro.
10#[derive(Debug)]
11pub struct ValueTraitArguments {
12    /// Whether the macro should generate a `ValueDebug`-like `dbg()`
13    /// implementation on the trait's `Vc`.
14    pub debug: bool,
15    /// By default, traits have a `turbo_tasks::NonLocalValue` supertype. Should we skip this?
16    pub local: bool,
17    /// Should the trait have a `turbo_tasks::OperationValue` supertype?
18    pub operation: Option<Span>,
19}
20
21impl Default for ValueTraitArguments {
22    fn default() -> Self {
23        Self {
24            debug: true,
25            local: false,
26            operation: None,
27        }
28    }
29}
30
31impl Parse for ValueTraitArguments {
32    fn parse(input: ParseStream) -> syn::Result<Self> {
33        let mut result = Self::default();
34        if input.is_empty() {
35            return Ok(result);
36        }
37
38        let punctuated: Punctuated<Meta, Token![,]> = input.parse_terminated(Meta::parse)?;
39        for meta in punctuated {
40            match meta.path().get_ident().map(ToString::to_string).as_deref() {
41                Some("no_debug") => {
42                    result.debug = false;
43                }
44                Some("local") => {
45                    result.local = true;
46                }
47                Some("operation") => {
48                    result.operation = Some(meta.span());
49                }
50                _ => {
51                    return Err(syn::Error::new_spanned(meta, "unknown parameter"));
52                }
53            }
54        }
55
56        Ok(result)
57    }
58}