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(Default, Debug, Clone, Serialize, Deserialize)]
9pub struct Endpoint {
10    pub name: Option<String>,
11    pub url_path: Option<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 = Environment::default();
22        let mut queues = None;
23        let mut is_disabled = Some(false);
24
25        while !input.is_empty() {
26            let ident: Ident = input.parse()?;
27            input.parse::<token::Eq>()?;
28
29            match ident.to_string().as_str() {
30                "name" => {
31                    name = Some(input.parse::<LitStr>()?.value());
32                }
33                "url_path" => {
34                    url_path = Some(input.parse::<LitStr>()?.value());
35                }
36                "environment" => {
37                    environment = parse_environment(input)?;
38                }
39                "queues" => {
40                    let content;
41                    syn::bracketed!(content in input);
42                    let queue_list = content.parse::<LitStr>()?.value();
43
44                    queues = Some(
45                        queue_list
46                            // Remove square brackets
47                            .trim_matches(|c| c == '[' || c == ']')
48                            .split(',')
49                            // Remove whitespaces and quotes per item
50                            .map(|i| i.trim().trim_matches('"').to_string())
51                            .collect::<Vec<String>>(),
52                    );
53                }
54                "is_disabled" => is_disabled = Some(input.parse::<LitBool>()?.value()),
55
56                // Ignore unknown attributes
57                _ => {}
58            }
59
60            if !input.is_empty() {
61                input.parse::<token::Comma>()?;
62            }
63        }
64
65        Ok(Endpoint {
66            name,
67            url_path,
68            environment,
69            queues,
70            is_disabled,
71        })
72    }
73}