1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use proc_macro::TokenStream;

#[derive(Debug)]
pub struct DeriveOptions {
	pub enable_client: bool,
	pub enable_server: bool,
}

impl DeriveOptions {
	pub fn new(enable_client: bool, enable_server: bool) -> Self {
		DeriveOptions {
			enable_client,
			enable_server,
		}
	}

	pub fn try_from(tokens: TokenStream) -> Result<Self, syn::Error> {
		if tokens.is_empty() {
			return Ok(Self::new(true, true));
		}
		let ident: syn::Ident = syn::parse::<syn::Ident>(tokens)?;
		let options = {
			let ident = ident.to_string();
			if ident == "client" {
				Some(Self::new(true, false))
			} else if ident == "server" {
				Some(Self::new(false, true))
			} else {
				None
			}
		};
		match options {
			Some(options) => Ok(options),
			None => Err(syn::Error::new(ident.span(), "Unknown attribute.")),
		}
	}
}