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
//
// imag - the personal information management suite for the commandline
// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; version
// 2.1 of the License.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//

use std::ops::Deref;
use std::path::Path;
use std::path::PathBuf;

use std::fmt::{Display, Debug, Formatter};
use std::fmt::Error as FmtError;
use std::result::Result as RResult;
use std::path::Components;

use failure::ResultExt;
use failure::Fallible as Result;
use failure::err_msg;
use failure::Error;

use crate::store::Store;

use crate::iter::create::StoreCreateIterator;
use crate::iter::delete::StoreDeleteIterator;
use crate::iter::get::StoreGetIterator;
use crate::iter::retrieve::StoreRetrieveIterator;

/// The Index into the Store
///
/// A StoreId object is a unique identifier for one entry in the store which might be present or
/// not.
///
#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct StoreId(PathBuf);

impl StoreId {

    pub fn new(id: PathBuf) -> Result<StoreId> {
        debug!("Trying to get a new baseless id from: {:?}", id);
        if id.is_absolute() {
            debug!("Error: Id is absolute!");
            Err(format_err!("Store Id local part is absolute: {}", id.display()))
        } else {
            debug!("Building Storeid object baseless");
            Ok(StoreId(id))
        }
    }

    pub(crate) fn with_base(self, base: &PathBuf) -> StoreIdWithBase<'_> {
        StoreIdWithBase(base, self.0)
    }

    pub fn to_str(&self) -> Result<String> {
        Ok(self.0.display().to_string())
    }

    /// Helper function for creating a displayable String from StoreId
    ///
    /// This is safe because the
    ///
    /// ```ignore
    ///     impl<T: fmt::Display + ?Sized> ToString for T
    /// ```
    ///
    /// does only fail if Display::display() failed. The implementation of ::std::path::Display and
    /// the implementation ::std::fmt::Display for ::std::path::Display do not return errors though.
    pub fn local_display_string(&self) -> String {
        self.local().display().to_string()
    }

    /// Returns the components of the `id` part of the StoreId object.
    ///
    /// Can be used to check whether a StoreId points to an entry in a specific collection of
    /// StoreIds.
    pub fn components(&self) -> Components {
        self.0.components()
    }

    /// Get the _local_ part of a StoreId object, as in "the part from the store root to the entry".
    pub fn local(&self) -> &PathBuf {
        &self.0
    }

    /// Check whether a StoreId points to an entry in a specific collection.
    ///
    /// A "collection" here is simply a directory. So `foo/bar/baz` is an entry which is in
    /// collection ["foo", "bar", "baz"], but also in ["foo", "bar"] and ["foo"].
    ///
    /// # Warning
    ///
    /// The collection specification _has_ to start with the module name. Otherwise this function
    /// may return false negatives.
    ///
    pub fn is_in_collection<S: AsRef<str>, V: AsRef<[S]>>(&self, colls: &V) -> bool {
        use std::path::Component;

        self.0
            .components()
            .zip(colls.as_ref().iter())
            .all(|(component, pred_coll)| match component {
                Component::Normal(ref s) => s
                    .to_str()
                    .map(|ref s| s == &pred_coll.as_ref())
                    .unwrap_or(false),
                _ => false
            })
    }

    pub fn local_push<P: AsRef<Path>>(&mut self, path: P) {
        self.0.push(path)
    }

}

impl Display for StoreId {

    fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
        write!(fmt, "{}", self.0.display())
    }

}

/// This Trait allows you to convert various representations to a single one
/// suitable for usage in the Store
pub trait IntoStoreId {
    fn into_storeid(self) -> Result<StoreId>;
}

impl IntoStoreId for StoreId {
    fn into_storeid(self) -> Result<StoreId> {
        Ok(self)
    }
}

impl IntoStoreId for PathBuf {
    fn into_storeid(self) -> Result<StoreId> {
        StoreId::new(self)
    }
}

#[derive(Debug, Clone, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub(crate) struct StoreIdWithBase<'a>(&'a PathBuf, PathBuf);

impl<'a> StoreIdWithBase<'a> {
    #[cfg(test)]
    pub(crate) fn new(base: &'a PathBuf, path: PathBuf) -> Self {
        StoreIdWithBase(base, path)
    }

    pub(crate) fn without_base(self) -> StoreId {
        StoreId(self.1)
    }

    /// Transform the StoreId object into a PathBuf, error if the base of the StoreId is not
    /// specified.
    pub(crate) fn into_pathbuf(self) -> Result<PathBuf> {
        let mut base = self.0.clone();
        base.push(self.1);
        Ok(base)
    }

    /// Try to create a StoreId object from a filesystem-absolute path.
    ///
    /// Automatically creates a StoreId object which has a `base` set to `store_part` if stripping
    /// the `store_part` from the `full_path` succeeded.
    pub(crate) fn from_full_path<D>(store_part: &'a PathBuf, full_path: D) -> Result<StoreIdWithBase<'a>>
        where D: Deref<Target = Path>
    {
        trace!("Creating StoreIdWithBase object from full path = {} with store_part = {}",
               full_path.display(),
               store_part.display());
        let p = full_path
            .strip_prefix(store_part)
            .context(format_err!("Cannot strip prefix '{}' from path: '{}'",
                                 store_part.display(),
                                 full_path.display()))
            .map_err(Error::from)
            .context(err_msg("Error building Store Id from full path"))?;
        Ok(StoreIdWithBase(store_part, PathBuf::from(p)))
    }
}

impl<'a> IntoStoreId for StoreIdWithBase<'a> {
    fn into_storeid(self) -> Result<StoreId> {
        Ok(StoreId(self.1))
    }
}

impl<'a> Into<StoreId> for StoreIdWithBase<'a> {
    fn into(self) -> StoreId {
        StoreId(self.1)
    }
}

impl<'a> Display for StoreIdWithBase<'a> {
    fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
        write!(fmt, "{}/{}", self.0.display(), self.1.display())
    }
}


