Skip to main content

ephemeris_forge_github/plan/
autolinks.rs

1use ephemeris_forge::{Collection, FieldDiff, Report};
2
3use crate::Autolinks;
4use crate::api::model;
5use crate::operation::autolinks::Operation;
6
7pub(super) fn plan(
8    config: Option<&Autolinks>,
9    live: &[model::Autolink],
10    report: &mut Report,
11) -> Vec<Operation> {
12    let Some(config) = config else {
13        return Vec::new();
14    };
15    Collection {
16        noun: "autolink",
17        mode: config.mode,
18    }
19    .plan(
20        &config.autolinks,
21        live,
22        report,
23        |autolink, key_prefix| autolink.key_prefix == key_prefix,
24        |key_prefix, desired, actual, _| {
25            let mut diff = FieldDiff::default();
26            let mut changed = diff.compare(
27                "url_template",
28                actual.map(|actual| &actual.url_template),
29                &desired.url_template,
30            );
31            changed |= diff.compare(
32                "is_alphanumeric",
33                actual.map(|actual| &actual.is_alphanumeric),
34                &desired.is_alphanumeric,
35            );
36            let mut operations = Vec::new();
37            if changed {
38                operations.extend(actual.map(|actual| Operation::Delete {
39                    key_prefix: key_prefix.to_owned(),
40                    id: actual.id,
41                }));
42                operations.push(Operation::Create {
43                    key_prefix: key_prefix.to_owned(),
44                    autolink: desired.clone(),
45                });
46            }
47            (diff, operations)
48        },
49        |autolink, _| {
50            Some((
51                autolink.key_prefix.clone(),
52                Operation::Delete {
53                    key_prefix: autolink.key_prefix.clone(),
54                    id: autolink.id,
55                },
56            ))
57        },
58    )
59}