Path: blob/main/pyo3-polars/pyo3-polars-derive/src/attr.rs
6939 views
use std::fmt::Debug;12use proc_macro2::Ident;3use syn::parse::{Parse, ParseStream};4use syn::Token;56use crate::keywords;78#[derive(Clone, Debug)]9pub struct KeyWordAttribute<K, V> {10#[allow(dead_code)]11pub kw: K,12pub value: V,13}1415impl<K: Parse, V: Parse> Parse for KeyWordAttribute<K, V> {16fn parse(input: ParseStream) -> syn::Result<Self> {17let kw = input.parse()?;18let _: Token![=] = input.parse()?;19let value = input.parse()?;20Ok(KeyWordAttribute { kw, value })21}22}2324pub type OutputAttribute = KeyWordAttribute<keywords::output_type, Ident>;25pub type OutputFuncAttribute = KeyWordAttribute<keywords::output_type_func, Ident>;26pub type OutputFuncAttributeWithKwargs =27KeyWordAttribute<keywords::output_type_func_with_kwargs, Ident>;2829#[derive(Default, Debug)]30pub struct ExprsFunctionOptions {31pub output_dtype: Option<Ident>,32pub output_type_fn: Option<Ident>,33pub output_type_fn_kwargs: Option<Ident>,34}3536impl Parse for ExprsFunctionOptions {37fn parse(input: ParseStream<'_>) -> syn::Result<Self> {38let mut options = ExprsFunctionOptions::default();3940while !input.is_empty() {41let lookahead = input.lookahead1();4243if lookahead.peek(keywords::output_type) {44let attr = input.parse::<OutputAttribute>()?;45options.output_dtype = Some(attr.value)46} else if lookahead.peek(keywords::output_type_func) {47let attr = input.parse::<OutputFuncAttribute>()?;48options.output_type_fn = Some(attr.value)49} else if lookahead.peek(keywords::output_type_func_with_kwargs) {50let attr = input.parse::<OutputFuncAttributeWithKwargs>()?;51options.output_type_fn_kwargs = Some(attr.value)52} else {53panic!("didn't recognize attribute")54}55}56Ok(options)57}58}596061