Skip to main content

kinetics_parser/
worker.rs

1use crate::environment::{parse_environment, Environment};
2use serde::{Deserialize, Serialize};
3use syn::{
4    parse::{Parse, ParseStream},
5    token, Ident, LitBool, LitInt, LitStr,
6};
7
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Worker {
10    pub name: Option<String>,
11    pub concurrency: u32,
12    pub fifo: bool,
13    pub environment: Environment,
14}
15
16impl Parse for Worker {
17    fn parse(input: ParseStream) -> syn::Result<Self> {
18        let mut name = None;
19        let mut concurrency = None;
20        let mut fifo = None;
21        let mut environment = 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                "environment" => {
36                    if environment.is_some() {
37                        return Err(syn::Error::new(
38                            ident_span,
39                            "Duplicate attribute `environment`",
40                        ));
41                    }
42                    environment = Some(parse_environment(input)?);
43                }
44                "concurrency" => {
45                    if concurrency.is_some() {
46                        return Err(syn::Error::new(
47                            ident_span,
48                            "Duplicate attribute `concurrency`",
49                        ));
50                    }
51                    concurrency = Some(input.parse::<LitInt>()?.base10_parse::<u32>()?);
52                }
53                "fifo" => {
54                    if fifo.is_some() {
55                        return Err(syn::Error::new(ident_span, "Duplicate attribute `fifo`"));
56                    }
57                    fifo = match input.parse::<LitBool>() {
58                        Ok(bool) => Some(bool.value),
59                        Err(_) => {
60                            return Err(input.error("expected boolean value for 'fifo'"));
61                        }
62                    };
63                }
64                // Ignore unknown attributes
65                _ => {}
66            }
67
68            if !input.is_empty() {
69                input.parse::<token::Comma>()?;
70            }
71        }
72
73        Ok(Self {
74            name,
75            concurrency: concurrency.unwrap_or(1),
76            fifo: fifo.unwrap_or_default(),
77            environment: environment.unwrap_or_default(),
78        })
79    }
80}