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    Push,
10    Pull,
11}
12
13#[derive(Debug, Clone, Copy)]
14pub struct SourceAuthorityActions {
15    authority: RepositorySourceAuthority,
16}
17
18impl SourceAuthorityActions {
19    pub fn new(authority: RepositorySourceAuthority) -> Self {
20        Self { authority }
21    }
22
23    pub fn authority(self) -> RepositorySourceAuthority {
24        self.authority
25    }
26
27    pub fn argv(self, action: SourceAction) -> Vec<String> {
28        match (self.authority, action) {
29            (_, SourceAction::Capture) => vec!["heddle", "capture", "-m", "..."],
30            (_, SourceAction::Push) => vec!["heddle", "push"],
31            (_, SourceAction::Pull) => vec!["heddle", "pull"],
32        }
33        .into_iter()
34        .map(str::to_string)
35        .collect()
36    }
37
38    pub fn display(self, action: SourceAction) -> String {
39        self.argv(action)
40            .into_iter()
41            .map(|arg| {
42                if arg == "..." {
43                    "\"...\"".to_string()
44                } else {
45                    shell_quote(&arg)
46                }
47            })
48            .collect::<Vec<_>>()
49            .join(" ")
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn authority_selects_the_source_transport() {
59        let overlay = SourceAuthorityActions::new(RepositorySourceAuthority::GitOverlay);
60        let native = SourceAuthorityActions::new(RepositorySourceAuthority::Native);
61        assert_eq!(overlay.display(SourceAction::Push), "heddle push");
62        assert_eq!(overlay.display(SourceAction::Pull), "heddle pull");
63        assert_eq!(native.display(SourceAction::Push), "heddle push");
64        assert_eq!(native.display(SourceAction::Pull), "heddle pull");
65    }
66
67    #[test]
68    fn capture_is_the_heddle_save_boundary() {
69        for authority in [
70            RepositorySourceAuthority::GitOverlay,
71            RepositorySourceAuthority::Native,
72        ] {
73            assert_eq!(
74                SourceAuthorityActions::new(authority).display(SourceAction::Capture),
75                "heddle capture -m \"...\""
76            );
77        }
78    }
79}