Skip to content

Commit 0f34279

Browse files
committed
Move StackStateAccess trait to its own module
1 parent 514e306 commit 0f34279

File tree

4 files changed

+94
-84
lines changed

4 files changed

+94
-84
lines changed

src/stack/access.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// SPDX-License-Identifier: GPL-2.0-only
2+
3+
//! The [`StackStateAccess`] trait allows uniform access to stack information for
4+
//! stack-like objects.
5+
6+
use crate::patchname::PatchName;
7+
8+
use super::{
9+
iter::{AllPatches, BothPatches},
10+
state::PatchState,
11+
};
12+
13+
/// Trait for accessing stack state.
14+
///
15+
/// Both `Stack` and `StackTransaction` implement this interface.
16+
pub(crate) trait StackStateAccess<'repo> {
17+
/// Get slice of applied patch names.
18+
fn applied(&self) -> &[PatchName];
19+
20+
/// Get slice of unapplied patch names.
21+
fn unapplied(&self) -> &[PatchName];
22+
23+
/// Get slice of hidden patch names.
24+
fn hidden(&self) -> &[PatchName];
25+
26+
/// Get patch state for given patch name.
27+
fn get_patch(&self, patchname: &PatchName) -> &PatchState<'repo>;
28+
29+
/// Test whether given patch name exists in the stack.
30+
///
31+
/// N.B. use [`StackStateAccess::collides()`] to test for potential patch name
32+
/// collisions.
33+
fn has_patch(&self, patchname: &PatchName) -> bool;
34+
35+
/// Test whether given patch name collides with an existing patch name.
36+
///
37+
/// A patch name collides if it is different from only by case from a patch in the
38+
/// stack.
39+
fn collides(&self, patchname: &PatchName) -> Option<&PatchName> {
40+
self.all_patches().find(|pn| patchname.collides(pn))
41+
}
42+
43+
/// Get stack's top commit, or base if no applied patches.
44+
fn top(&self) -> &git2::Commit<'repo>;
45+
46+
/// Get recorded head of the stack.
47+
///
48+
/// N.B. this is probably not what you want. See also [`crate::stack::Stack::branch_head`].
49+
fn head(&self) -> &git2::Commit<'repo>;
50+
51+
/// Get stack's base commit.
52+
fn base(&self) -> &git2::Commit<'repo>;
53+
54+
/// Get the commit for the given patch name.
55+
fn get_patch_commit(&self, patchname: &PatchName) -> &git2::Commit<'repo> {
56+
&self.get_patch(patchname).commit
57+
}
58+
59+
/// Test whether given patch name is applied.
60+
fn is_applied(&self, patchname: &PatchName) -> bool {
61+
self.applied().contains(patchname)
62+
}
63+
64+
/// Test whether given patch name is unapplied.
65+
fn is_unapplied(&self, patchname: &PatchName) -> bool {
66+
self.unapplied().contains(patchname)
67+
}
68+
69+
/// Test whether given patch name is hidden.
70+
fn is_hidden(&self, patchname: &PatchName) -> bool {
71+
self.hidden().contains(patchname)
72+
}
73+
74+
/// Iterator over all patch names: applied, unapplied, and hidden.
75+
fn all_patches(&self) -> AllPatches<'_> {
76+
AllPatches::new(self.applied(), self.unapplied(), self.hidden())
77+
}
78+
79+
/// Iterator over applied and unapplied patch names.
80+
fn applied_and_unapplied(&self) -> BothPatches<'_> {
81+
BothPatches::new(self.applied(), self.unapplied())
82+
}
83+
84+
/// Iterator over unapplied and hidden patch names.
85+
fn unapplied_and_hidden(&self) -> BothPatches<'_> {
86+
BothPatches::new(self.unapplied(), self.hidden())
87+
}
88+
}

src/stack/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-License-Identifier: GPL-2.0-only
22

33
//! The StGit stack data structure.
4+
mod access;
45
mod error;
56
mod iter;
67
mod serde;
@@ -10,7 +11,8 @@ mod state;
1011
mod transaction;
1112
mod upgrade;
1213

14+
pub(crate) use access::StackStateAccess;
1315
pub(crate) use error::Error;
1416
pub(crate) use stack::{get_branch_name, state_refname_from_branch_name, Stack};
15-
pub(crate) use state::{PatchState, StackState, StackStateAccess};
17+
pub(crate) use state::{PatchState, StackState};
1618
pub(crate) use transaction::StackTransaction;

