kinetics_parser/
endpoint.rs

1use crate::environment::{parse_environment, Environment};
2use syn::{
3    parse::{Parse, ParseStream},
4    token, Ident, LitBool, LitStr,
5};
6
7#[derive(Default, Debug, Clone)]
8pub struct Endpoint {
9    pub name: Option<String>,
10    pub url_path: Option<String>,
11    pub environment: Environment,
12    pub queues: Option<Vec<String>>,
13    pub is_disabled: Option<bool>,
14}
15
16impl Parse for Endpoint {
17    fn parse(input: ParseStream) -> syn::Result<Self> {
18        let mut name = None;
19        let mut url_path = None;
20        let mut environment = Environment::default();
21        let mut queues = None;
22        let mut is_disabled = Some(false);
23
24        while !input.is_empty() {
25            let ident: Ident = input.parse()?;
26            input.parse::<token::Eq>()?;
27
28            match ident.to_string().as_str() {
29                "name" => {
30                    name = Some(input.parse::<LitStr>()?.value());
31                }
32                "url_path" => {
33                    url_path = Some(input.parse::<LitStr>()?.value());
34                }
35                "environment" => {
36                    environment = parse_environment(input)?;
37                }
38                "queues" => {
39                    let content;
40                    syn::bracketed!(content in input);
41                    let queue_list = content.parse::<LitStr>()?.value();
42
43                    queues = Some(
44                        queue_list
45                            // Remove square brackets
46                            .trim_matches(|c| c == '[' || c == ']')
47                            .split(',')
48                            // Remove whitespaces and quotes per item
49                            .map(|i| i.trim().trim_matches('"').to_string())
50                            .collect::<Vec<String>>(),
51                    );
52                }
53                "is_disabled" => is_disabled = Some(input.parse::<LitBool>()?.value()),
54
55                // Ignore unknown attributes
56                _ => {}
57            }
58
59            if !input.is_empty() {
60                input.parse::<token::Comma>()?;
61            }
62        }
63
64        Ok(Endpoint {
65            name,
66            url_path,
67            environment,
68            queues,
69            is_disabled,
70        })
71    }
72}