1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//! Declares interfaces for position builders,
//! while offering statemachine traits to orchestrate them statisticaly.
//!
//! Converting positions is a frequent operation, so performances is a major concern.
//!
//! The statemachines qre a "zero cost" abstraction, as they are completely monomorphised.
//!
//! With all these traits it is also easier to do multiple conversions with a single traversal,
//! with no impact to performances of "mono" convertions.

pub trait Transition<O> {
    fn transit(self) -> O;
}
pub trait SetLen<IdO, O> {
    fn set(self, len: IdO) -> O;
}
pub mod top_down {
    use super::*;
    pub trait CreateBuilder {
        fn create() -> Self;
    }
    pub trait ReceiveDirName<O> {
        fn push(self, dir_name: &str) -> O;
    }
    pub trait ReceiveIdx<Idx, O> {
        fn push(self, idx: Idx) -> O;
    }
    pub trait ReceiveIdxNoSpace<Idx, O> {
        fn push(self, idx: Idx) -> O;
    }
    pub trait ReceiveOffset<IdO, O> {
        fn push(self, bytes: IdO) -> O;
    }
    pub trait ReceiveParent<IdN, O> {
        fn push(self, parent: IdN) -> O;
    }
    pub trait SetNode<IdN, O> {
        fn set_node(self, node: IdN) -> O;
    }
    pub trait SetFileName<O> {
        fn set_file_name(self, file_name: &str) -> O;
    }
    // pub trait FileSysReceiver {
    //     type InFile;
    // }

    pub trait ReceiveDir<IdN, Idx, O>:
        Sized
        + ReceiveParent<IdN, Self::SA1>
        + SetNode<IdN, O>
        + SetFileName<Self::SB1<O>>
        + Transition<Self::SB1<O>>
    {
        type SA1: ReceiveIdx<Idx, Self::SA2>;
        type SA2: ReceiveDirName<Self>;
        type SB1<OO>;
    }
    pub trait FileSysReceiver {
        type InFile<O>;
    }
    impl<IdN, Idx, O, T: FileSysReceiver> ReceiveDir<IdN, Idx, O> for T
    where
        T: ReceiveParent<IdN, T>
            + SetNode<IdN, O>
            + ReceiveIdx<Idx, T>
            + ReceiveDirName<T>
            + SetFileName<T::InFile<O>>
            + Transition<T::InFile<O>>,
    {
        type SA1 = T;
        type SA2 = T;
        type SB1<OO> = T::InFile<OO>;
    }

    pub trait ReceiveInFile<IdN, Idx, IdO, O>:
        Sized + ReceiveParent<IdN, Self::S1> + SetLen<IdO, Self::O0>
    {
        type S1: ReceiveIdx<Idx, Self::S2>;
        type S2: ReceiveOffset<IdO, Self::S3>;
        type S3: ReceiveIdxNoSpace<Idx, Self>;
        type O0: SetNode<IdN, O>;
    }
    impl<IdN, Idx, IdO, O, T> ReceiveInFile<IdN, Idx, IdO, O> for T
    where
        T: ReceiveParent<IdN, T>
            + SetNode<IdN, O>
            + ReceiveOffset<IdO, T>
            + ReceiveIdx<Idx, T>
            + SetLen<IdO, T>
            + ReceiveIdxNoSpace<Idx, T>,
    {
        type S1 = T;
        type S2 = T;
        type S3 = T;

        type O0 = T;
    }

    // Great bu try to fusion with `ReceiveInFile`s
    pub trait ReceiveInFileNoSpace<IdN, Idx, IdO, O>:
        Sized + ReceiveParent<IdN, Self::S1> + SetNode<IdN, Self::O0>
    {
        type S1: ReceiveIdx<Idx, Self::S2>;
        type S2: ReceiveOffset<IdO, Self>;
        type O0: SetLen<IdO, Self::O1>;
        type O1: Transition<O>;
    }
    impl<IdN, Idx, IdO, O, T> ReceiveInFileNoSpace<IdN, Idx, IdO, O> for T
    where
        T: ReceiveParent<IdN, T>
            + SetNode<IdN, T>
            + ReceiveOffset<IdO, T>
            + ReceiveIdx<Idx, T>
            + SetLen<IdO, T>
            + ReceiveIdxNoSpace<Idx, T>,
        T: Transition<O>,
    {
        type S1 = T;
        type S2 = T;

        type O0 = T;
        type O1 = T;
    }
}
pub mod bottom_up {
    use super::*;
    pub trait CreateBuilder {
        fn create() -> Self;
    }
    pub trait ReceiveDirName<O> {
        fn push(self, dir_name: &str) -> O;
    }
    pub trait ReceiveIdx<Idx, O> {
        fn push(self, idx: Idx) -> O;
    }
    pub trait ReceiveIdxNoSpace<Idx, O> {
        fn push(self, idx: Idx) -> O;
    }
    pub trait ReceiveOffset<IdO, O> {
        fn push(self, bytes: IdO) -> O;
    }
    pub trait ReceiveNode<IdN, O> {
        fn push(self, node: IdN) -> O;
    }
    pub trait SetRoot<IdN, O> {
        fn set_root(self, root: IdN) -> O;
    }
    pub trait FileSysReceiver {
        type InFile<O>;
    }

