party_run/parser/
command_parser.rs

1//! Command parser handling user-provided TOML commmands.
2use std::fs;
3
4use anyhow::{bail, Context};
5
6use super::toml_command::Tasks;
7
8/// Command parser that takes a TOML path and parses its contents.
9pub struct CommandParser {
10    /// File path
11    pub path: String,
12}
13
14impl CommandParser {
15    /// Parse the contents of the inner file and return the parsed set of tasks.
16    /// Returns an error if a command is not well formed (empty).
17    pub fn parse(&self) -> anyhow::Result<Tasks> {
18        let contents = fs::read_to_string(self.path.clone())
19            .context(format!("Failed to read from {}", self.path))?;
20
21        let tasks: Tasks = toml::from_str(&contents).context("Invalid TOML data")?;
22
23        for task in &tasks.tasks {
24            if task.command.is_empty() {
25                bail!("Inalid command");
26            }
27        }
28
29        Ok(tasks)
30    }
31}