kinetics_parser/
cron.rs

1use crate::environment::{parse_environment, Environment};
2use syn::{
3    parse::{Parse, ParseStream},
4    token, Ident, LitStr,
5};
6
7#[derive(Debug, Clone)]
8pub struct Cron {
9    pub name: Option<String>,
10    pub schedule: String,
11    pub environment: Environment,
12}
13
14impl Parse for Cron {
15    fn parse(input: ParseStream) -> syn::Result<Self> {
16        let mut name = None;
17        let mut environment = Environment::default();
18        let mut schedule = None;
19
20        while !input.is_empty() {
21            let ident: Ident = input.parse()?;
22            input.parse::<token::Eq>()?;
23
24            match ident.to_string().as_str() {
25                "name" => {
26                    name = Some(input.parse::<LitStr>()?.value());
27                }
28                "environment" => {
29                    environment = parse_environment(input)?;
30                }
31                "schedule" => {
32                    schedule = Some(input.parse::<LitStr>()?.value());
33                }
34                // Ignore unknown attributes
35                _ => {}
36            }
37
38            if !input.is_empty() {
39                input.parse::<token::Comma>()?;
40            }
41        }
42
43        let Some(schedule) = schedule else {
44            return Err(input.error("Cron validation failed: no schedule provided"));
45        };
46
47        Ok(Cron {
48            name,
49            environment,
50            schedule,
51        })
52    }
53}