git_workflow/state/
mod.rs1pub mod next_action;
4pub mod repo;
5pub mod sync;
6pub mod working_dir;
7
8pub use next_action::NextAction;
9pub use repo::RepoType;
10pub use sync::SyncState;
11pub use working_dir::WorkingDirState;
12
13use std::marker::PhantomData;
14
15use crate::error::{GwError, Result};
16use crate::git;
17
18pub struct Protected;
20
21pub struct Deletable;
23
24#[derive(Debug)]
26pub struct Branch<Status> {
27 name: String,
28 _status: PhantomData<Status>,
29}
30
31impl<S> Branch<S> {
32 pub fn name(&self) -> &str {
34 &self.name
35 }
36}
37
38impl Branch<Protected> {
39 pub fn protected(name: String) -> Self {
41 Branch {
42 name,
43 _status: PhantomData,
44 }
45 }
46}
47
48impl Branch<Deletable> {
49 pub fn deletable(name: String) -> Self {
51 Branch {
52 name,
53 _status: PhantomData,
54 }
55 }
56
57 pub fn delete(self, verbose: bool) -> Result<()> {
59 git::delete_branch(&self.name, verbose)
60 }
61}
62
63pub fn classify_branch(branch: &str, repo_type: &RepoType) -> BranchClassification {
65 if repo_type.is_protected(branch) {
66 BranchClassification::Protected(Branch::protected(branch.to_string()))
67 } else {
68 BranchClassification::Deletable(Branch::deletable(branch.to_string()))
69 }
70}
71
72pub enum BranchClassification {
74 Protected(Branch<Protected>),
75 Deletable(Branch<Deletable>),
76}
77
78impl BranchClassification {
79 pub fn name(&self) -> &str {
81 match self {
82 BranchClassification::Protected(b) => b.name(),
83 BranchClassification::Deletable(b) => b.name(),
84 }
85 }
86
87 pub fn try_deletable(self) -> Result<Branch<Deletable>> {
89 match self {
90 BranchClassification::Protected(b) => {
91 Err(GwError::ProtectedBranch(b.name().to_string()))
92 }
93 BranchClassification::Deletable(b) => Ok(b),
94 }
95 }
96}