kinetics_parser/
endpoint.rs

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