|
| 1 | +use proc_macro2::TokenStream; |
| 2 | +use quote::ToTokens; |
1 | 3 | use syn::{
|
2 | 4 | parse::{Parse, ParseStream},
|
3 | 5 | punctuated::Punctuated,
|
| 6 | + spanned::Spanned, |
4 | 7 | token::Comma,
|
5 |
| - Attribute, ExprPath, Ident, LitStr, Path, Result, Token, |
| 8 | + Attribute, Expr, ExprPath, Ident, LitStr, Path, Result, Token, |
6 | 9 | };
|
7 | 10 |
|
8 | 11 | pub mod kw {
|
9 | 12 | syn::custom_keyword!(annotation);
|
10 | 13 | syn::custom_keyword!(attribute);
|
| 14 | + syn::custom_keyword!(dict); |
| 15 | + syn::custom_keyword!(extends); |
| 16 | + syn::custom_keyword!(freelist); |
11 | 17 | syn::custom_keyword!(from_py_with);
|
| 18 | + syn::custom_keyword!(gc); |
12 | 19 | syn::custom_keyword!(get);
|
13 | 20 | syn::custom_keyword!(item);
|
14 |
| - syn::custom_keyword!(pass_module); |
| 21 | + syn::custom_keyword!(module); |
15 | 22 | syn::custom_keyword!(name);
|
| 23 | + syn::custom_keyword!(pass_module); |
16 | 24 | syn::custom_keyword!(set);
|
17 | 25 | syn::custom_keyword!(signature);
|
| 26 | + syn::custom_keyword!(subclass); |
18 | 27 | syn::custom_keyword!(text_signature);
|
19 | 28 | syn::custom_keyword!(transparent);
|
| 29 | + syn::custom_keyword!(unsendable); |
| 30 | + syn::custom_keyword!(weakref); |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Clone, Debug)] |
| 34 | +pub struct KeywordAttribute<K, V> { |
| 35 | + pub kw: K, |
| 36 | + pub value: V, |
20 | 37 | }
|
21 | 38 |
|
| 39 | +/// A helper type which parses the inner type via a literal string |
| 40 | +/// e.g. LitStrValue<Path> -> parses "some::path" in quotes. |
22 | 41 | #[derive(Clone, Debug, PartialEq)]
|
23 |
| -pub struct FromPyWithAttribute(pub ExprPath); |
| 42 | +pub struct LitStrValue<T>(pub T); |
24 | 43 |
|
25 |
| -impl Parse for FromPyWithAttribute { |
| 44 | +impl<T: Parse> Parse for LitStrValue<T> { |
26 | 45 | fn parse(input: ParseStream) -> Result<Self> {
|
27 |
| - let _: kw::from_py_with = input.parse()?; |
28 |
| - let _: Token![=] = input.parse()?; |
29 |
| - let string_literal: LitStr = input.parse()?; |
30 |
| - string_literal.parse().map(FromPyWithAttribute) |
| 46 | + let lit_str: LitStr = input.parse()?; |
| 47 | + lit_str.parse().map(LitStrValue) |
31 | 48 | }
|
32 | 49 | }
|
33 | 50 |
|
34 |
| -#[derive(Clone, Debug, PartialEq)] |
35 |
| -pub struct NameAttribute(pub Ident); |
36 |
| - |
37 |
| -impl Parse for NameAttribute { |
38 |
| - fn parse(input: ParseStream) -> Result<Self> { |
39 |
| - let _: kw::name = input.parse()?; |
40 |
| - let _: Token![=] = input.parse()?; |
41 |
| - let string_literal: LitStr = input.parse()?; |
42 |
| - string_literal.parse().map(NameAttribute) |
| 51 | +impl<T: ToTokens> ToTokens for LitStrValue<T> { |
| 52 | + fn to_tokens(&self, tokens: &mut TokenStream) { |
| 53 | + self.0.to_tokens(tokens) |
43 | 54 | }
|
44 | 55 | }
|
45 | 56 |
|
46 |
| -/// For specifying the path to the pyo3 crate. |
| 57 | +/// A helper type which parses a name via a literal string |
47 | 58 | #[derive(Clone, Debug, PartialEq)]
|
48 |
| -pub struct CrateAttribute(pub Path); |
| 59 | +pub struct NameLitStr(pub Ident); |
49 | 60 |
|
50 |
| -impl Parse for CrateAttribute { |
| 61 | +impl Parse for NameLitStr { |
51 | 62 | fn parse(input: ParseStream) -> Result<Self> {
|
52 |
| - let _: Token![crate] = input.parse()?; |
53 |
| - let _: Token![=] = input.parse()?; |
54 | 63 | let string_literal: LitStr = input.parse()?;
|
55 |
| - string_literal.parse().map(CrateAttribute) |
| 64 | + if let Ok(ident) = string_literal.parse() { |
| 65 | + Ok(NameLitStr(ident)) |
| 66 | + } else { |
| 67 | + bail_spanned!(string_literal.span() => "expected a single identifier in double quotes") |
| 68 | + } |
56 | 69 | }
|
57 | 70 | }
|
58 | 71 |
|
59 |
| -#[derive(Clone, Debug, PartialEq)] |
60 |
| -pub struct TextSignatureAttribute { |
61 |
| - pub kw: kw::text_signature, |
62 |
| - pub eq_token: Token![=], |
63 |
| - pub lit: LitStr, |
| 72 | +impl ToTokens for NameLitStr { |
| 73 | + fn to_tokens(&self, tokens: &mut TokenStream) { |
| 74 | + self.0.to_tokens(tokens) |
| 75 | + } |
64 | 76 | }
|
65 | 77 |
|
66 |
| -impl Parse for TextSignatureAttribute { |
| 78 | +pub type ExtendsAttribute = KeywordAttribute<kw::extends, Path>; |
| 79 | +pub type FreelistAttribute = KeywordAttribute<kw::freelist, Box<Expr>>; |
| 80 | +pub type ModuleAttribute = KeywordAttribute<kw::module, LitStr>; |
| 81 | +pub type NameAttribute = KeywordAttribute<kw::name, NameLitStr>; |
| 82 | +pub type TextSignatureAttribute = KeywordAttribute<kw::text_signature, LitStr>; |
| 83 | + |
| 84 | +impl<K: Parse + std::fmt::Debug, V: Parse> Parse for KeywordAttribute<K, V> { |
67 | 85 | fn parse(input: ParseStream) -> Result<Self> {
|
68 |
| - Ok(TextSignatureAttribute { |
69 |
| - kw: input.parse()?, |
70 |
| - eq_token: input.parse()?, |
71 |
| - lit: input.parse()?, |
72 |
| - }) |
| 86 | + let kw: K = input.parse()?; |
| 87 | + let _: Token![=] = input.parse()?; |
| 88 | + let value = input.parse()?; |
| 89 | + Ok(KeywordAttribute { kw, value }) |
73 | 90 | }
|
74 | 91 | }
|
75 | 92 |
|
| 93 | +impl<K: ToTokens, V: ToTokens> ToTokens for KeywordAttribute<K, V> { |
| 94 | + fn to_tokens(&self, tokens: &mut TokenStream) { |
| 95 | + self.kw.to_tokens(tokens); |
| 96 | + Token).to_tokens(tokens); |
| 97 | + self.value.to_tokens(tokens); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +pub type FromPyWithAttribute = KeywordAttribute<kw::from_py_with, LitStrValue<ExprPath>>; |
| 102 | + |
| 103 | +/// For specifying the path to the pyo3 crate. |
| 104 | +pub type CrateAttribute = KeywordAttribute<Token![crate], LitStrValue<Path>>; |
| 105 | + |
76 | 106 | pub fn get_pyo3_options<T: Parse>(attr: &syn::Attribute) -> Result<Option<Punctuated<T, Comma>>> {
|
77 | 107 | if is_attribute_ident(attr, "pyo3") {
|
78 | 108 | attr.parse_args_with(Punctuated::parse_terminated).map(Some)
|
|
0 commit comments