assemble-core 0.2.0

The core crate of the assemble-rs package
Documentation
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/// Defines types of file collections and the FileCollection trait
use std::collections::{HashSet, LinkedList, VecDeque};

use std::env::JoinPathsError;
use std::ffi::OsString;
use std::fmt::{Debug, Formatter};

use std::iter::FusedIterator;
use std::ops::{Add, AddAssign};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;

use itertools::Itertools;
use walkdir::WalkDir;

use crate::exception::BuildException;

use crate::identifier::TaskId;
use crate::lazy_evaluation::ProviderExt;
use crate::lazy_evaluation::{IntoProvider, Prop, Provider};
use crate::project::buildable::{Buildable, BuiltByContainer, IntoBuildable};

use crate::project::ProjectResult;
use crate::utilities::{AndSpec, Spec, True};
use crate::{BuildResult, Project};
use crate::error::PayloadError;

/// A file set is a collection of files. File collections are intended to be live.
pub trait FileCollection {
    /// Gets the files contained by this collection.
    fn files(&self) -> HashSet<PathBuf>;
    /// Gets the files contained by this collection. Is fallible.
    fn try_files(&self) -> BuildResult<HashSet<PathBuf>> {
        Ok(self.files())
    }
    /// Gets whether this file collection is empty or not
    fn is_empty(&self) -> bool {
        self.files().is_empty()
    }
    /// Get this file collection as a path
    fn path(&self) -> Result<OsString, JoinPathsError> {
        std::env::join_paths(self.files())
    }
}

macro_rules! implement_file_collection {
    ($ty:tt) => {
        impl<P: AsRef<Path>> FileCollection for $ty<P> {
            fn files(&self) -> HashSet<PathBuf> {
                self.iter().map(|p| p.as_ref().to_path_buf()).collect()
            }
        }
    };
}
implement_file_collection!(HashSet);
implement_file_collection!(Vec);
implement_file_collection!(VecDeque);
implement_file_collection!(LinkedList);
// impl<P : AsRef<Path>> FileCollection for HashSet<P> {
//     fn files(&self) -> HashSet<PathBuf> {
//         self.iter()
//             .map(|p| p.as_ref().to_path_buf())
//             .collect()
//     }
// }
//
// impl<P : AsRef<Path>> FileCollection for Vec<P> {
//     fn files(&self) -> HashSet<PathBuf> {
//         self.iter()
//             .map(|p| p.as_ref().to_path_buf())
//             .collect()
//     }
// }

impl FileCollection for PathBuf {
    fn files(&self) -> HashSet<PathBuf> {
        HashSet::from_iter([self.clone()])
    }
}

#[derive(Clone)]
pub struct FileSet {
    filter: Arc<dyn FileFilter>,
    built_by: BuiltByContainer,
    components: Vec<Component>,
}

impl Debug for FileSet {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if f.alternate() {
            let files = self.files();
            f.debug_set().entries(files).finish()
        } else {
            f.debug_struct("FileSet")
                .field("components", &self.components)
                .finish_non_exhaustive()
        }
    }
}

impl FileSet {
    pub fn new() -> Self {
        Self {
            filter: Arc::new(True::new()),
            built_by: BuiltByContainer::default(),
            components: vec![],
        }
    }

    pub fn with_path(path: impl AsRef<Path>) -> Self {
        Self {
            filter: Arc::new(True::new()),
            built_by: BuiltByContainer::default(),
            components: vec![Component::Path(path.as_ref().to_path_buf())],
        }
    }

    pub fn with_path_providers<I, P>(providers: I) -> Self
    where
        I: IntoIterator<Item = P>,
        P: IntoProvider<PathBuf>,
        <P as IntoProvider<PathBuf>>::Provider: 'static,
    {
        let mut output = Self::new();
        for provider in providers {
            output += FileSet::with_provider(provider);
        }
        output
    }

    pub fn with_provider<F: FileCollection, P: IntoProvider<F>>(fc_provider: P) -> Self
    where
        F: Send + Sync + Clone + 'static,
        P::Provider: 'static,
    {
        let mut prop: Prop<FileSet> = Prop::default();
        let provider = fc_provider.into_provider();
        prop.set_with(provider.map(|f: F| FileSet::from_iter(f.files())))
            .unwrap();
        let component = Component::Provider(prop);
        Self {
            filter: Arc::new(True::new()),
            built_by: BuiltByContainer::default(),
            components: vec![component],
        }
    }

