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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
//! Repository is where SIT stores all of its artifacts.
//!
//! It is represented by the [`Repository`] structure.
//!
//! [`Repository`]: struct.Repository.html
//!


use std::path::{Component, Path, PathBuf};
use std::fs;
use std::io::{Read, Write};

use tempdir::TempDir;

use glob;

use serde_json;

use super::hash::{HashingAlgorithm, Hasher};
use super::encoding::Encoding;
use super::id::IdGenerator;

use std::marker::PhantomData;

/// Current repository format version
const VERSION: &str = "1";
/// Repository's config file name
const CONFIG_FILE: &str = "config.json";
/// Repository's issues path
const ISSUES_PATH: &str = "issues";

/// Repository is the container for all SIT artifacts
#[derive(Debug)]
pub struct Repository {
    /// Path to the container
    path: PathBuf,
    /// Path to the config file. Mainly to avoid creating
    /// this path on demand for every operation that would
    /// require it
    config_path: PathBuf,
    /// Path to issues. Mainly to avoid creating this path
    /// on demand for every operation that would require it
    issues_path: PathBuf,
    /// Configuration
    config: Config,
}

/// Repository configuration
#[derive(Debug, Clone, TypedBuilder, Serialize, Deserialize)]
pub struct Config {
     /// Hashing algorithm used
    hashing_algorithm: HashingAlgorithm,
    /// Encoding used
    encoding: Encoding,
    /// ID generator
    id_generator: IdGenerator,
    /// Repository version
    #[default = "String::from(VERSION)"]
    version: String,
}

#[derive(Debug, Error)]
pub enum Error {
    /// Item already exists
    AlreadyExists,
    /// Item not found
    NotFound,
    /// Invalid repository version
    #[error(no_from, non_std)]
    InvalidVersion {
        expected: String,
        got: String,
    },
    /// I/O error
    IoError(::std::io::Error),
    /// JSON (de)serialization error
    SerializationError(serde_json::Error),
    /// Base decoding error
    BaseDecodeError(::data_encoding::DecodeError),
}

#[allow(unused_variables,dead_code)]
mod default_files {
    include!(concat!(env!("OUT_DIR"), "/default_files.rs"));

    use std::path::PathBuf;
    use std::collections::HashMap;

    lazy_static! {
      pub static ref ASSETS: HashMap<PathBuf, File> = {
         let mut map = HashMap::new();
         let prefix = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("default-files");
         for entry in FILES.walk() {
            match entry {
               DirEntry::File(f) => {
                  let path = PathBuf::from(f.path().strip_prefix(&prefix).unwrap());
                  map.insert(path.clone(), f.clone());
               },
               _ => (),
            }
         }
         map
       };
    }

}

impl Repository {

    /// Attempts creating a new repository. Fails with `Error::AlreadyExists`
    /// if a repository already exists.
    pub fn new<P: Into<PathBuf>>(path: P) -> Result<Self, Error> {
        Repository::new_with_config(path, Config {
            hashing_algorithm: Default::default(),
            encoding: Encoding::default(),
            id_generator: IdGenerator::default(),
            version: String::from(VERSION),
        })
    }

    /// Attempts creating a new repository with a specified config. Fails with `Error::AlreadyExists`
    /// if a repository already exists.
    pub fn new_with_config<P: Into<PathBuf>>(path: P, config: Config) -> Result<Self, Error> {
        let path: PathBuf = path.into();
        if path.is_dir() {
            Err(Error::AlreadyExists)
        } else {
            let mut config_path = path.clone();
            config_path.push(CONFIG_FILE);
            let mut issues_path = path.clone();
            issues_path.push(ISSUES_PATH);
            fs::create_dir_all(&issues_path)?;
            let repo = Repository {
                path,
                config_path,
                issues_path,
                config,
            };
            repo.save()?;
            Ok(repo)
        }

    }

