kinetics_parser/
worker.rs

1use crate::environment::{parse_environment, Environment};
2use syn::{
3    parse::{Parse, ParseStream},
4    token, Ident, LitBool, LitInt, LitStr,
5};
6
7#[derive(Debug, Clone)]
8pub struct Worker {
9    pub name: Option<String>,
10    pub concurrency: i16,
11    pub fifo: bool,
12    pub environment: Environment,
13}
14
15impl Default for Worker {
16    fn default() -> Self {
17        Worker {
18            name: None,
19            concurrency: 1,
20            fifo: false,
21            environment: Environment::new(),
22        }
23    }
24}
25
26impl Parse for Worker {
27    fn parse(input: ParseStream) -> syn::Result<Self> {
28        let mut worker = Worker::default();
29
30        while !input.is_empty() {
31            let ident: Ident = input.parse()?;
32            input.parse::<token::Eq>()?;
33
34            match ident.to_string().as_str() {
35                "name" => {
36                    worker.name = Some(input.parse::<LitStr>()?.value());
37                }
38                "environment" => {
39                    worker.environment = parse_environment(input)?;
40                }
41                "concurrency" => {
42                    worker.concurrency = input.parse::<LitInt>()?.base10_parse::<i16>()?;
43                }
44                "fifo" => {
45                    worker.fifo = match input.parse::<LitBool>() {
46                        Ok(bool) => bool.value,
47                        Err(_) => {
48                            return Err(input.error("expected boolean value for 'fifo'"));
49                        }
50                    };
51                }
52                // Ignore unknown attributes
53                _ => {}
54            }
55
56            if !input.is_empty() {
57                input.parse::<token::Comma>()?;
58            }
59        }
60
61        Ok(worker)
62    }
63}