summaryrefslogtreecommitdiff
path: root/crates/typst-macros/src/time.rs
blob: 94fa4e5be0742c060acdf24c00d39a54180c51bb (plain) (blame)
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
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};
use syn::parse::{Parse, ParseStream};
use syn::{parse_quote, Result};

use crate::util::{kw, parse_key_value, parse_string};

/// Expand the `#[time(..)]` macro.
pub fn time(stream: TokenStream, item: syn::ItemFn) -> Result<TokenStream> {
    let meta: Meta = syn::parse2(stream)?;
    Ok(create(meta, item))
}

/// The `..` in `#[time(..)]`.
pub struct Meta {
    pub span: Option<syn::Expr>,
    pub name: Option<String>,
}

impl Parse for Meta {
    fn parse(input: ParseStream) -> Result<Self> {
        Ok(Self {
            name: parse_string::<kw::name>(input)?,
            span: parse_key_value::<kw::span, syn::Expr>(input)?,
        })
    }
}

fn create(meta: Meta, mut item: syn::ItemFn) -> TokenStream {
    let name = meta.name.unwrap_or_else(|| item.sig.ident.to_string());
    let construct = match meta.span.as_ref() {
        Some(span) => quote! { with_span(#name, Some(#span.into_raw())) },
        None => quote! { new(#name) },
    };

    item.block.stmts.insert(
        0,
        parse_quote! {
            let __scope = ::typst_timing::TimingScope::#construct;
        },
    );

    item.into_token_stream()
}