    /// Opens an existing repository. Fails if there's no valid repository at the
    /// given path
    pub fn open<P: Into<PathBuf>>(path: P) -> Result<Self, Error> {
        let path: PathBuf = path.into();
        let mut config_path = path.clone();
        config_path.push(CONFIG_FILE);
        let mut issues_path = path.clone();
        issues_path.push(ISSUES_PATH);
        fs::create_dir_all(&issues_path)?;
        let file = fs::File::open(&config_path)?;
        let config: Config = serde_json::from_reader(file)?;
        if config.version != VERSION {
            return Err(Error::InvalidVersion { expected: String::from(VERSION), got: config.version });
        }
        let repository = Repository {
            path,
            config_path,
            issues_path,
            config,
        };
        Ok(repository)
    }

    pub fn find_in_or_above<P: Into<PathBuf>, S: AsRef<str>>(dir: S, path: P) -> Result<Self, Error> {
        let mut path: PathBuf = path.into();
        let dir = dir.as_ref();
        path.push(dir);
        loop {
            if !path.is_dir() {
                // get out of `dir`
                path.pop();
                // if can't pop anymore, we're at the root of the filesystem
                if !path.pop() {
                    return Err(Error::NotFound)
                }
                // try assuming current path + `dir`
                path.push(dir);
            } else {
                break;
            }
        }
        Repository::open(path)
    }


    /// Saves the repository. Ensures the directory exists and the configuration has
    /// been saved.
    fn save(&self) -> Result<(), Error> {
        fs::create_dir_all(&self.path)?;
        let file = fs::File::create(&self.config_path)?;
        serde_json::to_writer_pretty(file, &self.config)?;
        Ok(())
    }

    /// Populates repository with default files
    pub fn populate_default_files(&self) -> Result<(), Error> {
        for (name, file) in default_files::ASSETS.iter() {
            let mut dir = self.path.join(name);
            dir.pop();
            fs::create_dir_all(dir)?;
            let mut f = fs::File::create(self.path.join(name))?;
            f.write(file.contents)?;
        }
        Ok(())
    }

    /// Returns repository path
    pub fn path(&self) -> &Path {
        self.path.as_path()
    }

    /// Returns issues path
    pub fn issues_path(&self) -> &Path {
        self.issues_path.as_path()
    }

    /// Returns repository's config
    pub fn config(&self) -> &Config {
        &self.config
    }

    /// Returns an unordered (as in "order not defined") issue iterator
    pub fn issue_iter(&self) -> Result<IssueIter, Error> {
        Ok(IssueIter { repository: self, dir: fs::read_dir(&self.issues_path)? })
    }

    /// Creates and returns a new issue with a unique ID
    pub fn new_issue(&self) -> Result<Issue, Error> {
        self.new_named_issue(self.config.id_generator.generate())
    }

    /// Creates and returns a new issue with a specific name. Will fail
    /// if there's an issue with the same name.
    pub fn new_named_issue<S: Into<String>>(&self, name: S) -> Result<Issue, Error> {
        let id: String = name.into();
        let mut path = self.issues_path.clone();
        path.push(&id);
        fs::create_dir(path)?;
        let id = OsString::from(id);
        Ok(Issue {
            repository: self,
            id,
        })
    }
}

use super::Issue as IssueTrait;

use std::ffi::{OsString, OsStr};

/// An issue residing in a repository
#[derive(Debug)]
pub struct Issue<'a> {
    repository: &'a Repository,
    id: OsString,
}


