Skip to main content

kinetics_parser/
endpoint.rs

1use crate::environment::{parse_environment, Environment};
2use serde::{Deserialize, Serialize};
3use syn::{
4    parse::{Parse, ParseStream},
5    token, Ident, LitBool, LitStr,
6};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Endpoint {
10    pub name: Option<String>,
11    pub url_path: String,
12    pub environment: Environment,
13    pub queues: Option<Vec<String>>,
14    pub is_disabled: Option<bool>,
15}
16
17impl Parse for Endpoint {
18    fn parse(input: ParseStream) -> syn::Result<Self> {
19        let mut name = None;
20        let mut url_path = None;
21        let mut environment = None;
22        let mut queues = None;
23        let mut is_disabled = None;
24
25        while !input.is_empty() {
26            let ident_span = input.span();
27            let ident: Ident = input.parse()?;
28            input.parse::<token::Eq>()?;
29
30            match ident.to_string().as_str() {
31                "name" => {
32                    if name.is_some() {
33                        return Err(syn::Error::new(ident_span, "Duplicate attribute `name`"));
34                    }
35                    name = Some(input.parse::<LitStr>()?.value());
36                }
37                "url_path" => {
38                    if url_path.is_some() {
39                        return Err(syn::Error::new(
40                            ident_span,
41                            "Duplicate attribute `url_path`",
42                        ));
43                    }
44                    url_path = Some(input.parse::<LitStr>()?.value());
45                }
46                "environment" => {
47                    if environment.is_some() {
48                        return Err(syn::Error::new(
49                            ident_span,
50                            "Duplicate attribute `environment`",
51                        ));
52                    }
53                    environment = Some(parse_environment(input)?);
54                }
55                "queues" => {
56                    if queues.is_some() {
57                        return Err(syn::Error::new(ident_span, "Duplicate attribute `queues`"));
58                    }
59                    let content;
60                    syn::bracketed!(content in input);
61                    let queue_list = content.parse::<LitStr>()?.value();
62
63                    queues = Some(
64                        queue_list
65                            // Remove square brackets
66                            .trim_matches(|c| c == '[' || c == ']')
67                            .split(',')
68                            // Remove whitespaces and quotes per item
69                            .map(|i| i.trim().trim_matches('"').to_string())
70                            .collect::<Vec<String>>(),
71                    );
72                }
73                "is_disabled" => {
74                    if is_disabled.is_some() {
75                        return Err(syn::Error::new(
76                            ident_span,
77                            "Duplicate attribute `is_disabled`",
78                        ));
79                    }
80                    is_disabled = Some(input.parse::<LitBool>()?.value());
81                }
82
83                // Ignore unknown attributes
84                _ => {}
85            }
86
87            if !input.is_empty() {
88                input.parse::<token::Comma>()?;
89            }
90        }
91
92        Ok(Endpoint {
93            name,
94            url_path: url_path
95                .ok_or_else(|| input.error("Missing required attribute `url_path`"))?,
96            environment: environment.unwrap_or_default(),
97            queues,
98            is_disabled,
99        })
100    }
101}