|
15 | 15 | // You should have received a copy of the GNU General Public License |
16 | 16 | // along with this program. If not, see <https://www.gnu.org/licenses/>. |
17 | 17 |
|
18 | | -//! A model of a general VCS History. |
19 | | -
|
20 | | -use nonempty::NonEmpty; |
| 18 | +//! A model of a general VCS. |
21 | 19 |
|
22 | 20 | pub mod git; |
23 | | - |
24 | | -/// A non-empty bag of artifacts which are used to |
25 | | -/// derive a [`crate::file_system::Directory`] view. Examples of artifacts |
26 | | -/// would be commits in Git or patches in Pijul. |
27 | | -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] |
28 | | -pub struct History<A>(pub NonEmpty<A>); |
29 | | - |
30 | | -impl<A> History<A> { |
31 | | - /// Create a new `History` consisting of one artifact. |
32 | | - pub fn new(a: A) -> Self { |
33 | | - History(NonEmpty::new(a)) |
34 | | - } |
35 | | - |
36 | | - /// Push an artifact to the end of the `History`. |
37 | | - pub fn push(&mut self, a: A) { |
38 | | - self.0.push(a) |
39 | | - } |
40 | | - |
41 | | - /// Iterator over the artifacts. |
42 | | - pub fn iter(&self) -> impl Iterator<Item = &A> { |
43 | | - self.0.iter() |
44 | | - } |
45 | | - |
46 | | - /// Get the firest artifact in the `History`. |
47 | | - pub fn first(&self) -> &A { |
48 | | - self.0.first() |
49 | | - } |
50 | | - |
51 | | - /// Get the length of `History` (aka the artefacts count) |
52 | | - pub fn len(&self) -> usize { |
53 | | - self.0.len() |
54 | | - } |
55 | | - |
56 | | - /// Check if `History` is empty |
57 | | - pub fn is_empty(&self) -> bool { |
58 | | - self.0.is_empty() |
59 | | - } |
60 | | - |
61 | | - /// Given that the `History` is topological order from most |
62 | | - /// recent artifact to least recent, `find_suffix` gets returns |
63 | | - /// the history up until the point of the given artifact. |
64 | | - /// |
65 | | - /// This operation may fail if the artifact does not exist in |
66 | | - /// the given `History`. |
67 | | - pub fn find_suffix(&self, artifact: &A) -> Option<Self> |
68 | | - where |
69 | | - A: Clone + PartialEq, |
70 | | - { |
71 | | - let new_history: Option<NonEmpty<A>> = NonEmpty::from_slice( |
72 | | - &self |
73 | | - .iter() |
74 | | - .cloned() |
75 | | - .skip_while(|current| *current != *artifact) |
76 | | - .collect::<Vec<_>>(), |
77 | | - ); |
78 | | - |
79 | | - new_history.map(History) |
80 | | - } |
81 | | - |
82 | | - /// Apply a function from `A` to `B` over the `History` |
83 | | - pub fn map<F, B>(self, f: F) -> History<B> |
84 | | - where |
85 | | - F: FnMut(A) -> B, |
86 | | - { |
87 | | - History(self.0.map(f)) |
88 | | - } |
89 | | - |
90 | | - /// Find an artifact in the `History`. |
91 | | - /// |
92 | | - /// The function provided should return `Some` if the item is the desired |
93 | | - /// output and `None` otherwise. |
94 | | - pub fn find<F, B>(&self, f: F) -> Option<B> |
95 | | - where |
96 | | - F: Fn(&A) -> Option<B>, |
97 | | - { |
98 | | - self.iter().find_map(f) |
99 | | - } |
100 | | - |
101 | | - /// Find an atrifact in the given `History` using the artifacts ID. |
102 | | - /// |
103 | | - /// This operation may fail if the artifact does not exist in the history. |
104 | | - pub fn find_in_history<Identifier, F>(&self, identifier: &Identifier, id_of: F) -> Option<A> |
105 | | - where |
106 | | - A: Clone, |
107 | | - F: Fn(&A) -> Identifier, |
108 | | - Identifier: PartialEq, |
109 | | - { |
110 | | - self.iter() |
111 | | - .find(|artifact| { |
112 | | - let current_id = id_of(artifact); |
113 | | - *identifier == current_id |
114 | | - }) |
115 | | - .cloned() |
116 | | - } |
117 | | - |
118 | | - /// Find all occurences of an artifact in a bag of `History`s. |
119 | | - pub fn find_in_histories<Identifier, F>( |
120 | | - histories: Vec<Self>, |
121 | | - identifier: &Identifier, |
122 | | - id_of: F, |
123 | | - ) -> Vec<Self> |
124 | | - where |
125 | | - A: Clone, |
126 | | - F: Fn(&A) -> Identifier + Copy, |
127 | | - Identifier: PartialEq, |
128 | | - { |
129 | | - histories |
130 | | - .into_iter() |
131 | | - .filter(|history| history.find_in_history(identifier, id_of).is_some()) |
132 | | - .collect() |
133 | | - } |
134 | | -} |
0 commit comments