fn process_file<S: AsRef<str>, R: ::std::io::Read>(hasher: &mut Hasher, name: S, mut reader: R, mut buf: &mut Vec<u8>, tempdir: &TempDir) -> Result<(), ::std::io::Error> {
    #[cfg(windows)] // replace backslashes with slashes
    let name_for_hashing = name.as_ref().replace("\\", "/");
    #[cfg(unix)]
    let name_for_hashing = name.as_ref();
    hasher.process((name_for_hashing.as_ref() as &str).as_bytes());
    let path = tempdir.path().join(PathBuf::from(name.as_ref() as &str));
    let mut dir = path.clone();
    dir.pop();
    fs::create_dir_all(dir)?;
    let mut file = fs::File::create(path)?;
    loop {
        let bytes_read = reader.read(&mut buf)?;
        hasher.process(&buf);
        file.write(&buf[0..bytes_read])?;
        if bytes_read == 0 {
            break;
        }
    }
    Ok(())
}
impl<'a> Issue<'a> {
    pub fn new_record_in<P: AsRef<Path>, S: AsRef<str>, R: ::std::io::Read,
        I: Iterator<Item=(S, R)>>(&self, path: P, iter: I, link_parents: bool) -> Result<<Issue<'a> as IssueTrait>::Record, <Issue<'a> as IssueTrait>::Error> {
        let tempdir = TempDir::new_in(&self.repository.path,"sit")?;
        let mut hasher = self.repository.config.hashing_algorithm.hasher();
        let mut buf = vec![0; 4096];

        let mut files: Vec<(Box<AsRef<str>>, Box<Read>)> = vec![];
        // iterate over all files
        for (name, mut reader) in iter {
            files.push((Box::new(name) as Box<AsRef<str>>, Box::new(reader) as Box<Read>));
        }

        // Link parents if requested
        if link_parents {
            match self.record_iter()?.last() {
                None => (),
                Some(records) => {
                    let parents = records.iter().map(|rec| (format!(".prev/{}", rec.encoded_hash()), &b""[..]));

                    for (name, mut reader) in parents {
                        files.push((Box::new(name) as Box<AsRef<str>>, Box::new(reader) as Box<Read>));
                    }
                },
            }
        }

        // IMPORTANT: Sort lexicographically
        files.sort_by(|&(ref name1, _), &(ref name2, _)|
            name1.as_ref().as_ref().cmp(name2.as_ref().as_ref()));


        for (name, mut reader) in files {
            process_file(&mut *hasher, name.as_ref(), reader, &mut buf, &tempdir)?;
        }

        let hash = hasher.result_box();
        let actual_path = path.as_ref().join(PathBuf::from(self.repository.config.encoding.encode(&hash)));
        fs::rename(tempdir.into_path(), &actual_path)?;
        Ok(Record {
            hash,
            issue: self.id.clone(),
            repository: self.repository,
            actual_path,
        })
    }

}
impl<'a> IssueTrait for Issue<'a> {

    type Error = Error;
    type Record = Record<'a>;
    type Records = Vec<Record<'a>>;
    type RecordIter = IssueRecordIter<'a>;

    fn id(&self) -> &str {
        self.id.to_str().unwrap()
    }

    fn record_iter(&self) -> Result<Self::RecordIter, Self::Error> {
        let path = self.repository.issues_path.join(PathBuf::from(&self.id()));
        let glob_pattern = format!("{}/**/*", path.to_str().unwrap());
        let dir = fs::read_dir(&path)?.filter(|r| r.is_ok())
            .map(|e| e.unwrap())
            .collect();
        let files: Vec<_> = glob::glob(&glob_pattern).expect("invalid glob pattern")
            .filter(|r| r.is_ok())
            .map(|r| r.unwrap())
            .map(|f| f.strip_prefix(&path).unwrap().into())
            .collect();
        Ok(IssueRecordIter {
            issue: self.id.clone(),
            repository: self.repository,
            dir,
            files,
            parents: vec![],
        })
    }

    fn new_record<S: AsRef<str>, R: ::std::io::Read,
        I: Iterator<Item=(S, R)>>(&self, iter: I, link_parents: bool) -> Result<Self::Record, Self::Error> {
       self.new_record_in(self.repository.issues_path.join(PathBuf::from(self.id())), iter, link_parents)
    }

}

/// An iterator over records in an issue
pub struct IssueRecordIter<'a> {
    issue: OsString,
    repository: &'a Repository,
    dir: Vec<fs::DirEntry>,
    files: Vec<PathBuf>,
    parents: Vec<String>,
}

impl<'a> Iterator for IssueRecordIter<'a> {
    type Item = Vec<Record<'a>>;

