|
| 1 | +use proc_macro2::{Ident, Span}; |
| 2 | +use syn::{ |
| 3 | + parenthesized, |
| 4 | + parse::{self, Parse}, |
| 5 | + spanned::Spanned, |
| 6 | + Field, LitInt, Token, Type, Visibility, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::ast::{Actor, Subscription}; |
| 10 | + |
| 11 | +use super::util::{self, FilterAttrs}; |
| 12 | + |
| 13 | +impl Actor { |
| 14 | + pub(crate) fn parse(item: &Field, span: Span) -> parse::Result<Self> { |
| 15 | + if item.vis != Visibility::Inherited { |
| 16 | + return Err(parse::Error::new( |
| 17 | + span, |
| 18 | + "this field must have inherited / private visibility", |
| 19 | + )); |
| 20 | + } |
| 21 | + |
| 22 | + let FilterAttrs { cfgs, attrs, .. } = util::filter_attributes(item.attrs.clone()); |
| 23 | + |
| 24 | + if !cfgs.is_empty() { |
| 25 | + return Err(parse::Error::new(span, "`#[cfg]` is not allowed on actors")); |
| 26 | + } |
| 27 | + |
| 28 | + let mut priority = None; |
| 29 | + let mut init = None; |
| 30 | + let mut subscriptions = Vec::new(); |
| 31 | + |
| 32 | + for attr in attrs { |
| 33 | + match attr.path.get_ident() { |
| 34 | + Some(name) => { |
| 35 | + match &*name.to_string() { |
| 36 | + "priority" => { |
| 37 | + if priority.is_some() { |
| 38 | + return Err(parse::Error::new( |
| 39 | + attr.span(), |
| 40 | + "only one `#[priority]` attribute is allowed on an actor", |
| 41 | + )); |
| 42 | + } |
| 43 | + |
| 44 | + let prio: EqPriority = syn::parse2(attr.tokens)?; |
| 45 | + priority = Some(prio.priority); |
| 46 | + } |
| 47 | + "init" => { |
| 48 | + if init.is_some() { |
| 49 | + return Err(parse::Error::new( |
| 50 | + attr.span(), |
| 51 | + "only one `#[init]` attribute is allowed on an actor", |
| 52 | + )); |
| 53 | + } |
| 54 | + |
| 55 | + // `#[init(expr)]` can be parsed via `ExprParen` |
| 56 | + let paren: syn::ExprParen = syn::parse2(attr.tokens)?; |
| 57 | + |
| 58 | + init = Some(paren.expr); |
| 59 | + } |
| 60 | + "subscribe" => { |
| 61 | + let subscribe: Subscribe = syn::parse2(attr.tokens)?; |
| 62 | + let capacity = subscribe |
| 63 | + .capacity |
| 64 | + .map(|lit| { |
| 65 | + lit.base10_digits().parse::<u8>().map_err(|_| { |
| 66 | + parse::Error::new(lit.span(), "not a `u8` value") |
| 67 | + }) |
| 68 | + }) |
| 69 | + .transpose()?; |
| 70 | + |
| 71 | + subscriptions.push(Subscription { |
| 72 | + ty: subscribe.ty, |
| 73 | + capacity: capacity.unwrap_or(1), |
| 74 | + }); |
| 75 | + } |
| 76 | + _ => { |
| 77 | + return Err(parse::Error::new( |
| 78 | + name.span(), |
| 79 | + "this attribute is not supported on actor declarations", |
| 80 | + )); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + None => { |
| 85 | + return Err(parse::Error::new( |
| 86 | + attr.path.span(), |
| 87 | + "this attribute is not supported on actor declarations", |
| 88 | + )); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + Ok(Actor { |
| 94 | + ty: Box::new(item.ty.clone()), |
| 95 | + priority: priority.unwrap_or(1), |
| 96 | + init, |
| 97 | + subscriptions, |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +struct EqPriority { |
| 103 | + priority: u8, |
| 104 | +} |
| 105 | + |
| 106 | +impl parse::Parse for EqPriority { |
| 107 | + fn parse(input: parse::ParseStream<'_>) -> syn::Result<Self> { |
| 108 | + let _eq: Token![=] = input.parse()?; |
| 109 | + let lit: syn::LitInt = input.parse()?; |
| 110 | + |
| 111 | + if !lit.suffix().is_empty() { |
| 112 | + return Err(parse::Error::new( |
| 113 | + lit.span(), |
| 114 | + "this literal must be unsuffixed", |
| 115 | + )); |
| 116 | + } |
| 117 | + |
| 118 | + let value = lit.base10_parse::<u8>().ok(); |
| 119 | + match value { |
| 120 | + None | Some(0) => Err(parse::Error::new( |
| 121 | + lit.span(), |
| 122 | + "this literal must be in the range 1...255", |
| 123 | + )), |
| 124 | + Some(priority) => Ok(Self { priority }), |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +struct Subscribe { |
| 130 | + ty: Type, |
| 131 | + capacity: Option<LitInt>, |
| 132 | +} |
| 133 | + |
| 134 | +impl Parse for Subscribe { |
| 135 | + fn parse(input: parse::ParseStream<'_>) -> syn::Result<Self> { |
| 136 | + let content; |
| 137 | + parenthesized!(content in input); |
| 138 | + |
| 139 | + let ty = content.parse()?; |
| 140 | + |
| 141 | + let capacity = if content.is_empty() { |
| 142 | + None |
| 143 | + } else { |
| 144 | + let _: Token![,] = content.parse()?; |
| 145 | + let ident: Ident = content.parse()?; |
| 146 | + |
| 147 | + if ident.to_string() == "capacity" { |
| 148 | + let _: Token![=] = content.parse()?; |
| 149 | + Some(content.parse()?) |
| 150 | + } else { |
| 151 | + return Err(parse::Error::new( |
| 152 | + ident.span(), |
| 153 | + format!("expected `capacity`, found `{}`", ident), |
| 154 | + )); |
| 155 | + } |
| 156 | + }; |
| 157 | + |
| 158 | + Ok(Self { ty, capacity }) |
| 159 | + } |
| 160 | +} |
0 commit comments