Skip to main content

kinetics_parser/params/
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 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 = None;
21        let mut is_disabled = None;
22
23        while !input.is_empty() {
24            let ident_span = input.span();
25            let ident: Ident = input.parse()?;
26            input.parse::<token::Eq>()?;
27
28            match ident.to_string().as_str() {
29                "name" => {
30                    if name.is_some() {
31                        return Err(syn::Error::new(ident_span, "Duplicate attribute `name`"));
32                    }
33                    name = Some(input.parse::<LitStr>()?.value());
34                }
35                "url_path" => {
36                    if url_path.is_some() {
37                        return Err(syn::Error::new(
38                            ident_span,
39                            "Duplicate attribute `url_path`",
40                        ));
41                    }
42                    url_path = Some(input.parse::<LitStr>()?.value());
43                }
44                "environment" => {
45                    if environment.is_some() {
46                        return Err(syn::Error::new(
47                            ident_span,
48                            "Duplicate attribute `environment`",
49                        ));
50                    }
51                    environment = Some(parse_environment(input)?);
52                }
53                "is_disabled" => {
54                    if is_disabled.is_some() {
55                        return Err(syn::Error::new(
56                            ident_span,
57                            "Duplicate attribute `is_disabled`",
58                        ));
59                    }
60                    is_disabled = Some(input.parse::<LitBool>()?.value());
61                }
62
63                // Ignore unknown attributes
64                _ => {}
65            }
66
67            if !input.is_empty() {
68                input.parse::<token::Comma>()?;
69            }
70        }
71
72        Ok(Endpoint {
73            name,
74            url_path: url_path
75                .ok_or_else(|| input.error("Missing required attribute `url_path`"))?,
76            environment: environment.unwrap_or_default(),
77            is_disabled,
78        })
79    }
80}