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
use core::fmt;
use std::{
    fmt::{Debug, Display},
    path::PathBuf,
};

use super::PrimInt;

#[derive(PartialEq, Eq, Hash, Clone, Default)]
pub struct Position<F, T: PrimInt> {
    file: F,
    offset: T,
    len: T,
}

impl<F, T: PrimInt> Position<F, T> {
    pub fn new(file: F, offset: T, len: T) -> Self {
        Self { file, offset, len }
    }
    pub fn inc_offset(&mut self, x: T) {
        self.offset += x;
    }
    pub fn set_len(&mut self, x: T) {
        self.len = x;
    }
    pub fn range(&self) -> std::ops::Range<T> {
        self.offset..(self.offset + self.len)
    }
}

impl<F: std::ops::Deref, T: PrimInt> Position<F, T> {
    pub fn file(&self) -> &F::Target {
        self.file.deref()
    }
}

impl<T: PrimInt> Position<PathBuf, T> {
    pub fn inc_path(&mut self, s: &str) {
        self.file.push(s);
    }
}

impl<F: Debug, T: PrimInt> Debug for Position<F, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Position")
            .field("file", &self.file)
            .field("offset", &self.offset)
            .field("len", &self.len)
            .finish()
    }
}

impl<T: PrimInt + Display> Display for Position<PathBuf, T> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{{\"offset\":{},\"len\":{},\"file\":{:?}}}",
            &self.offset, &self.len, &self.file
        )
    }
}

// TODO use an interface for TopDownPositionBuilder, should actually be the same as position here, this way you can see the generated pos as a DTO
// TODO in the same way finishing a prepare struct could directly be converted into a position, or be an accumulator itself (actually better for some structs)
impl<IdN, Idx, IdO: PrimInt> Into<super::file_and_offset::Position<PathBuf, IdO>>
    for super::spaces_related::TopDownPositionBuilder<IdN, Idx, IdO>
{
    fn into(self) -> super::file_and_offset::Position<PathBuf, IdO> {
        // TODO how to handle position of directory ?
        let range = self.range.unwrap();
        let len = range.end - range.start;
        super::file_and_offset::Position {
            file: self.file,
            offset: range.start,
            len,
        }
    }
}

use super::spaces_related::{NoSpacePrepareParams, SealedFileTopDownPosBuilder, TopDownPosBuilder};

impl<IdN, Idx: PrimInt, IdO: PrimInt + Default>
    TopDownPosBuilder<IdN, Idx, IdO, NoSpacePrepareParams<Idx>> for Position<PathBuf, IdO>
{
    type Prepared = Position<PathBuf, IdO>;

    type SealedFile = Position<PathBuf, IdO>;

    fn seal_path(mut self, file_name: &str) -> Self::SealedFile {
        self.file.push(file_name);
        self
    }

    fn seal_without_path(self) -> Self::SealedFile {
        self
    }

    fn push(&mut self, parent: IdN, offset: Idx, dir_name: &str, _additional: ()) {
        self.file.push(dir_name);
    }

    fn finish(self, node: IdN) -> Self::Prepared {
        todo!("how exactly should directories be handled")
    }
}
impl<IdN, Idx: PrimInt, IdO: PrimInt>
    SealedFileTopDownPosBuilder<IdN, Idx, IdO, NoSpacePrepareParams<Idx>> for Position<PathBuf, IdO>
{
    type Prepared = Position<PathBuf, IdO>;

    fn push(&mut self, parent: IdN, idx: Idx, offset: IdO, (no_s_idx,): (Idx,)) {
        self.offset += offset;
    }

    fn finish(self, node: IdN, len: Idx, _additional: ()) -> Self::Prepared {
        assert_eq!(self.len, num::zero());
        let len = num::cast(len).unwrap();
        Self::Prepared {
            file: self.file,
            offset: self.offset,
            len,
        }
    }
}
mod impl_receivers {
    use std::path::PathBuf;
    use super::super::building;
    use building::top_down;
    use building::bottom_up;
    use super::super::PrimInt;


    impl<IdO: PrimInt> top_down::CreateBuilder for super::Position<PathBuf, IdO> {
        fn create() -> Self {
            Self {
                file: Default::default(),
                offset: num::zero(),
                len: num::zero(),
            }
        }
    }


