Skip to main content

solar_core/
tool.rs

1mod cargo_deny;
2mod commitalyzer;
3mod github_workspaces;
4mod licenses;
5mod pre_commit;
6mod semver_release;
7mod vhooks;
8
9pub use cargo_deny::CargoDeny;
10pub use commitalyzer::Commitalyzer;
11pub use github_workspaces::Workspaces;
12pub use licenses::Licenses;
13pub use pre_commit::PreCommit;
14pub use semver_release::SemverRelease;
15pub use vhooks::Vhooks;
16
17use crate::SolarError;
18
19use strum::IntoEnumIterator;
20use strum_macros::EnumIter;
21
22use clap::Subcommand as SC;
23
24pub enum Action {
25    INSTALL,
26    UPGRADE,
27    REMOVE,
28}
29
30pub trait ToolTrait {
31    fn act(&self, action: &Action) -> Result<(), SolarError> {
32        match action {
33            Action::INSTALL => self.install(),
34            Action::UPGRADE => self.upgrade(),
35            Action::REMOVE => self.remove(),
36        }
37    }
38
39    fn install(&self) -> Result<(), SolarError>;
40
41    fn remove(&self) -> Result<(), SolarError>;
42
43    fn upgrade(&self) -> Result<(), SolarError> {
44        self.remove()?;
45        self.install()?;
46        Ok(())
47    }
48}
49
50#[derive(SC, Clone, EnumIter, PartialEq, Debug)]
51pub enum Tool {
52    /// Configures a versioned git hook folder for a project.
53    VHOOKS(Vhooks),
54
55    /// Installs commitalyzer (git commit linting tool) to the git hooks directory.
56    COMMITALYZER(Commitalyzer),
57
58    /// Installs and configured SemVer-Release in the project.
59    SEMVERRELEASE(SemverRelease),
60
61    /// Installs the appropriate licenses into the project.
62    LICENSES(Licenses),
63
64    /// Configures project with standard Github workflows.
65    WORKSPACES(Workspaces),
66
67    /// Configures project with a standard pre-commit hook for rust.
68    PRECOMMIT(PreCommit),
69
70    /// Configures project with a cargo deny for license checking.
71    DENY(CargoDeny),
72}
73
74impl Tool {
75    fn act(&self, action: &Action) -> Result<(), SolarError> {
76        match self {
77            Self::VHOOKS(tool) => tool.act(action),
78            Self::COMMITALYZER(tool) => tool.act(action),
79            Self::SEMVERRELEASE(tool) => tool.act(action),
80            Self::LICENSES(tool) => tool.act(action),
81            Self::WORKSPACES(tool) => tool.act(action),
82            Self::PRECOMMIT(tool) => tool.act(action),
83            Self::DENY(tool) => tool.act(action),
84        }
85    }
86
87    pub fn perform(arg: &Option<Self>, action: Action) -> Result<(), SolarError> {
88        match arg {
89            Some(tool) => tool.act(&action),
90            None => {
91                for tool in Self::iter() {
92                    tool.act(&action)?;
93                }
94                Ok(())
95            }
96        }
97    }
98}