ion_proc/attribute/
class.rs

1/*
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 */
6
7use syn::meta::ParseNestedMeta;
8use syn::{LitStr, Result};
9
10use crate::attribute::name::Name;
11use crate::attribute::{ArgumentError, ParseArgument as _, ParseArgumentWith as _, ParseAttribute};
12use crate::class::method::MethodKind;
13
14// TODO: Add `inspectable` to provide `toString` and `toJSON`
15#[derive(Default)]
16pub(crate) struct ClassAttribute {
17	pub(crate) name: Option<LitStr>,
18}
19
20impl ParseAttribute for ClassAttribute {
21	type Parent<'a> = ();
22
23	fn parse(&mut self, meta: &ParseNestedMeta) -> Result<bool> {
24		self.name.parse_argument(meta, "name", "Class")
25	}
26}
27
28#[derive(Default)]
29pub(crate) struct MethodAttribute {
30	pub(crate) name: Option<Name>,
31	pub(crate) aliases: Vec<LitStr>,
32	pub(crate) kind: Option<MethodKind>,
33	pub(crate) skip: bool,
34}
35
36impl ParseAttribute for MethodAttribute {
37	type Parent<'a> = ();
38
39	fn parse(&mut self, meta: &ParseNestedMeta) -> Result<bool> {
40		const METHOD_KIND_ERROR: ArgumentError =
41			ArgumentError::Full("Method cannot have multiple `constructor`, `get`, or `set` attributes.");
42
43		let success = self.name.parse_argument(meta, "name", "Method")?
44			|| self.aliases.parse_argument(meta, "alias", None)?
45			|| self
46				.kind
47				.parse_argument_with(meta, MethodKind::Constructor, "constructor", METHOD_KIND_ERROR)?
48			|| self.kind.parse_argument_with(meta, MethodKind::Getter, "get", METHOD_KIND_ERROR)?
49			|| self.kind.parse_argument_with(meta, MethodKind::Setter, "set", METHOD_KIND_ERROR)?
50			|| self.skip.parse_argument(meta, "skip", "Method")?;
51
52		Ok(success)
53	}
54}