|
| 1 | +// Take a look at the license at the top of the repository in the LICENSE file. |
| 2 | + |
| 3 | +use crate::dbus_interface::attributes::{ATTRIBUTE_NAME, EmitsChangedSignal}; |
| 4 | +use proc_macro2::Span; |
| 5 | +use syn::punctuated::Punctuated; |
| 6 | +use syn::{Attribute, LitStr, Meta, Token, parenthesized}; |
| 7 | + |
| 8 | +/// The attribute placed on an `fn` item in the `#[dbus_interface]` trait. |
| 9 | +pub(crate) enum DBusItemAttribute { |
| 10 | + /// `#[dbus(...)]` |
| 11 | + Method(DBusItemAttributeMethod), |
| 12 | + /// `#[dbus(signal, ...)]` |
| 13 | + Signal(DBusItemAttributeSignal), |
| 14 | + /// `#[dbus(property, ...)] |
| 15 | + Property(DBusItemAttributeProperty), |
| 16 | +} |
| 17 | + |
| 18 | +pub(crate) struct DBusItemAttributeMethod { |
| 19 | + pub(crate) name: Option<LitStr>, |
| 20 | + pub(crate) out_args: Option<Punctuated<LitStr, Token![,]>>, |
| 21 | + pub(crate) no_reply: bool, |
| 22 | +} |
| 23 | + |
| 24 | +pub(crate) struct DBusItemAttributeSignal { |
| 25 | + pub(crate) name: Option<LitStr>, |
| 26 | +} |
| 27 | + |
| 28 | +pub(crate) struct DBusItemAttributeProperty { |
| 29 | + pub(crate) name: Option<LitStr>, |
| 30 | + pub(crate) access: DBusPropertyAccess, |
| 31 | + pub(crate) emits_changed_signal: Option<EmitsChangedSignal>, |
| 32 | +} |
| 33 | + |
| 34 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 35 | +pub(crate) enum DBusPropertyAccess { |
| 36 | + Read, |
| 37 | + Write, |
| 38 | + ReadWrite, |
| 39 | +} |
| 40 | + |
| 41 | +impl DBusPropertyAccess { |
| 42 | + pub(crate) fn or(self, other: DBusPropertyAccess) -> Self { |
| 43 | + if self == other { |
| 44 | + self |
| 45 | + } else { |
| 46 | + DBusPropertyAccess::ReadWrite |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +impl DBusItemAttribute { |
| 52 | + pub(crate) fn parse(attrs: &[Attribute], span: Span) -> syn::Result<Self> { |
| 53 | + let tag = DBusItemAttributeTag::parse(attrs)?; |
| 54 | + match tag { |
| 55 | + None => DBusItemAttributeMethod::parse(attrs).map(Self::Method), |
| 56 | + Some(DBusItemAttributeTag::Signal) => { |
| 57 | + DBusItemAttributeSignal::parse(attrs).map(Self::Signal) |
| 58 | + } |
| 59 | + Some(DBusItemAttributeTag::Property) => { |
| 60 | + DBusItemAttributeProperty::parse(attrs, span).map(Self::Property) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +impl DBusItemAttributeMethod { |
| 67 | + fn parse(attrs: &[Attribute]) -> syn::Result<Self> { |
| 68 | + let mut name: Option<LitStr> = None; |
| 69 | + let mut out_args: Option<Punctuated<LitStr, Token![,]>> = None; |
| 70 | + let mut no_reply = false; |
| 71 | + for attr in attrs { |
| 72 | + if attr.path().is_ident(ATTRIBUTE_NAME) { |
| 73 | + attr.parse_nested_meta(|meta| { |
| 74 | + if meta.path.is_ident("name") { |
| 75 | + name = Some(meta.value()?.parse()?); |
| 76 | + Ok(()) |
| 77 | + } else if meta.path.is_ident("out_args") { |
| 78 | + let content; |
| 79 | + parenthesized!(content in meta.input); |
| 80 | + out_args = Some(content.call(Punctuated::parse_terminated)?); |
| 81 | + Ok(()) |
| 82 | + } else if meta.path.is_ident("no_reply") { |
| 83 | + no_reply = true; |
| 84 | + Ok(()) |
| 85 | + } else { |
| 86 | + Err(meta.error(format!( |
| 87 | + "unknown attribute `{}`. Possible attributes are `name`, `out_args`, `no_reply`", |
| 88 | + meta.path.get_ident().unwrap(), |
| 89 | + ))) |
| 90 | + } |
| 91 | + })?; |
| 92 | + } |
| 93 | + } |
| 94 | + Ok(Self { |
| 95 | + name, |
| 96 | + out_args, |
| 97 | + no_reply, |
| 98 | + }) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +impl DBusItemAttributeSignal { |
| 103 | + fn parse(attrs: &[Attribute]) -> syn::Result<Self> { |
| 104 | + let mut name: Option<LitStr> = None; |
| 105 | + for attr in attrs { |
| 106 | + if attr.path().is_ident(ATTRIBUTE_NAME) { |
| 107 | + attr.parse_nested_meta(|meta| { |
| 108 | + if meta.path.is_ident("signal") { |
| 109 | + Ok(()) |
| 110 | + } else if meta.path.is_ident("name") { |
| 111 | + name = Some(meta.value()?.parse()?); |
| 112 | + Ok(()) |
| 113 | + } else { |
| 114 | + Err(meta.error(format!( |
| 115 | + "unknown attribute `{}`. Possible attributes are `name`", |
| 116 | + meta.path.get_ident().unwrap(), |
| 117 | + ))) |
| 118 | + } |
| 119 | + })?; |
| 120 | + } |
| 121 | + } |
| 122 | + Ok(Self { name }) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +impl DBusItemAttributeProperty { |
| 127 | + fn parse(attrs: &[Attribute], span: Span) -> syn::Result<Self> { |
| 128 | + let mut name: Option<LitStr> = None; |
| 129 | + let mut access: Option<DBusPropertyAccess> = None; |
| 130 | + let mut emits_changed_signal: Option<EmitsChangedSignal> = None; |
| 131 | + for attr in attrs { |
| 132 | + if attr.path().is_ident(ATTRIBUTE_NAME) { |
| 133 | + attr.parse_nested_meta(|meta| { |
| 134 | + if meta.path.is_ident("property") { |
| 135 | + Ok(()) |
| 136 | + } else if meta.path.is_ident("name") { |
| 137 | + name = Some(meta.value()?.parse()?); |
| 138 | + Ok(()) |
| 139 | + } else if meta.path.is_ident("get") { |
| 140 | + access = Some(access |
| 141 | + .unwrap_or(DBusPropertyAccess::Read) |
| 142 | + .or(DBusPropertyAccess::Read)); |
| 143 | + Ok(()) |
| 144 | + } else if meta.path.is_ident("set") { |
| 145 | + access = Some(access |
| 146 | + .unwrap_or(DBusPropertyAccess::Write) |
| 147 | + .or(DBusPropertyAccess::Write)); |
| 148 | + Ok(()) |
| 149 | + } else if meta.path.is_ident("emits_changed_signal") { |
| 150 | + emits_changed_signal = Some(meta.value()?.parse()?); |
| 151 | + Ok(()) |
| 152 | + } else { |
| 153 | + Err(meta.error(format!( |
| 154 | + "unknown attribute `{}`. Possible attributes are `name`, `get`, `set`, `emits_changed_signal`", |
| 155 | + meta.path.get_ident().unwrap(), |
| 156 | + ))) |
| 157 | + } |
| 158 | + })?; |
| 159 | + } |
| 160 | + } |
| 161 | + let Some(access) = access else { |
| 162 | + return Err(syn::Error::new( |
| 163 | + span, |
| 164 | + "either the attribute `get` or `set` attribute (or both) must be specified", |
| 165 | + )); |
| 166 | + }; |
| 167 | + Ok(Self { |
| 168 | + name, |
| 169 | + access, |
| 170 | + emits_changed_signal, |
| 171 | + }) |
| 172 | + } |
| 173 | +} |
| 174 | + |
| 175 | +enum DBusItemAttributeTag { |
| 176 | + Signal, |
| 177 | + Property, |
| 178 | +} |
| 179 | + |
| 180 | +impl DBusItemAttributeTag { |
| 181 | + pub(crate) fn parse(attrs: &[Attribute]) -> syn::Result<Option<Self>> { |
| 182 | + let mut tag = None; |
| 183 | + for attr in attrs { |
| 184 | + if attr.path().is_ident(ATTRIBUTE_NAME) { |
| 185 | + let nested = |
| 186 | + attr.parse_args_with(Punctuated::<Meta, Token![,]>::parse_terminated)?; |
| 187 | + if let Some(first_nested) = nested.first() { |
| 188 | + if first_nested.path().is_ident("signal") { |
| 189 | + tag = Some(Self::Signal); |
| 190 | + first_nested.require_path_only()?; |
| 191 | + } else if first_nested.path().is_ident("property") { |
| 192 | + tag = Some(Self::Property); |
| 193 | + first_nested.require_path_only()?; |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + } |
| 198 | + Ok(tag) |
| 199 | + } |
| 200 | +} |
0 commit comments