    pub fn built_by<B: IntoBuildable>(&mut self, b: B)
    where
        <B as IntoBuildable>::Buildable: 'static,
    {
        self.built_by.add(b);
    }

    pub fn join(self, other: Self) -> Self {
        Self {
            components: vec![Component::Collection(self), Component::Collection(other)],
            ..Default::default()
        }
    }

    pub fn insert<T: Into<FileSet>>(&mut self, fileset: T) {
        *self += fileset;
    }

    pub fn iter(&self) -> FileIterator {
        self.into_iter()
    }

    /// Adds a filter to a fileset
    pub fn filter<F: FileFilter + 'static>(self, filter: F) -> Self {
        let mut files = self;
        let prev = std::mem::replace(&mut files.filter, Arc::new(True::new()));
        let and = AndSpec::new(prev, filter);
        files.filter = Arc::new(and);
        files
    }
}

impl Default for FileSet {
    fn default() -> Self {
        Self::new()
    }
}

impl<'f> IntoIterator for &'f FileSet {
    type Item = PathBuf;
    type IntoIter = FileIterator<'f>;

    fn into_iter(self) -> Self::IntoIter {
        FileIterator {
            components: &self.components[..],
            filters: &*self.filter,
            index: 0,
            current_iterator: None,
        }
    }
}

impl<'f> IntoIterator for FileSet {
    type Item = PathBuf;
    type IntoIter = std::vec::IntoIter<PathBuf>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter().collect::<Vec<_>>().into_iter()
    }
}

impl FileCollection for FileSet {
    fn files(&self) -> HashSet<PathBuf> {
        self.iter().collect()
    }

    fn try_files(&self) -> BuildResult<HashSet<PathBuf>> {
        Ok(self
            .components
            .iter()
            .map(|c| c.try_files())
            .collect::<Result<Vec<HashSet<_>>, _>>()?
            .into_iter()
            .flatten()
            .filter(|p| self.filter.accept(p))
            .collect())
    }
}

impl<F: Into<FileSet>> Add<F> for FileSet {
    type Output = Self;

    fn add(self, rhs: F) -> Self::Output {
        self.join(rhs.into())
    }
}

impl<F: Into<FileSet>> AddAssign<F> for FileSet {
    fn add_assign(&mut self, rhs: F) {
        let old = std::mem::take(self);
        *self = old.join(rhs.into())
    }
}

impl<P: AsRef<Path>> From<P> for FileSet {
    fn from(path: P) -> Self {
        Self::with_path(path)
    }
}

impl Buildable for FileSet {
    fn get_dependencies(&self, project: &Project) -> ProjectResult<HashSet<TaskId>> {
        self.built_by.get_dependencies(project)
    }
}

impl<P: AsRef<Path>> FromIterator<P> for FileSet {
    fn from_iter<T: IntoIterator<Item = P>>(iter: T) -> Self {
        iter.into_iter()
            .map(|p: P| FileSet::with_path(p))
            .reduce(|accum, next| accum + next)
            .unwrap_or_default()
    }
}

impl Provider<FileSet> for FileSet {
    fn try_get(&self) -> Option<FileSet> {
        Some(self.clone())
    }
}

#[derive(Clone)]
pub enum Component {
    Path(PathBuf),
    Collection(FileSet),
    Provider(Prop<FileSet>),
}

impl Debug for Component {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        if f.alternate() {
            match self {
                Component::Path(p) => {
                    write!(f, "{:?}", p)
                }
                Component::Collection(c) => {
                    write!(f, "{:#?}", c)
                }
                Component::Provider(p) => {
                    write!(f, "{:#?}", p)
                }
            }
        } else {
            match self {
                Component::Path(p) => f.debug_tuple("Path").field(p).finish(),
                Component::Collection(c) => f.debug_tuple("Collection").field(c).finish(),
                Component::Provider(p) => f.debug_tuple("Provider").field(p).finish(),
            }
        }
    }
}

