Skip to main content

verbs/
source_authority.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Typed source-authority decisions shared by behavior and recommendations.
3
4use repo::{RepositorySourceAuthority, shell_quote};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum SourceAction {
8    Capture,
9    Commit,
10    Push,
11    Pull,
12}
13
14#[derive(Debug, Clone, Copy)]
15pub struct SourceAuthorityActions {
16    authority: RepositorySourceAuthority,
17}
18
19impl SourceAuthorityActions {
20    pub fn new(authority: RepositorySourceAuthority) -> Self {
21        Self { authority }
22    }
23
24    pub fn authority(self) -> RepositorySourceAuthority {
25        self.authority
26    }
27
28    pub fn argv(self, action: SourceAction) -> Vec<String> {
29        match (self.authority, action) {
30            (_, SourceAction::Capture) => vec!["heddle", "capture", "-m", "..."],
31            (RepositorySourceAuthority::GitOverlay, SourceAction::Commit) => {
32                vec!["heddle", "commit", "-m", "..."]
33            }
34            (RepositorySourceAuthority::Native, SourceAction::Commit) => {
35                vec!["heddle", "capture", "-m", "..."]
36            }
37            (_, SourceAction::Push) => vec!["heddle", "push"],
38            (_, SourceAction::Pull) => vec!["heddle", "pull"],
39        }
40        .into_iter()
41        .map(str::to_string)
42        .collect()
43    }
44
45    pub fn display(self, action: SourceAction) -> String {
46        self.argv(action)
47            .into_iter()
48            .map(|arg| {
49                if arg == "..." {
50                    "\"...\"".to_string()
51                } else {
52                    shell_quote(&arg)
53                }
54            })
55            .collect::<Vec<_>>()
56            .join(" ")
57    }
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn authority_selects_the_source_transport() {
66        let overlay = SourceAuthorityActions::new(RepositorySourceAuthority::GitOverlay);
67        let native = SourceAuthorityActions::new(RepositorySourceAuthority::Native);
68        assert_eq!(overlay.display(SourceAction::Push), "heddle push");
69        assert_eq!(overlay.display(SourceAction::Pull), "heddle pull");
70        assert_eq!(native.display(SourceAction::Push), "heddle push");
71        assert_eq!(native.display(SourceAction::Pull), "heddle pull");
72    }
73
74    #[test]
75    fn commit_routes_to_the_authoritative_store() {
76        let overlay = SourceAuthorityActions::new(RepositorySourceAuthority::GitOverlay);
77        let native = SourceAuthorityActions::new(RepositorySourceAuthority::Native);
78        assert_eq!(
79            overlay.display(SourceAction::Commit),
80            "heddle commit -m \"...\""
81        );
82        assert_eq!(
83            native.display(SourceAction::Commit),
84            "heddle capture -m \"...\""
85        );
86    }
87
88    #[test]
89    fn capture_is_the_heddle_save_boundary() {
90        for authority in [
91            RepositorySourceAuthority::GitOverlay,
92            RepositorySourceAuthority::Native,
93        ] {
94            assert_eq!(
95                SourceAuthorityActions::new(authority).display(SourceAction::Capture),
96                "heddle capture -m \"...\""
97            );
98        }
99    }
100}