Skip to main content

ephemeris_forge_github/config/
autolinks.rs

1use std::collections::BTreeMap;
2
3use ephemeris_forge::{Mode, non_empty_name};
4use facet::Facet;
5
6use super::ConfigError;
7
8const NUMBER_PLACEHOLDER: &str = "<num>";
9
10non_empty_name! {
11    /// The prefix that triggers an autolink, which is never empty.
12    pub struct KeyPrefix;
13    /// An autolink key prefix was the empty string.
14    pub error EmptyKeyPrefix("autolink key prefix cannot be empty");
15}
16
17/// An autolink reference.
18#[derive(Clone, Debug, Eq, Facet, PartialEq)]
19#[facet(deny_unknown_fields)]
20pub struct Autolink {
21    /// The link target. It contains `<num>` where the reference number goes.
22    pub url_template: String,
23    /// Whether the reference may contain letters. Otherwise it is digits only.
24    pub is_alphanumeric: bool,
25}
26
27/// Desired autolink references of a repository.
28#[derive(Clone, Debug, Eq, Facet, PartialEq)]
29#[facet(deny_unknown_fields)]
30pub struct Autolinks {
31    /// Whether autolinks that are not declared are kept or deleted.
32    pub mode: Mode,
33    /// The autolinks keyed by key prefix.
34    pub autolinks: BTreeMap<KeyPrefix, Autolink>,
35}
36
37impl Autolinks {
38    /// Check what decoding cannot.
39    ///
40    /// # Errors
41    /// Returns an error when a URL template has no `<num>`.
42    pub fn validate(&self) -> Result<(), ConfigError> {
43        for (key_prefix, autolink) in &self.autolinks {
44            if !autolink.url_template.contains(NUMBER_PLACEHOLDER) {
45                return Err(ConfigError::AutolinkTemplateWithoutNumber {
46                    key_prefix: key_prefix.clone(),
47                });
48            }
49        }
50        Ok(())
51    }
52}