src/stack/stack.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,8 @@ use anyhow::{anyhow, Result};
99
use git2::{Branch, Commit};
1010

1111
use super::{
12-
state::{StackState, StackStateAccess},
13-
transaction::TransactionBuilder,
14-
upgrade::stack_upgrade,
15-
PatchState,
12+
state::StackState, transaction::TransactionBuilder, upgrade::stack_upgrade, PatchState,
13+
StackStateAccess,
1614
};
1715
use crate::{patchname::PatchName, repo::RepositoryExtended};
1816

src/stack/state.rs

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ use crate::{
1616
commit::{CommitMessage, CommitOptions, RepositoryCommitExtended},
1717
patchname::PatchName,
1818
signature::{SignatureExtended, TimeExtended},
19-
stack::serde::RawStackState,
2019
};
2120

22-
use super::iter::{AllPatches, BothPatches};
21+
use super::{access::StackStateAccess, iter::AllPatches, serde::RawStackState};
2322

2423
/// Stack state as recorded in the git repository.
2524
///
@@ -59,83 +58,6 @@ pub(crate) struct PatchState<'repo> {
5958
pub commit: Commit<'repo>,
6059
}
6160

62-
/// Trait for accessing stack state.
63-
///
64-
/// Both `Stack` and `StackTransaction` implement this interface.
65-
pub(crate) trait StackStateAccess<'repo> {
66-
/// Get slice of applied patch names.
67-
fn applied(&self) -> &[PatchName];
68-
69-
/// Get slice of unapplied patch names.
70-
fn unapplied(&self) -> &[PatchName];
71-
72-
/// Get slice of hidden patch names.
73-
fn hidden(&self) -> &[PatchName];
74-
75-
/// Get patch state for given patch name.
76-
fn get_patch(&self, patchname: &PatchName) -> &PatchState<'repo>;
77-
78-
/// Test whether given patch name exists in the stack.
79-
///
80-
/// N.B. use [`StackStateAccess::collides()`] to test for potential patch name
81-
/// collisions.
82-
fn has_patch(&self, patchname: &PatchName) -> bool;
83-
84-
/// Test whether given patch name collides with an existing patch name.
85-
///
86-
/// A patch name collides if it is different from only by case from a patch in the
87-
/// stack.
88-
fn collides(&self, patchname: &PatchName) -> Option<&PatchName> {
89-
self.all_patches().find(|pn| patchname.collides(pn))
90-
}
91-
92-
/// Get stack's top commit, or base if no applied patches.
93-
fn top(&self) -> &Commit<'repo>;
94-
95-
/// Get recorded head of the stack.
96-
///
97-
/// N.B. this is probably not what you want. See also [`crate::stack::Stack::branch_head`].
98-
fn head(&self) -> &Commit<'repo>;
99-
100-
/// Get stack's base commit.
101-
fn base(&self) -> &Commit<'repo>;
102-
103-
/// Get the commit for the given patch name.
104-
fn get_patch_commit(&self, patchname: &PatchName) -> &Commit<'repo> {
105-
&self.get_patch(patchname).commit
106-
}
107-
108-
/// Test whether given patch name is applied.
109-
fn is_applied(&self, patchname: &PatchName) -> bool {
110-
self.applied().contains(patchname)
111-
}
112-
113-
/// Test whether given patch name is unapplied.
114-
fn is_unapplied(&self, patchname: &PatchName) -> bool {
115-
self.unapplied().contains(patchname)
116-
}
117-
118-
/// Test whether given patch name is hidden.
119-
fn is_hidden(&self, patchname: &PatchName) -> bool {
120-
self.hidden().contains(patchname)
121-
}
122-
123-
/// Iterator over all patch names: applied, unapplied, and hidden.
124-
fn all_patches(&self) -> AllPatches<'_> {
125-
AllPatches::new(self.applied(), self.unapplied(), self.hidden())
126-
}
127-
128-
/// Iterator over applied and unapplied patch names.
129-
fn applied_and_unapplied(&self) -> BothPatches<'_> {
130-
BothPatches::new(self.applied(), self.unapplied())
131-
}
132-
133-
/// Iterator over unapplied and hidden patch names.
134-
fn unapplied_and_hidden(&self) -> BothPatches<'_> {
135-
BothPatches::new(self.unapplied(), self.hidden())
136-
}
137-
}
138-
13961
impl<'repo> StackStateAccess<'repo> for StackState<'repo> {
14062
fn applied(&self) -> &[PatchName] {
14163
&self.applied

0 commit comments

Comments
 (0)