Skip to main content

walrus_cron/
parser.rs

1//! Cron markdown parsing.
2//!
3//! Parses YAML frontmatter + Markdown body into a [`CronEntry`].
4
5use compact_str::CompactString;
6use serde::Deserialize;
7use wcore::utils::split_yaml_frontmatter;
8
9/// A cron job entry parsed from a markdown file.
10#[derive(Debug, Clone)]
11pub struct CronEntry {
12    /// Cron job name.
13    pub name: CompactString,
14    /// Cron schedule expression (e.g. "0 0 9 * * *").
15    pub schedule: String,
16    /// Name of the agent to invoke.
17    pub agent: CompactString,
18    /// Message template (from the markdown body).
19    pub message: String,
20}
21
22/// YAML frontmatter for cron markdown files.
23#[derive(Deserialize)]
24struct CronFrontmatter {
25    name: String,
26    schedule: String,
27    agent: String,
28}
29
30/// Parse a cron markdown file (YAML frontmatter + body) into a [`CronEntry`].
31///
32/// The frontmatter provides name, schedule, and agent. The markdown body
33/// (trimmed) becomes the cron entry's message template.
34pub fn parse_cron_md(content: &str) -> anyhow::Result<CronEntry> {
35    let (frontmatter, body) = split_yaml_frontmatter(content)?;
36    let fm: CronFrontmatter = serde_yaml::from_str(frontmatter)?;
37
38    Ok(CronEntry {
39        name: CompactString::from(fm.name),
40        schedule: fm.schedule,
41        agent: CompactString::from(fm.agent),
42        message: body.trim().to_owned(),
43    })
44}