    fn next(&mut self) -> Option<Self::Item> {
        // top level
        if self.parents.len() == 0 {
            let result: Vec<_> = self.files.iter()
                // find issues
                .filter(|f|
                    f.components().count() == 1)
                // that don't have .prev/ID files in them
                .filter(|f| !self.files.iter()
                    .any(|f1| f1.starts_with(f) && f1.components().any(|c| c == Component::Normal(OsStr::new(".prev")))))
                // filter out invalid record names (if any)
                .filter(|f| self.repository.config.encoding.decode(f.to_str().unwrap().as_bytes()).is_ok())
                .map(|f| Record {
                    hash: self.repository.config.encoding.decode(f.to_str().unwrap().as_bytes()).unwrap(),
                    issue: self.issue.clone(),
                    repository: self.repository,
                    actual_path: self.repository.issues_path().join(&self.issue).join(f.to_str().unwrap()),
                })
                .collect();
            if result.len() == 0 {
                return None
            }
            self.parents = result.iter().map(|r| r.encoded_hash()).collect();
            return Some(result);
        } else {
            let result: Vec<_> = self.dir.iter()
                // filter out invalid record names (if any)
                .filter(|r| self.repository.config.encoding.decode(r.file_name().to_str().unwrap().as_bytes()).is_ok())
                .filter(|r| {
                    let links: Vec<_> = self.files.iter()
                        .filter(|f| f.starts_with(r.file_name()))
                        .filter(|f| {
                            let components: Vec<_> = f.components().skip(1).collect();
                            components.len() == 2 && components[0] == Component::Normal(OsStr::new(".prev"))
                        })
                        .map(|f| {
                            let components: Vec<_> = f.components().skip(2).collect();
                            PathBuf::from(components[0].as_os_str())
                        }).collect();
                    links.len() > 0 && links.iter().all(|l| self.parents.iter().any(|p| p == &String::from(l.to_str().unwrap())))
                })
                .map(|r| Record {
                    hash: self.repository.config.encoding.decode(r.file_name().to_str().unwrap().as_bytes()).unwrap(),
                    issue: self.issue.clone(),
                    repository: self.repository,
                    actual_path: self.repository.issues_path().join(&self.issue).join(r.file_name()),
                })
                .collect();
            if result.len() == 0 {
                return None
            }
            self.parents = result.iter().map(|r| r.encoded_hash()).collect();
            return Some(result);
        }
    }
}


/// Unordered (as in "order not defined') issue iterator
/// within a repository
pub struct IssueIter<'a> {
    repository: &'a Repository,
    dir: fs::ReadDir,
}

impl<'a> Iterator for IssueIter<'a> {
    type Item = Issue<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.dir.next() {
                None => return None,
                // bail on an entry if the entry is erroneous
                Some(Err(_)) => continue,
                Some(Ok(entry)) => {
                    let file_type = entry.file_type();
                    // bail on an entry if checking for the file type
                    // resulted in an error
                    if file_type.is_err() {
                        continue;
                    }
                    let file_type = file_type.unwrap();
                    if file_type.is_dir() {
                        return Some(Issue { repository: self.repository, id: entry.file_name() });
                    } else {
                        continue;
                    }
                }
            }
        }
    }
}

use super::Record as RecordTrait;

/// A record within an issue
#[derive(Debug)]
pub struct Record<'a> {
    hash: Vec<u8>,
    issue: OsString,
    repository: &'a Repository,
    actual_path: PathBuf,
}

/// Somethiing that can provide access to its underlying repository
pub trait RepositoryProvider {
    /// Returns underlying repository;
    fn repository(&self) -> &Repository;
}

impl<'a> RepositoryProvider for Record<'a> {
    fn repository(&self) -> &Repository {
        self.repository
    }
}

