|
2 | 2 | #![forbid(unsafe_code, rust_2018_idioms)]
|
3 | 3 |
|
4 | 4 | ///
|
5 |
| -pub mod visit { |
6 |
| - use git_object::immutable; |
7 |
| - |
8 |
| - #[derive(Default, Clone)] |
9 |
| - pub struct State { |
10 |
| - buf1: Vec<u8>, |
11 |
| - buf2: Vec<u8>, |
12 |
| - } |
13 |
| - |
14 |
| - pub struct Changes<'a>(Option<&'a immutable::Tree<'a>>); |
15 |
| - |
16 |
| - impl<'a, T> From<T> for Changes<'a> |
17 |
| - where |
18 |
| - T: Into<Option<&'a immutable::Tree<'a>>>, |
19 |
| - { |
20 |
| - fn from(v: T) -> Self { |
21 |
| - Changes(v.into()) |
22 |
| - } |
23 |
| - } |
24 |
| - |
25 |
| - mod changes { |
26 |
| - use crate::visit; |
27 |
| - use git_hash::{oid, ObjectId}; |
28 |
| - use git_object::immutable; |
29 |
| - use quick_error::quick_error; |
30 |
| - |
31 |
| - const EMPTY_TREE: immutable::Tree<'static> = immutable::Tree::empty(); |
32 |
| - |
33 |
| - quick_error! { |
34 |
| - #[derive(Debug)] |
35 |
| - pub enum Error { |
36 |
| - NotFound(oid: ObjectId) { |
37 |
| - display("The object {} referenced by the tree was not found in the database", oid) |
38 |
| - } |
39 |
| - Cancelled { |
40 |
| - display("The delegate cancelled the operation") |
41 |
| - } |
42 |
| - } |
43 |
| - } |
44 |
| - |
45 |
| - impl<'a> visit::Changes<'a> { |
46 |
| - /// Returns the changes that need to be applied to `self` to get `other`. |
47 |
| - pub fn to_obtain_tree<LocateFn>( |
48 |
| - &self, |
49 |
| - _other: &git_object::immutable::Tree<'_>, |
50 |
| - _state: &mut visit::State, |
51 |
| - _locate: LocateFn, |
52 |
| - _delegate: &mut impl visit::Record, |
53 |
| - ) -> Result<(), Error> |
54 |
| - where |
55 |
| - LocateFn: for<'b> FnMut(&oid, &'b mut Vec<u8>) -> Option<immutable::Object<'b>>, |
56 |
| - { |
57 |
| - let _this = *self.0.as_ref().unwrap_or(&&EMPTY_TREE); |
58 |
| - todo!("changes tree to tree") |
59 |
| - } |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - pub mod record { |
64 |
| - use git_hash::ObjectId; |
65 |
| - use git_object::{bstr::BStr, tree}; |
66 |
| - |
67 |
| - pub enum Change { |
68 |
| - Addition { mode: tree::Mode, oid: ObjectId }, |
69 |
| - Copy, |
70 |
| - Deletion, |
71 |
| - Modification, |
72 |
| - Renaming, |
73 |
| - Type, |
74 |
| - } |
75 |
| - |
76 |
| - #[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)] |
77 |
| - pub enum PathComponentUpdateMode { |
78 |
| - Replace, |
79 |
| - Push, |
80 |
| - } |
81 |
| - |
82 |
| - #[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)] |
83 |
| - pub struct PathComponent<'a> { |
84 |
| - pub name: &'a BStr, |
85 |
| - /// An ID referring uniquely to the path built thus far. Used to keep track of source paths |
86 |
| - /// in case of [renames][Change::Rename] and [copies][Change::Copy]. |
87 |
| - pub id: usize, |
88 |
| - } |
89 |
| - |
90 |
| - #[derive(Clone, Copy, PartialOrd, PartialEq, Ord, Eq, Hash)] |
91 |
| - pub enum Action { |
92 |
| - Continue, |
93 |
| - Cancel, |
94 |
| - } |
95 |
| - |
96 |
| - pub trait Record { |
97 |
| - fn update_path_component(&mut self, component: PathComponent<'_>, mode: PathComponentUpdateMode); |
98 |
| - fn pop_path_component(&mut self); |
99 |
| - fn record(&mut self, change: Change) -> Action; |
100 |
| - } |
101 |
| - |
102 |
| - #[cfg(test)] |
103 |
| - mod tests { |
104 |
| - use super::*; |
105 |
| - |
106 |
| - #[test] |
107 |
| - fn size_of_change() { |
108 |
| - assert_eq!( |
109 |
| - std::mem::size_of::<Change>(), |
110 |
| - 22, |
111 |
| - "this type shouldn't grow without us knowing" |
112 |
| - ) |
113 |
| - } |
114 |
| - } |
115 |
| - } |
116 |
| - pub use record::Record; |
117 |
| - |
118 |
| - pub mod recorder { |
119 |
| - use crate::visit::record; |
120 |
| - use git_hash::ObjectId; |
121 |
| - use git_object::{ |
122 |
| - bstr::{BStr, BString, ByteSlice, ByteVec}, |
123 |
| - tree, |
124 |
| - }; |
125 |
| - use std::{ops::Deref, path::PathBuf}; |
126 |
| - |
127 |
| - #[derive(Clone, Debug, PartialEq, Eq)] |
128 |
| - pub enum Change { |
129 |
| - Addition { |
130 |
| - mode: tree::Mode, |
131 |
| - oid: ObjectId, |
132 |
| - path: PathBuf, |
133 |
| - }, |
134 |
| - } |
135 |
| - |
136 |
| - pub type Changes = Vec<Change>; |
137 |
| - |
138 |
| - #[derive(Clone, Debug, Default)] |
139 |
| - pub struct Recorder { |
140 |
| - current_component: BString, |
141 |
| - pub records: Vec<Change>, |
142 |
| - } |
143 |
| - |
144 |
| - impl Recorder { |
145 |
| - fn pop_element(&mut self) { |
146 |
| - if let Some(pos) = self.current_component.rfind_byte(b'/') { |
147 |
| - self.current_component.resize(pos, 0); |
148 |
| - } |
149 |
| - } |
150 |
| - |
151 |
| - fn push_element(&mut self, name: &BStr) { |
152 |
| - self.current_component.push(b'/'); |
153 |
| - self.current_component.push_str(name); |
154 |
| - } |
155 |
| - } |
156 |
| - |
157 |
| - impl record::Record for Recorder { |
158 |
| - fn update_path_component( |
159 |
| - &mut self, |
160 |
| - component: record::PathComponent<'_>, |
161 |
| - mode: record::PathComponentUpdateMode, |
162 |
| - ) { |
163 |
| - use record::PathComponentUpdateMode::*; |
164 |
| - match mode { |
165 |
| - Push => self.push_element(component.name), |
166 |
| - Replace => { |
167 |
| - self.pop_element(); |
168 |
| - self.push_element(component.name); |
169 |
| - } |
170 |
| - } |
171 |
| - } |
172 |
| - |
173 |
| - fn pop_path_component(&mut self) { |
174 |
| - self.pop_element(); |
175 |
| - } |
176 |
| - |
177 |
| - fn record(&mut self, change: record::Change) -> record::Action { |
178 |
| - use record::Change::*; |
179 |
| - self.records.push(match change { |
180 |
| - Addition { mode, oid } => Change::Addition { |
181 |
| - mode, |
182 |
| - oid, |
183 |
| - path: self.current_component.deref().clone().into_path_buf_lossy(), |
184 |
| - }, |
185 |
| - _ => todo!("record other kinds of changes"), |
186 |
| - }); |
187 |
| - record::Action::Continue |
188 |
| - } |
189 |
| - } |
190 |
| - } |
191 |
| - pub use recorder::Recorder; |
192 |
| -} |
| 5 | +pub mod visit; |
0 commit comments