|
| 1 | +use quote::quote; |
| 2 | +use syn::parenthesized; |
| 3 | + |
| 4 | +#[derive(Debug)] |
| 5 | +pub(crate) struct Column { |
| 6 | + pub(crate) name: Option<syn::LitStr>, |
| 7 | + pub(crate) ty: Option<ColumnType>, |
| 8 | +} |
| 9 | + |
| 10 | +impl Column { |
| 11 | + pub(super) fn from_ast(attr: &syn::Attribute) -> syn::Result<Column> { |
| 12 | + attr.parse_args() |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +impl syn::parse::Parse for Column { |
| 17 | + fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { |
| 18 | + let mut result = Self { |
| 19 | + name: None, |
| 20 | + ty: None, |
| 21 | + }; |
| 22 | + |
| 23 | + // Loop through the list of comma separated arguments to fill in the result one by one. |
| 24 | + // |
| 25 | + // Allowed syntax: |
| 26 | + // |
| 27 | + // #[column("name")] |
| 28 | + // #[column(type = "type")] |
| 29 | + // #[column("name", type = "type")] |
| 30 | + // #[column(type = "type", "name")] |
| 31 | + loop { |
| 32 | + let lookahead = input.lookahead1(); |
| 33 | + |
| 34 | + if lookahead.peek(syn::LitStr) { |
| 35 | + if result.name.is_some() { |
| 36 | + return Err(syn::Error::new(input.span(), "duplicate column name")); |
| 37 | + } |
| 38 | + result.name = Some(input.parse()?); |
| 39 | + } else if lookahead.peek(syn::Token![type]) { |
| 40 | + if result.ty.is_some() { |
| 41 | + return Err(syn::Error::new(input.span(), "duplicate column type")); |
| 42 | + } |
| 43 | + let _type_token: syn::Token![type] = input.parse()?; |
| 44 | + let _eq_token: syn::Token![=] = input.parse()?; |
| 45 | + result.ty = Some(input.parse()?); |
| 46 | + } else { |
| 47 | + return Err(lookahead.error()); |
| 48 | + } |
| 49 | + |
| 50 | + if input.is_empty() { |
| 51 | + break; |
| 52 | + } |
| 53 | + let _comma_token: syn::Token![,] = input.parse()?; |
| 54 | + } |
| 55 | + |
| 56 | + Ok(result) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +mod kw { |
| 61 | + syn::custom_keyword!(varchar); |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Debug)] |
| 65 | +pub enum ColumnType { |
| 66 | + VarChar(u64), |
| 67 | + Custom(syn::LitStr), |
| 68 | +} |
| 69 | + |
| 70 | +impl syn::parse::Parse for ColumnType { |
| 71 | + fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { |
| 72 | + let lookahead = input.lookahead1(); |
| 73 | + if lookahead.peek(syn::LitStr) { |
| 74 | + Ok(Self::Custom(input.parse()?)) |
| 75 | + } else if lookahead.peek(kw::varchar) { |
| 76 | + let _kw: kw::varchar = input.parse()?; |
| 77 | + let content; |
| 78 | + parenthesized!(content in input); |
| 79 | + let lit: syn::LitInt = content.parse()?; |
| 80 | + Ok(Self::VarChar(lit.base10_parse()?)) |
| 81 | + } else { |
| 82 | + Err(lookahead.error()) |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +impl quote::ToTokens for ColumnType { |
| 88 | + fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) { |
| 89 | + match self { |
| 90 | + Self::VarChar(size) => quote! { db::Type::VarChar(#size) }, |
| 91 | + Self::Custom(custom) => quote! { db::Type::Custom(#custom) }, |
| 92 | + } |
| 93 | + .to_tokens(tokens); |
| 94 | + } |
| 95 | +} |
0 commit comments