#[derive(Debug)]
/// Record wrapper that dynamically rehashes wrapped Record's content
pub struct DynamicallyHashedRecord<'a, T: RecordTrait + RepositoryProvider + 'a>(&'a T);

impl<'a, T: RecordTrait<Str=String, Hash=Vec<u8>> + RepositoryProvider + 'a> RecordTrait for DynamicallyHashedRecord<'a, T> {
    type Read = T::Read;
    type Str = String;
    type Hash = Vec<u8>;
    type Iter = T::Iter;

    fn hash(&self) -> Self::Hash {
        let tempdir = TempDir::new_in(&self.0.repository().path(),"sit").unwrap();
        let mut hasher = self.0.repository().config.hashing_algorithm.hasher();
        let mut buf = vec![0; 4096];

        let mut files: Vec<(Box<AsRef<str>>, Box<Read>)> = vec![];

        for (name, reader) in self.file_iter() {
            files.push((Box::new(name) as Box<AsRef<str>>, Box::new(reader) as Box<Read>));
        }

        // IMPORTANT: Sort lexicographically
        files.sort_by(|&(ref name1, _), &(ref name2, _)|
            name1.as_ref().as_ref().cmp(name2.as_ref().as_ref()));

        for (name, mut reader) in files {
            process_file(&mut *hasher, name.as_ref(), reader, &mut buf, &tempdir).unwrap();
        }

        hasher.result_box()
    }

    fn encoded_hash(&self) -> Self::Str {
        self.0.repository().config.encoding.encode(self.hash().as_ref())
    }

    fn file_iter(&self) -> Self::Iter {
        self.0.file_iter()
    }

    fn issue_id(&self) -> Self::Str {
        self.0.issue_id()
    }
}

#[derive(Debug)]
/// Record with filtered content
pub struct FilteredRecord<'a, S: AsRef<str>, R: Read, T: RecordTrait<Str=S, Read=R> + RepositoryProvider + 'a,
           F: Fn(&(S, R)) -> bool>(&'a T, F);

impl<'a, S: AsRef<str>, R: Read, T: RecordTrait<Str=S, Read=R> + RepositoryProvider + 'a, F: Copy + Fn(&(S, R)) -> bool> RecordTrait for FilteredRecord<'a, S, R, T, F> {
    type Read = T::Read;
    type Hash = T::Hash;
    type Str = T::Str;
    type Iter = ::std::iter::Filter<T::Iter, F>;

    fn hash(&self) -> Self::Hash {
        self.0.hash()
    }

    fn encoded_hash(&self) -> Self::Str {
        self.0.encoded_hash()
    }

    fn file_iter(&self) -> Self::Iter {
        self.0.file_iter().filter(self.1)
    }

    fn issue_id(&self) -> Self::Str {
        self.0.issue_id()
    }
}

impl <'a, S: AsRef<str>, R: Read, T: RecordTrait<Str=S, Read=R> + RepositoryProvider + 'a, F: Copy + Fn(&(S, R)) -> bool> RepositoryProvider for FilteredRecord<'a, S, R, T, F> {
    fn repository(&self) -> &Repository {
        self.0.repository()
    }
}

/// Allows any Record to have its content dynamically rehashed
pub trait DynamicallyHashable<'a> : RecordTrait + RepositoryProvider + Sized {
    /// Returns a record that has its hash dynamically computed
    fn dynamically_hashed(&'a self) -> DynamicallyHashedRecord<'a, Self> {
        DynamicallyHashedRecord(self)
    }
}

impl<'a> DynamicallyHashable<'a> for Record<'a> {}
impl<'a, S: AsRef<str>, R: Read, T: RecordTrait<Str=S, Read=R> + RepositoryProvider + 'a, F: Copy + Fn(&(S, R)) -> bool> DynamicallyHashable<'a> for FilteredRecord<'a, S, R, T, F> {}

impl<'a> Record<'a> {

    /// Returns path to the record, as it should be per repository's naming scheme
    ///
    /// The record MIGHT not be at this path as this is the path where
    /// it SHOULD BE. The actual path can be retrieved using `actual_path()`
    pub fn path(&self) -> PathBuf {
        self.repository.issues_path.join(PathBuf::from(&self.issue)).join(self.encoded_hash())
    }

    /// Returns an actual path to the record directory
    pub fn actual_path(&self) -> &Path {
        self.actual_path.as_path()
    }


    /// Returns a record with filtered files
    pub fn filtered<F>(&'a self, filter: F) -> FilteredRecord<'a, <Record<'a> as RecordTrait>::Str,
        <Record<'a> as RecordTrait>::Read,
        Record<'a>, F> where F: Fn(&(<Record<'a> as RecordTrait>::Str, <Record<'a> as RecordTrait>::Read)) -> bool {
        FilteredRecord(self, filter)
    }

}

