znap_syn/
lib.rs

1pub mod codegen;
2pub mod common;
3pub mod parser;
4use codegen::action as action_codegen;
5use codegen::collection as collection_codegen;
6use codegen::error_code as error_code_codegen;
7use deluxe::ExtractAttributes;
8use deluxe::ParseMetaItem;
9use parser::action as action_parser;
10use parser::collection as collection_parser;
11use parser::error_code as error_code_parser;
12use proc_macro2::TokenStream;
13use quote::ToTokens;
14use syn::ItemEnum;
15use syn::Variant;
16use syn::{
17    parse::{Parse, ParseStream, Result as ParseResult},
18    Ident, ItemFn, ItemMod, ItemStruct,
19};
20
21#[derive(Debug)]
22pub struct CollectionMod {
23    pub actions: Vec<Ident>,
24    pub get_action_fns: Vec<ActionFn>,
25    pub post_action_fns: Vec<ActionFn>,
26    pub name: Ident,
27    pub collection_mod: ItemMod,
28}
29
30impl Parse for CollectionMod {
31    fn parse(input: ParseStream) -> ParseResult<Self> {
32        let collection_mod = <ItemMod as Parse>::parse(input)?;
33        collection_parser::parse(&collection_mod)
34    }
35}
36
37impl From<&CollectionMod> for TokenStream {
38    fn from(collection_mod: &CollectionMod) -> Self {
39        collection_codegen::generate(collection_mod)
40    }
41}
42
43impl ToTokens for CollectionMod {
44    fn to_tokens(&self, tokens: &mut TokenStream) {
45        tokens.extend::<TokenStream>(self.into());
46    }
47}
48
49#[derive(Debug)]
50pub struct GetActionFn {
51    pub raw_method: ItemFn,
52    pub name: Ident,
53    pub action: Ident,
54}
55
56#[derive(Debug)]
57pub struct ActionFn {
58    pub raw_method: ItemFn,
59    pub name: Ident,
60    pub action: Ident,
61}
62
63#[derive(Debug)]
64pub struct ActionStruct {
65    pub name: Ident,
66    pub raw_struct: ItemStruct,
67    pub attributes: Option<ActionAttributesStruct>,
68    pub query_attrs: Option<Vec<(Ident, Ident)>>,
69    pub params_attrs: Option<Vec<(Ident, Ident)>>,
70    pub path_attrs: Option<PathAttributesStruct>,
71}
72
73impl Parse for ActionStruct {
74    fn parse(input: ParseStream) -> ParseResult<Self> {
75        let action_struct = <ItemStruct as Parse>::parse(input)?;
76        action_parser::parse(&action_struct)
77    }
78}
79
80impl From<&ActionStruct> for TokenStream {
81    fn from(action_struct: &ActionStruct) -> Self {
82        action_codegen::generate(action_struct)
83    }
84}
85
86impl ToTokens for ActionStruct {
87    fn to_tokens(&self, tokens: &mut TokenStream) {
88        tokens.extend::<TokenStream>(self.into());
89    }
90}
91
92#[derive(Debug, ExtractAttributes)]
93#[deluxe(attributes(action))]
94pub struct ActionAttributesStruct {
95    pub icon: String,
96    pub title: String,
97    pub description: String,
98    pub label: String,
99    #[deluxe(append, rename = link, default = Vec::new())]
100    pub links: Vec<ActionLinkStruct>,
101}
102
103#[derive(Debug, ParseMetaItem)]
104pub struct ActionLinkStruct {
105    label: String,
106    href: String,
107    #[deluxe(append, rename = parameter, default = Vec::new())]
108    parameters: Vec<ActionLinkParameterStruct>,
109}
110
111#[derive(Debug, ParseMetaItem)]
112pub struct ActionLinkParameterStruct {
113    label: String,
114    name: String,
115    #[deluxe(default = false)]
116    required: bool,
117}
118
119#[derive(Debug, ExtractAttributes)]
120#[deluxe(attributes(action_path))]
121pub struct PathAttributesStruct {
122    // #[deluxe(default = Some("api".to_string()))]
123    pub prefix: Option<String>,
124    // #[deluxe(default = Some("{{prefix}}/{{action_name}}".to_string()))]
125    pub template: Option<String>,
126}
127
128#[derive(Debug)]
129pub struct ErrorEnum {
130    pub name: Ident,
131    pub raw_enum: ItemEnum,
132    pub error_variants: Vec<ErrorVariant>,
133}
134
135impl Parse for ErrorEnum {
136    fn parse(input: ParseStream) -> ParseResult<Self> {
137        let error_enum = <ItemEnum as Parse>::parse(input)?;
138        error_code_parser::parse(&error_enum)
139    }
140}
141
142impl From<&ErrorEnum> for TokenStream {
143    fn from(error_enum: &ErrorEnum) -> Self {
144        error_code_codegen::generate(error_enum)
145    }
146}
147
148impl ToTokens for ErrorEnum {
149    fn to_tokens(&self, tokens: &mut TokenStream) {
150        tokens.extend::<TokenStream>(self.into());
151    }
152}
153
154#[derive(Debug, ExtractAttributes)]
155#[deluxe(attributes(error))]
156pub struct ErrorAttributesStruct {
157    pub msg: String,
158}
159
160#[derive(Debug)]
161pub struct ErrorVariant {
162    pub name: Ident,
163    pub raw_variant: Variant,
164    pub msg: String,
165}