    impl<IdO: PrimInt> bottom_up::CreateBuilder for super::Position<PathBuf, IdO> {
        fn create() -> Self {
            Self {
                file: Default::default(),
                offset: num::zero(),
                len: num::zero(),
            }
        }
    }

    impl<IdN, IdO: PrimInt> top_down::ReceiveParent<IdN, Self> for super::Position<PathBuf, IdO> {
        fn push(self, _parent: IdN) -> Self {
            self
        }
    }

    impl<IdN, IdO: PrimInt> bottom_up::ReceiveNode<IdN, Self> for super::Position<PathBuf, IdO> {
        fn push(self, _node: IdN) -> Self {
            self
        }
    }

    impl<IdN, IdO: PrimInt> bottom_up::SetRoot<IdN, Self> for super::Position<PathBuf, IdO> {
        fn set_root(self, _root: IdN) -> Self {
            self
        }
    }

    impl<IdN, IdO: PrimInt> top_down::SetNode<IdN, Self> for super::Position<PathBuf, IdO> {
        fn set_node(self, _node: IdN) -> Self {
            self
        }
    }

    impl<IdO: PrimInt> top_down::ReceiveDirName<Self> for super::Position<PathBuf, IdO> {
        fn push(mut self, dir_name: &str) -> Self {
            self.file.push(dir_name);
            self
        }
    }

    impl<IdO: PrimInt> bottom_up::ReceiveDirName<Self> for super::Position<PathBuf, IdO> {
        fn push(mut self, dir_name: &str) -> Self {
            self.file = std::path::PathBuf::from(dir_name).join(self.file);
            self
        }
    }

    impl<IdO: PrimInt> top_down::SetFileName<Self> for super::Position<PathBuf, IdO> {
        fn set_file_name(mut self, file_name: &str) -> Self {
            self.file.push(file_name);
            self
        }
    }

    impl<Idx, IdO: PrimInt> top_down::ReceiveIdx<Idx, Self> for super::Position<PathBuf, IdO> {
        fn push(self, _idx: Idx) -> Self {
            self
        }
    }

    impl<Idx, IdO: PrimInt> bottom_up::ReceiveIdx<Idx, Self> for super::Position<PathBuf, IdO> {
        fn push(self, _idx: Idx) -> Self {
            self
        }
    }

    impl<Idx, IdO: PrimInt> top_down::ReceiveIdxNoSpace<Idx, Self> for super::Position<PathBuf, IdO> {
        fn push(self, _idx: Idx) -> Self {
            self
        }
    }

    impl<IdO: PrimInt> top_down::ReceiveOffset<IdO, Self> for super::Position<PathBuf, IdO> {
        fn push(mut self, offset: IdO) -> Self {
            self.offset += offset;
            self
        }
    }

    impl<IdO: PrimInt> bottom_up::ReceiveOffset<IdO, Self> for super::Position<PathBuf, IdO> {
        fn push(mut self, offset: IdO) -> Self {
            self.offset += offset;
            self
        }
    }

    impl<IdO: PrimInt> building::SetLen<IdO, Self> for super::Position<PathBuf, IdO> {
        fn set(mut self, len: IdO) -> Self {
            self.len = len;
            self
        }
    }

    impl<IdO: PrimInt> top_down::FileSysReceiver for super::Position<PathBuf, IdO> {
        type InFile<O> = Self;
    }

    impl<IdO: PrimInt> building::Transition<super::Position<PathBuf, IdO>> for super::Position<PathBuf, IdO> {
        fn transit(self) -> super::Position<PathBuf, IdO> {
            self
        }
    }

    // impl<IdN, Idx, IdO: PrimInt> top_down::ReceiveInFile<IdN, Idx, Self> for super::Position<PathBuf, IdO> {
    //     type S1 = Self;

    //     type S2 = Self;

    //     fn finish(self) -> Self {
    //         self
    //     }
    // }
    // impl<IdN, Idx, IdO: PrimInt> top_down::ReceiveDir<IdN, Idx, Self> for super::Position<PathBuf, IdO> {
    //     type SA1 = Self;

    //     type SA2 = Self;

    //     type SB1 = Self;

    //     fn go_inside_file(mut self, file_name: &str) -> Self::SB1 {
    //         self.file.push(file_name);
    //         self
    //     }

    //     fn finish(self) -> Self {
    //         self
    //     }
    // }
}