#[macro_export]
macro_rules! module_entry_path_mod {
    ($name:expr) => (
        #[allow(missing_docs,
                missing_copy_implementations,
                trivial_casts, trivial_numeric_casts,
                unstable_features,
                unused_import_braces, unused_qualifications,
                unused_imports)]
        /// A helper module to create valid module entry paths
        pub mod module_path {
            use std::convert::AsRef;
            use std::path::Path;
            use std::path::PathBuf;
            use $crate::storeid::StoreId;
            use failure::Fallible as Result;

            pub fn new_id<P: AsRef<Path>>(p: P) -> Result<StoreId> {

                let path_str = p
                    .as_ref()
                    .to_str()
                    .ok_or_else(|| {
                        format_err!("File path is not valid UTF-8: {}", p.as_ref().display())
                    })?;

                let id = format!("{}/{}", $name, path_str);

                StoreId::new(PathBuf::from(id))
            }

        }
    )
}

pub struct StoreIdIterator {
    iter: Box<dyn Iterator<Item = Result<StoreId>>>,
}

impl Debug for StoreIdIterator {

    fn fmt(&self, fmt: &mut Formatter) -> RResult<(), FmtError> {
        write!(fmt, "StoreIdIterator")
    }

}

impl StoreIdIterator {

    pub fn new(iter: Box<dyn Iterator<Item = Result<StoreId>>>) -> StoreIdIterator {
        StoreIdIterator { iter }
    }

    pub fn with_store(self, store: &Store) -> StoreIdIteratorWithStore<'_> {
        StoreIdIteratorWithStore(self, store)
    }

}

impl Iterator for StoreIdIterator {
    type Item = Result<StoreId>;

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

}

pub struct StoreIdIteratorWithStore<'a>(StoreIdIterator, &'a Store);

impl<'a> Deref for StoreIdIteratorWithStore<'a> {
    type Target = StoreIdIterator;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<'a> Iterator for StoreIdIteratorWithStore<'a> {
    type Item = Result<StoreId>;

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

impl<'a> StoreIdIteratorWithStore<'a> {

    pub fn new(iter: Box<dyn Iterator<Item = Result<StoreId>>>, store: &'a Store) -> Self {
        StoreIdIteratorWithStore(StoreIdIterator::new(iter), store)
    }

    pub fn into_storeid_iter(self) -> StoreIdIterator {
        self.0
    }

    /// Transform the iterator into a StoreCreateIterator
    ///
    /// This immitates the API from `libimagstore::iter`.
    pub fn into_create_iter(self) -> StoreCreateIterator<'a> {
        StoreCreateIterator::new(Box::new(self.0), self.1)
    }

    /// Transform the iterator into a StoreDeleteIterator
    ///
    ///
    /// This immitates the API from `libimagstore::iter`.
    pub fn into_delete_iter(self) -> StoreDeleteIterator<'a> {
        StoreDeleteIterator::new(Box::new(self.0), self.1)
    }

    /// Transform the iterator into a StoreGetIterator
    ///
    ///
    /// This immitates the API from `libimagstore::iter`.
    pub fn into_get_iter(self) -> StoreGetIterator<'a> {
        StoreGetIterator::new(Box::new(self.0), self.1)
    }

    /// Transform the iterator into a StoreRetrieveIterator
    ///
    ///
    /// This immitates the API from `libimagstore::iter`.
    pub fn into_retrieve_iter(self) -> StoreRetrieveIterator<'a> {
        StoreRetrieveIterator::new(Box::new(self.0), self.1)
    }

}

#[cfg(test)]
mod test {
    module_entry_path_mod!("test");

    #[test]
    fn test_correct_path() {
        let p = crate::storeid::test::module_path::new_id("test");

        assert_eq!(p.unwrap().to_str().unwrap(), "test/test");
    }

    #[test]
    fn storeid_in_collection() {
        let p = crate::storeid::test::module_path::new_id("1/2/3/4/5/6/7/8/9/0").unwrap();

        assert!(p.is_in_collection(&["test", "1"]));
        assert!(p.is_in_collection(&["test", "1", "2"]));
        assert!(p.is_in_collection(&["test", "1", "2", "3"]));
        assert!(p.is_in_collection(&["test", "1", "2", "3", "4"]));
        assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5"]));
        assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6"]));
        assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6", "7"]));
        assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6", "7", "8"]));
        assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6", "7", "8", "9"]));
        assert!(p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6", "7", "8", "9", "0"]));

        assert!(!p.is_in_collection(&["test", "0", "2", "3", "4", "5", "6", "7", "8", "9", "0"]));
        assert!(!p.is_in_collection(&["test", "1", "2", "3", "4", "5", "6", "8"]));
        assert!(!p.is_in_collection(&["test", "1", "2", "3", "leet", "5", "6", "7"]));
    }

}