use serde::{Serialize, Serializer};

impl<'a> Serialize for Record<'a> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
        use record::RecordExt;
        self.serde_serialize(serializer)
    }
}


impl<'a> PartialEq for Record<'a> {
   fn eq(&self, other: &Record<'a>) -> bool {
       self.hash == other.hash
   }
}

impl<'a> RecordTrait for Record<'a> {
    type Read = ::std::fs::File;
    type Str = String;
    type Hash = Vec<u8>;
    type Iter = RecordFileIterator<'a>;

    fn hash(&self) -> Self::Hash {
        self.hash.clone()
    }

    fn encoded_hash(&self) -> Self::Str {
        self.repository.config.encoding.encode(&self.hash)
    }

    fn file_iter(&self) -> Self::Iter {
        let path = self.actual_path();
        let glob_pattern = format!("{}/**/*", path.to_str().unwrap());
        RecordFileIterator {
            glob: glob::glob(&glob_pattern).expect("invalid glob pattern"),
            prefix: self.actual_path().into(),
            phantom: PhantomData,
        }
    }
    fn issue_id(&self) -> Self::Str {
        self.issue.clone().into_string().unwrap()
    }
}

/// An iterator over files in a record
pub struct RecordFileIterator<'a> {
    glob: glob::Paths,
    prefix: PathBuf,
    phantom: PhantomData<&'a ()>,
}

impl<'a> Iterator for RecordFileIterator<'a> {
    type Item = (String, fs::File);

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            match self.glob.next() {
                None => return None,
                // skip on errors
                Some(Err(_)) => continue,
                Some(Ok(name)) => {
                    if name.is_file() {
                        let stripped = String::from(name.strip_prefix(&self.prefix).unwrap().to_str().unwrap());
                        #[cfg(windows)] // replace backslashes with slashes
                        let stripped = stripped.replace("\\", "/");
                        return Some((stripped, fs::File::open(name).unwrap()))
                    } else {
                        // if it is not a file, keep iterating
                        continue
                    }
                }
            }
        }
    }
}


#[cfg(test)]
mod tests {

    use tempdir::TempDir;

    use super::*;