impl Component {
    pub fn iter(&self) -> Box<dyn Iterator<Item = PathBuf> + '_> {
        match self {
            Component::Path(p) => {
                if p.is_file() || !p.exists() {
                    Box::new(Some(p.clone()).into_iter())
                } else {
                    Box::new(
                        WalkDir::new(p)
                            .into_iter()
                            .map_ok(|entry| entry.into_path())
                            .map(|res| res.unwrap()),
                    )
                }
            }
            Component::Collection(c) => Box::new(c.iter()),
            Component::Provider(pro) => {
                let component = pro.get();
                Box::new(component.into_iter())
            }
        }
    }
}

impl FileCollection for Component {
    fn files(&self) -> HashSet<PathBuf> {
        self.iter().collect()
    }

    fn try_files(&self) -> BuildResult<HashSet<PathBuf>> {
        Ok(match self {
            Component::Path(p) => {
                if p.is_file() || !p.exists() {
                    Box::new(Some(p.clone()).into_iter()) as Box<dyn Iterator<Item = PathBuf> + '_>
                } else {
                    Box::new(
                        WalkDir::new(p)
                            .into_iter()
                            .map_ok(|entry| entry.into_path())
                            .map(|r| r.map_err(BuildException::new))
                            .collect::<Result<HashSet<PathBuf>, _>>()?
                            .into_iter(),
                    ) as Box<dyn Iterator<Item = PathBuf> + '_>
                }
            }
            Component::Collection(c) => {
                Box::new(c.iter()) as Box<dyn Iterator<Item = PathBuf> + '_>
            }
            Component::Provider(pro) => {
                let component = pro.fallible_get().map_err(PayloadError::<BuildException>::new)?;
                Box::new(component.into_iter()) as Box<dyn Iterator<Item = PathBuf> + '_>
            }
        }
        .collect())
    }
}

// impl Debug for dyn Provider<Component> {
//     fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
//         write!(f, "Component Provider")
//     }
// }

impl<'f> IntoIterator for &'f Component {
    type Item = PathBuf;
    type IntoIter = Box<dyn Iterator<Item = PathBuf> + 'f>;

    fn into_iter(self) -> Self::IntoIter {
        self.iter()
    }
}

/// An iterator over file components.
pub struct FileIterator<'files> {
    components: &'files [Component],
    filters: &'files dyn FileFilter,
    index: usize,
    current_iterator: Option<Box<dyn Iterator<Item = PathBuf> + 'files>>,
}

impl<'files> Debug for FileIterator<'files> {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("FileIterator")
            .field("components", &self.components)
            .field("index", &self.index)
            .finish_non_exhaustive()
    }
}

impl<'files> FileIterator<'files> {
    fn next_iterator(&mut self) -> Option<Box<dyn Iterator<Item = PathBuf> + 'files>> {
        if self.index == self.components.len() {
            return None;
        }

        let output = Some(self.components[self.index].iter());
        self.index += 1;
        output
    }

    fn get_next_path(&mut self) -> Option<PathBuf> {
        if self.index == self.components.len() {
            return None;
        }
        loop {
            if self.current_iterator.is_none() {
                self.current_iterator = self.next_iterator();
            }

            if let Some(iterator) = &mut self.current_iterator {
                for path in iterator.by_ref() {
                    if self.filters.accept(&path) {
                        return Some(path);
                    }
                }
                self.current_iterator = None;
            } else {
                return None;
            }
        }
    }
}

impl<'files> Iterator for FileIterator<'files> {
    type Item = PathBuf;

    fn next(&mut self) -> Option<Self::Item> {
        self.get_next_path()
    }
}

impl<'files> FusedIterator for FileIterator<'files> {}

pub trait FileFilter: Spec<Path> + Send + Sync {}

assert_obj_safe!(FileFilter);

impl<F> FileFilter for F where F: Spec<Path> + Send + Sync + ?Sized {}

impl Spec<Path> for glob::Pattern {
    fn accept(&self, value: &Path) -> bool {
        self.matches_path(value)
    }
}

impl Spec<Path> for &str {
    fn accept(&self, value: &Path) -> bool {
        glob::Pattern::from_str(self).unwrap().accept(value)
    }
}