    pub trait ReceiveInFile<IdN, Idx, IdO, O>:
        Sized + SetLen<IdO, Self::SA1> + Transition<Self::SB1<O>>
    {
        type SA1: ReceiveNode<IdN, Self::SA2> + ReceiveDirName<Self::SB1<O>> + SetRoot<IdN, O>;
        type SA2: ReceiveOffset<IdO, Self::SA3>;
        type SA3: ReceiveIdx<Idx, Self::SA1>;
        type SB1<OO>;
    }
    impl<IdN, Idx, IdO, O, T> ReceiveInFile<IdN, Idx, IdO, O> for T
    where
        T: ReceiveIdx<Idx, T>
            + ReceiveNode<IdN, T>
            + SetRoot<IdN, O>
            + ReceiveOffset<IdO, T>
            + ReceiveIdx<Idx, T>
            + ReceiveDirName<T>
            + SetLen<IdO, T>,
        T: Transition<T>,
        T: Transition<O>,
    {
        type SA1 = T;
        type SA2 = T;
        type SA3 = T;
        type SB1<OO> = T;
    }
    pub trait ReceiveDir<IdN, Idx, O>:
        Sized + ReceiveNode<IdN, Self::S1> + SetRoot<IdN, O>
    {
        type S1: ReceiveIdx<Idx, Self>;
        type S2: ReceiveDirName<Self>;
    }
    impl<IdN, Idx, O, T> ReceiveDir<IdN, Idx, O> for T
    where
        T: ReceiveIdx<Idx, T> + ReceiveNode<IdN, T> + ReceiveDirName<T> + SetRoot<IdN, O>,
    {
        type S1 = T;
        type S2 = T;
    }
}

pub struct CompoundPositionPreparer<A, B>(A, B);

mod impl_c_p_p_receivers2 {

    use super::super::file_and_offset::Position;
    use super::super::PrimInt;
    use super::bottom_up;
    use super::top_down;
    use super::CompoundPositionPreparer;
    use super::Transition;

    impl<A: top_down::CreateBuilder, B: top_down::CreateBuilder> top_down::CreateBuilder
        for CompoundPositionPreparer<A, B>
    {
        fn create() -> Self {
            Self(
                top_down::CreateBuilder::create(),
                top_down::CreateBuilder::create(),
            )
        }
    }

    // impl<IdN, A: top_down::ReceiveParent<IdN, A>, B: top_down::ReceiveParent<IdN, B>>
    //     top_down::ReceiveParent<IdN, Self> for CompoundPositionPreparer<A, B>
    // {
    //     fn push(self, parent: IdN) -> Self {
    //         Self(self.0.push(parent), self.1.push(parent))
    //     }
    // }

    impl<IdN, IdO: PrimInt, B: top_down::ReceiveParent<IdN, B>> top_down::ReceiveParent<IdN, Self>
        for CompoundPositionPreparer<Position<std::path::PathBuf, IdO>, B>
    {
        fn push(self, parent: IdN) -> Self {
            Self(self.0, self.1.push(parent))
        }
    }

    impl<A: top_down::ReceiveDirName<A>, B: top_down::ReceiveDirName<B>>
        top_down::ReceiveDirName<Self> for CompoundPositionPreparer<A, B>
    {
        fn push(self, dir_name: &str) -> Self {
            Self(self.0.push(dir_name), self.1.push(dir_name))
        }
    }

    impl<A: bottom_up::ReceiveDirName<A>, B: bottom_up::ReceiveDirName<B>>
        bottom_up::ReceiveDirName<Self> for CompoundPositionPreparer<A, B>
    {
        fn push(self, dir_name: &str) -> Self {
            Self(self.0.push(dir_name), self.1.push(dir_name))
        }
    }

    // impl<IdN, Idx: PrimInt, IdO: PrimInt, C> top_down::ReceiveIdx<Idx, Self> for CompoundPositionPreparer<IdN, Idx, IdO, C> {
    //     fn push(mut self, idx: Idx) -> Self {
    //         self.offsets.push(idx);
    //         self
    //     }
    // }

