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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![recursion_limit = "128"]
#![doc(html_logo_url = "https://raw.githubusercontent.com/RustPython/RustPython/main/logo.png")]
#![doc(html_root_url = "https://docs.rs/rustpython-derive/")]

extern crate proc_macro;

#[macro_use]
extern crate maplit;

#[macro_use]
mod error;
#[macro_use]
mod util;

mod compile_bytecode;
mod from_args;
mod pyclass;
mod pymodule;
mod pypayload;
mod pystructseq;
mod pytraverse;

use error::{extract_spans, Diagnostic};
use proc_macro2::TokenStream;
use quote::ToTokens;
use rustpython_doc as doc;
use syn::{AttributeArgs, DeriveInput, Item};

pub use compile_bytecode::Compiler;

fn result_to_tokens(result: Result<TokenStream, impl Into<Diagnostic>>) -> TokenStream {
    result
        .map_err(|e| e.into())
        .unwrap_or_else(ToTokens::into_token_stream)
}

pub fn derive_from_args(input: DeriveInput) -> TokenStream {
    result_to_tokens(from_args::impl_from_args(input))
}

pub fn pyclass(attr: AttributeArgs, item: Item) -> TokenStream {
    if matches!(item, syn::Item::Impl(_) | syn::Item::Trait(_)) {
        result_to_tokens(pyclass::impl_pyclass_impl(attr, item))
    } else {
        result_to_tokens(pyclass::impl_pyclass(attr, item))
    }
}

pub fn pyexception(attr: AttributeArgs, item: Item) -> TokenStream {
    if matches!(item, syn::Item::Impl(_)) {
        result_to_tokens(pyclass::impl_pyexception_impl(attr, item))
    } else {
        result_to_tokens(pyclass::impl_pyexception(attr, item))
    }
}

pub fn pymodule(attr: AttributeArgs, item: Item) -> TokenStream {
    result_to_tokens(pymodule::impl_pymodule(attr, item))
}

pub fn pystruct_sequence(input: DeriveInput) -> TokenStream {
    result_to_tokens(pystructseq::impl_pystruct_sequence(input))
}

pub fn pystruct_sequence_try_from_object(input: DeriveInput) -> TokenStream {
    result_to_tokens(pystructseq::impl_pystruct_sequence_try_from_object(input))
}

pub fn py_compile(input: TokenStream, compiler: &dyn Compiler) -> TokenStream {
    result_to_tokens(compile_bytecode::impl_py_compile(input, compiler))
}

pub fn py_freeze(input: TokenStream, compiler: &dyn Compiler) -> TokenStream {
    result_to_tokens(compile_bytecode::impl_py_freeze(input, compiler))
}

pub fn pypayload(input: DeriveInput) -> TokenStream {
    result_to_tokens(pypayload::impl_pypayload(input))
}

pub fn pytraverse(item: DeriveInput) -> TokenStream {
    result_to_tokens(pytraverse::impl_pytraverse(item))
}