Skip to main content

ephemeris_forge_forgejo/config/
deploy_keys.rs

1use std::collections::BTreeMap;
2
3use ephemeris_forge::Mode;
4use facet::Facet;
5
6/// Desired settings of one deploy key. The title is its key.
7#[derive(Clone, Debug, Eq, Facet, PartialEq)]
8#[facet(deny_unknown_fields)]
9pub struct DeployKey {
10    /// The public key.
11    pub key: String,
12    /// Whether the key can only read.
13    pub read_only: bool,
14}
15
16/// Desired deploy keys of a repository.
17#[derive(Clone, Debug, Eq, Facet, PartialEq)]
18#[facet(deny_unknown_fields)]
19pub struct DeployKeys {
20    /// Whether deploy keys that are not declared are deleted.
21    pub mode: Mode,
22    /// The deploy keys keyed by title.
23    pub keys: BTreeMap<String, DeployKey>,
24}
25
26impl DeployKey {
27    /// The key type and the key data, which identify a public key.
28    ///
29    /// The comment that follows is not part of the identity, because Forgejo
30    /// may rewrite it.
31    #[must_use]
32    pub(crate) fn identity(key: &str) -> (&str, &str) {
33        let mut fields = key.split_whitespace();
34        (
35            fields.next().unwrap_or_default(),
36            fields.next().unwrap_or_default(),
37        )
38    }
39
40    /// Whether a key reported by Forgejo is this key.
41    #[must_use]
42    pub fn matches(&self, reported: &str) -> bool {
43        Self::identity(&self.key) == Self::identity(reported)
44    }
45}