Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
pola-rs
GitHub Repository: pola-rs/polars
Path: blob/main/pyo3-polars/pyo3-polars-derive/src/attr.rs
6939 views
1
use std::fmt::Debug;
2
3
use proc_macro2::Ident;
4
use syn::parse::{Parse, ParseStream};
5
use syn::Token;
6
7
use crate::keywords;
8
9
#[derive(Clone, Debug)]
10
pub struct KeyWordAttribute<K, V> {
11
#[allow(dead_code)]
12
pub kw: K,
13
pub value: V,
14
}
15
16
impl<K: Parse, V: Parse> Parse for KeyWordAttribute<K, V> {
17
fn parse(input: ParseStream) -> syn::Result<Self> {
18
let kw = input.parse()?;
19
let _: Token![=] = input.parse()?;
20
let value = input.parse()?;
21
Ok(KeyWordAttribute { kw, value })
22
}
23
}
24
25
pub type OutputAttribute = KeyWordAttribute<keywords::output_type, Ident>;
26
pub type OutputFuncAttribute = KeyWordAttribute<keywords::output_type_func, Ident>;
27
pub type OutputFuncAttributeWithKwargs =
28
KeyWordAttribute<keywords::output_type_func_with_kwargs, Ident>;
29
30
#[derive(Default, Debug)]
31
pub struct ExprsFunctionOptions {
32
pub output_dtype: Option<Ident>,
33
pub output_type_fn: Option<Ident>,
34
pub output_type_fn_kwargs: Option<Ident>,
35
}
36
37
impl Parse for ExprsFunctionOptions {
38
fn parse(input: ParseStream<'_>) -> syn::Result<Self> {
39
let mut options = ExprsFunctionOptions::default();
40
41
while !input.is_empty() {
42
let lookahead = input.lookahead1();
43
44
if lookahead.peek(keywords::output_type) {
45
let attr = input.parse::<OutputAttribute>()?;
46
options.output_dtype = Some(attr.value)
47
} else if lookahead.peek(keywords::output_type_func) {
48
let attr = input.parse::<OutputFuncAttribute>()?;
49
options.output_type_fn = Some(attr.value)
50
} else if lookahead.peek(keywords::output_type_func_with_kwargs) {
51
let attr = input.parse::<OutputFuncAttributeWithKwargs>()?;
52
options.output_type_fn_kwargs = Some(attr.value)
53
} else {
54
panic!("didn't recognize attribute")
55
}
56
}
57
Ok(options)
58
}
59
}
60
61