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
use crate::shared::file_like::FileLike;
use crate::shared::lazy::LazyTree;
use crate::shared::staged::StagedTree;
use crate::shared::{SharedErrorKind, SharedResult};
use std::collections::HashSet;
use std::fmt::Debug;
use uuid::Uuid;

pub trait TreeLike: Sized {
    type F: FileLike + Debug;

    // todo: iterator using const generics
    fn ids(&self) -> HashSet<&Uuid>;
    fn maybe_find(&self, id: &Uuid) -> Option<&Self::F>;

    fn find(&self, id: &Uuid) -> SharedResult<&Self::F> {
        self.maybe_find(id)
            .ok_or_else(|| SharedErrorKind::FileNonexistent.into())
    }

    fn maybe_find_parent<F2: FileLike>(&self, file: &F2) -> Option<&Self::F> {
        self.maybe_find(file.parent())
    }

    fn find_parent<F2: FileLike>(&self, file: &F2) -> SharedResult<&Self::F> {
        self.maybe_find_parent(file)
            .ok_or_else(|| SharedErrorKind::FileParentNonexistent.into())
    }

    fn owned_ids(&self) -> HashSet<Uuid> {
        self.ids().iter().map(|id| **id).collect()
    }

    fn all_files(&self) -> SharedResult<Vec<&Self::F>> {
        let mut all = vec![];
        for id in self.ids() {
            let meta = self.find(id)?;
            all.push(meta);
        }

        Ok(all)
    }

    fn stage<Staged>(&self, staged: Staged) -> StagedTree<&Self, Staged>
    where
        Staged: TreeLike<F = Self::F>,
        Self: Sized,
    {
        StagedTree::new(self, staged)
    }

    fn to_staged<Staged>(self, staged: Staged) -> StagedTree<Self, Staged>
    where
        Staged: TreeLike<F = Self::F>,
        Self: Sized,
    {
        StagedTree::new(self, staged)
    }

    fn as_lazy(&self) -> LazyTree<&Self> {
        LazyTree::new(self)
    }

    fn to_lazy(self) -> LazyTree<Self> {
        LazyTree::new(self)
    }
}

pub trait TreeLikeMut: TreeLike {
    fn insert(&mut self, f: Self::F) -> SharedResult<Option<Self::F>>;
    fn remove(&mut self, id: Uuid) -> SharedResult<Option<Self::F>>;
    fn clear(&mut self) -> SharedResult<()>;
}

impl<T> TreeLike for &T
where
    T: TreeLike,
{
    type F = T::F;

    fn ids(&self) -> HashSet<&Uuid> {
        T::ids(self)
    }

    fn maybe_find(&self, id: &Uuid) -> Option<&Self::F> {
        T::maybe_find(self, id)
    }
}

impl<T> TreeLike for &mut T
where
    T: TreeLike,
{
    type F = T::F;

    fn ids(&self) -> HashSet<&Uuid> {
        T::ids(self)
    }

    fn maybe_find(&self, id: &Uuid) -> Option<&Self::F> {
        T::maybe_find(self, id)
    }
}

impl<T> TreeLikeMut for &mut T
where
    T: TreeLikeMut,
{
    fn insert(&mut self, f: Self::F) -> SharedResult<Option<Self::F>> {
        T::insert(self, f)
    }

    fn remove(&mut self, id: Uuid) -> SharedResult<Option<Self::F>> {
        T::remove(self, id)
    }

    fn clear(&mut self) -> SharedResult<()> {
        T::clear(self)
    }
}

impl<F> TreeLike for Vec<F>
where
    F: FileLike,
{
    type F = F;

    fn ids(&self) -> HashSet<&Uuid> {
        self.iter().map(|f| f.id()).collect()
    }

    fn maybe_find(&self, id: &Uuid) -> Option<&F> {
        self.iter().find(|f| f.id() == id)
    }
}

impl<F> TreeLikeMut for Vec<F>
where
    F: FileLike,
{
    fn insert(&mut self, f: F) -> SharedResult<Option<F>> {
        for (i, value) in self.iter().enumerate() {
            if value.id() == f.id() {
                let old = std::mem::replace(&mut self[i], f);
                return Ok(Some(old));
            }
        }

        self.push(f);

        Ok(None)
    }

    fn remove(&mut self, id: Uuid) -> SharedResult<Option<F>> {
        for (i, value) in self.iter().enumerate() {
            if *value.id() == id {
                return Ok(Some(self.remove(i)));
            }
        }

        Ok(None)
    }

    fn clear(&mut self) -> SharedResult<()> {
        self.clear();
        Ok(())
    }
}