Skip to main content

ephemeris_forge_github/config/
variables.rs

1use std::collections::BTreeMap;
2
3use ephemeris_forge::{Mode, VariableNameRules};
4use facet::Facet;
5
6use super::{ConfigError, EnvironmentName};
7
8/// The names GitHub refuses for Actions variables.
9const NAME_RULES: VariableNameRules = VariableNameRules {
10    reserved_names: &[],
11    reserved_prefixes: &["GITHUB_"],
12};
13
14/// Desired Actions variables of a repository.
15#[derive(Clone, Debug, Eq, Facet, PartialEq)]
16#[facet(deny_unknown_fields)]
17pub struct Variables {
18    /// Whether variables that are not declared are kept or deleted.
19    pub mode: Mode,
20    /// Repository variables keyed by name.
21    pub repository: BTreeMap<String, String>,
22    /// Environment variables keyed by environment name, then by variable name.
23    pub environments: BTreeMap<EnvironmentName, BTreeMap<String, String>>,
24}
25
26impl Variables {
27    /// Check what decoding cannot.
28    ///
29    /// # Errors
30    /// Returns an error when a variable name is one GitHub refuses.
31    pub fn validate(&self) -> Result<(), ConfigError> {
32        NAME_RULES.check(self.repository.keys().map(String::as_str))?;
33        for variables in self.environments.values() {
34            NAME_RULES.check(variables.keys().map(String::as_str))?;
35        }
36        Ok(())
37    }
38}