use std::ops::Deref; use anyhow::Context; pub enum ActionConfig { Actual { doc: yaml_rust2::Yaml }, None, } impl Deref for ActionConfig { type Target = yaml_rust2::Yaml; fn deref(&self) -> &Self::Target { match &self { ActionConfig::Actual { doc } => doc, ActionConfig::None => &yaml_rust2::Yaml::BadValue, } } } impl TryFrom<&str> for ActionConfig { type Error = anyhow::Error; fn try_from(value: &str) -> Result { let mut cuddle = yaml_rust2::YamlLoader::load_from_str(value)?; if cuddle.len() != 1 { anyhow::bail!("cuddle.yaml can only be 1 document wide"); } let doc = cuddle.pop().unwrap(); let doc = doc["please"]["actions"].clone(); if doc.is_badvalue() { return Ok(Self::None); } Ok(Self::Actual { doc }) } } impl ActionConfig { pub fn parse() -> anyhow::Result { let cuddle_yaml = std::fs::read_to_string("cuddle.yaml").context("failed to read cuddle.yaml")?; Self::try_from(cuddle_yaml.as_str()) } }