    impl<Idx: PrimInt, A: top_down::ReceiveIdx<Idx, A>, B: top_down::ReceiveIdx<Idx, B>>
        top_down::ReceiveIdx<Idx, Self> for CompoundPositionPreparer<A, B>
    {
        fn push(self, idx: Idx) -> Self {
            Self(self.0.push(idx), self.1.push(idx))
        }
    }

    // impl<IdN, Idx: PrimInt, IdO: PrimInt, C> top_down::ReceiveIdxNoSpace<Idx, Self> for CompoundPositionPreparer<IdN, Idx, IdO, C> {
    //     fn push(self, _idx: Idx) -> Self {
    //         //self.offsets.push(idx);
    //         self
    //     }
    // }

    impl<Idx: PrimInt, A: top_down::ReceiveIdxNoSpace<Idx, A>, B: top_down::ReceiveIdxNoSpace<Idx, B>>
        top_down::ReceiveIdxNoSpace<Idx, Self> for CompoundPositionPreparer<A, B>
    {
        fn push(self, idx: Idx) -> Self {
            Self(self.0.push(idx), self.1.push(idx))
        }
    }

    impl<A, B> top_down::FileSysReceiver for CompoundPositionPreparer<A, B> {
        type InFile<O> = Self;
    }

    impl<IdO: PrimInt, A: top_down::ReceiveOffset<IdO, A>, B: top_down::ReceiveOffset<IdO, B>>
        top_down::ReceiveOffset<IdO, Self> for CompoundPositionPreparer<A, B>
    {
        fn push(self, bytes: IdO) -> Self {
            Self(self.0.push(bytes), self.1.push(bytes))
        }
    }
    impl<IdO: PrimInt, A: super::SetLen<IdO, A>, B: super::SetLen<IdO, B>> super::SetLen<IdO, Self>
        for CompoundPositionPreparer<A, B>
    {
        fn set(self, len: IdO) -> Self {
            Self(self.0.set(len), self.1.set(len))
        }
    }
    // impl<IdN, A: top_down::SetNode<IdN, A>, B: top_down::SetNode<IdN, B>>
    //     top_down::SetNode<IdN, Self> for CompoundPositionPreparer<A, B>
    // {
    //     fn set_node(self, node: IdN) -> Self {
    //         Self(self.0.set(len), self.1.set(len))
    //     }
    // }
    // impl<IdN, IdO: PrimInt, B: top_down::SetNode<IdN, BB>, BB>
    //     top_down::SetNode<
    //         IdN,
    //         CompoundPositionPreparer<
    //             super::super::file_and_offset::Position<std::path::PathBuf, IdO>,
    //             BB,
    //         >,
    //     >
    //     for CompoundPositionPreparer<
    //         super::super::file_and_offset::Position<std::path::PathBuf, IdO>,
    //         B,
    //     >
    // {
    //     fn set_node(
    //         self,
    //         node: IdN,
    //     ) -> CompoundPositionPreparer<
    //         super::super::file_and_offset::Position<std::path::PathBuf, IdO>,
    //         BB,
    //     > {
    //         CompoundPositionPreparer(self.0, self.1.set_node(node))
    //     }
    // }
    impl<IdN, IdO: PrimInt, B: top_down::SetNode<IdN, BB>, BB>
        top_down::SetNode<
            IdN,
            (
                super::super::file_and_offset::Position<std::path::PathBuf, IdO>,
                BB,
            ),
        >
        for CompoundPositionPreparer<
            super::super::file_and_offset::Position<std::path::PathBuf, IdO>,
            B,
        >
    {
        fn set_node(
            self,
            node: IdN,
        ) -> (
            super::super::file_and_offset::Position<std::path::PathBuf, IdO>,
            BB,
        ) {
            (self.0, self.1.set_node(node))
        }
    }

    impl<A: top_down::SetFileName<A>, B: top_down::SetFileName<B>> top_down::SetFileName<Self>
        for CompoundPositionPreparer<A, B>
    {
        fn set_file_name(self, file_name: &str) -> Self {
            Self(
                self.0.set_file_name(file_name),
                self.1.set_file_name(file_name),
            )
        }
    }
    impl<A: Transition<A>, B: Transition<B>> Transition<Self> for CompoundPositionPreparer<A, B> {
        fn transit(self) -> Self {
            Self(self.0.transit(), self.1.transit())
        }
    }
    // impl<A: Into<AA>, B: Into<BB>, AA, BB> Transition<(AA, BB)> for CompoundPositionPreparer<A, B> {
    //     fn transit(self) -> (AA, BB) {
    //         (self.0.into(), self.1.into())
    //     }
    // }
}