    #[test]
    fn new_repo() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        assert_eq!(repo.issue_iter().unwrap().count(), 0); // no issues in a new repo
        assert_eq!(repo.path(), tmp);
    }

    #[test]
    fn new_repo_already_exists() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let _repo = Repository::new(&tmp).unwrap();
        // try creating it again
        let repo = Repository::new(&tmp);
        assert!(repo.is_err());
        assert_matches!(repo.unwrap_err(), Error::AlreadyExists);
    }

    #[test]
    fn open_repo() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        // create an issue
        let issue = repo.new_issue().unwrap();
        let repo = Repository::open(&tmp).unwrap();
        // load issues
        let mut issues: Vec<Issue> = repo.issue_iter().unwrap().collect();
        assert_eq!(issues.len(), 1);
        // check equality of the issue's ID
        assert_eq!(issues.pop().unwrap().id(), issue.id());
    }

    #[test]
    fn find_repo() {
        let tmp = TempDir::new("sit").unwrap().into_path();
        let sit = tmp.join(".sit");
        // create repo
        Repository::new(&sit).unwrap();
        let deep_subdir = tmp.join("a/b/c/d");
        let repo = Repository::find_in_or_above(".sit", &deep_subdir).unwrap();
        assert_eq!(repo.path(), sit);
        // negative test
        assert_matches!(Repository::find_in_or_above(".sit-dir", &deep_subdir), Err(Error::NotFound));
    }

    #[test]
    fn new_issue() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        // create an issue
        let issue = repo.new_issue().unwrap();
        // load issues
        let mut issues: Vec<Issue> = repo.issue_iter().unwrap().collect();
        assert_eq!(issues.len(), 1);
        // check equality of the issue's ID
        assert_eq!(issues.pop().unwrap().id(), issue.id());
    }

    #[test]
    fn new_named_issue() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        // create an issue
        let issue = repo.new_named_issue("one").unwrap();
        // load issues
        let mut issues: Vec<Issue> = repo.issue_iter().unwrap().collect();
        assert_eq!(issues.len(), 1);
        // check equality of the issue's ID
        assert_eq!(issues.pop().unwrap().id(), issue.id());
    }

    #[test]
    fn new_named_issue_dup() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        // create an issue
        let _issue = repo.new_named_issue("one").unwrap();
        // attempt to use the same name
        let issue1 = repo.new_named_issue("one");
        assert!(issue1.is_err());
        assert_matches!(issue1.unwrap_err(), Error::IoError(_));
        // there's still just one issue
        assert_eq!(repo.issue_iter().unwrap().count(), 1);
    }

    #[test]
    fn new_record() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        // create an issue
        let issue = repo.new_issue().unwrap();
        // create a record
        let record = issue.new_record(vec![("test", &b"hello"[..])].into_iter(), true).unwrap();
        // peek into the record
        let mut files: Vec<_> = record.file_iter().collect();
        assert_eq!(files.len(), 1);
        let (name, mut file) = files.pop().unwrap();
        assert_eq!(name, "test");
        use std::io::Read;
        let mut string = String::new();
        assert!(file.read_to_string(&mut string).is_ok());
        assert_eq!(string, "hello");
        // list records
        let mut records: Vec<Record> = issue.record_iter().unwrap().flat_map(|v| v).collect();
        assert_eq!(records.len(), 1);
        assert_eq!(records.pop().unwrap().hash(), record.hash());
    }


    #[test]
    fn new_record_parents_linking() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        // create an issue
        let issue = repo.new_issue().unwrap();
        // create a few top records
        let record1 = issue.new_record(vec![("test", &[1u8][..])].into_iter(), false).unwrap();
        let record1link = format!(".prev/{}", record1.encoded_hash());
        let record2 = issue.new_record(vec![("test", &[2u8][..])].into_iter(), false).unwrap();
        let record2link = format!(".prev/{}", record2.encoded_hash());
        // now attempt to create a record that should link both together
        let record = issue.new_record(vec![("test", &[3u8][..])].into_iter(), true).unwrap();
        assert!(record.file_iter().any(|(name, _)| name == *&record1link));
        assert!(record.file_iter().any(|(name, _)| name == *&record2link));
    }

    #[test]
    fn record_ordering() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        // create an issue
        let issue = repo.new_issue().unwrap();
        // create a few top records
        let record1 = issue.new_record(vec![("test", &[1u8][..])].into_iter(), false).unwrap();
        let record2 = issue.new_record(vec![("test", &[2u8][..])].into_iter(), false).unwrap();
        // now attempt to create a record that should link both together
        let record3 = issue.new_record(vec![("test", &[3u8][..])].into_iter(), true).unwrap();
        // and another top record
        let record4 = issue.new_record(vec![("test", &[4u8][..])].into_iter(), false).unwrap();
        // and another linking record
        let record5 = issue.new_record(vec![("test", &[5u8][..])].into_iter(), true).unwrap();

        // now, look at their ordering
        let mut records: Vec<_> = issue.record_iter().unwrap().collect();
        let row_3 = records.pop().unwrap();
        let row_2 = records.pop().unwrap();
        let row_1 = records.pop().unwrap();
        assert_eq!(records.len(), 0);

        assert_eq!(row_1.len(), 3);
        assert!(row_1.iter().any(|r| r == &record1));
        assert!(row_1.iter().any(|r| r == &record2));
        assert!(row_1.iter().any(|r| r == &record4));

        assert_eq!(row_2.len(), 1);
        assert!(row_2.iter().any(|r| r == &record3));

        assert_eq!(row_3.len(), 1);
        assert!(row_3.iter().any(|r| r == &record5));
    }

    #[test]
    fn record_deterministic_hashing() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        let issue1 = repo.new_issue().unwrap();
        let record1 = issue1.new_record(vec![("z/a", &[2u8][..]), ("test", &[1u8][..])].into_iter(), false).unwrap();
        let issue2 = repo.new_issue().unwrap();
        let record2 = issue2.new_record(vec![("test", &[1u8][..]), ("z/a", &[2u8][..])].into_iter(), false).unwrap();
        assert_eq!(record1.hash(), record2.hash());
        #[cfg(windows)] {
            let issue3 = repo.new_issue().unwrap();
            let record3 = issue3.new_record(vec![("test", &[1u8][..]), ("z\\a", &[2u8][..])].into_iter(), false).unwrap();
            assert_eq!(record3.hash(), record2.hash());
        }
    }

    #[test]
    fn record_dynamic_hashing() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        let issue = repo.new_issue().unwrap();
        let record = issue.new_record(vec![("z/a", &[2u8][..]), ("test", &[1u8][..])].into_iter(), false).unwrap();
        let record_dynamic = record.dynamically_hashed();
        assert_eq!(record_dynamic.hash(), record.hash());
        assert_eq!(record_dynamic.encoded_hash(), record.encoded_hash());
        // now, put some file in the dynamic one
        let hash = record.hash();
        let encoded_hash = record.encoded_hash();
        ::std::fs::File::create(record.path().join("dynamic")).unwrap();
        assert_eq!(record.hash(), hash);
        assert_eq!(record.encoded_hash(), encoded_hash);
        assert_ne!(record_dynamic.hash(), record.hash());
        assert_ne!(record_dynamic.encoded_hash(), record.encoded_hash());
    }

    #[test]
    fn record_filtering() {
         let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let repo = Repository::new(&tmp).unwrap();
        let issue = repo.new_issue().unwrap();
        let record = issue.new_record(vec![("z/a", &[2u8][..]), ("test", &[1u8][..])].into_iter(), false).unwrap();
        fn not_za(val: &(String, fs::File)) -> bool {
            val.0 != "z/a"
        }
        let filtered = record.filtered(not_za);
        // Check the content
        assert_eq!(filtered.file_iter().count(), 1);
        let files: Vec<_> = filtered.file_iter().map(|(name, _)| name).collect();
        assert_eq!(files, vec!["test"]);
        // Filtering alone doesn't change hash
        assert_eq!(filtered.hash(), record.hash());
        assert_eq!(filtered.encoded_hash(), record.encoded_hash());
        // But doing it dynamically does
        assert_ne!(filtered.dynamically_hashed().hash(), record.hash());
        assert_ne!(filtered.dynamically_hashed().encoded_hash(), record.encoded_hash());
    }

    #[test]
    fn record_outside_naming_scheme() {
        let mut tmp = TempDir::new("sit").unwrap().into_path();
        tmp.push(".sit");
        let mut tmp1 = tmp.clone();
        tmp1.pop();

        let repo = Repository::new(&tmp).unwrap();
        let issue = repo.new_issue().unwrap();
        let _record1 = issue.new_record(vec![("z/a", &[2u8][..]), ("test", &[1u8][..])].into_iter(), false).unwrap();
        let record2 = issue.new_record_in(&tmp1, vec![("a", &[2u8][..])].into_iter(), true).unwrap();

        // lets test that record2 can iterate over correct files
        let files: Vec<_> = record2.file_iter().collect();
        assert_eq!(files.len(), 2); // a and .prev/...


        // record2 can't be found as it is outside of the standard naming scheme
        let records: Vec<Vec<_>> = issue.record_iter().unwrap().collect();
        assert_eq!(records.len(), 1);
        assert_eq!(records[0].len(), 1);

        // On Windows, if a file within a directory that is being
        // moved is open (even for reading), this will prevent
        // this said directory from being moved, returning "Access Denied"
        // Therefore, we drop `files` here to release the `File` readers
        #[cfg(windows)]
        drop(files);

        ::std::fs::rename(record2.actual_path(), record2.path()).unwrap();

        // and now it can be
        let records: Vec<Vec<_>> = issue.record_iter().unwrap().collect();
        assert_eq!(records.len(), 2);
        assert_eq!(records[0].len(), 1);
        assert_eq!(records[0].len(), 1);

    }

}