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
use std::{
    collections::BTreeMap,
    fmt::{self, Debug},
    str,
    sync::Arc,
};

use crate::{error::ErrorKind, types::NodeHash};

pub(crate) struct Manifest {
    pub files: BTreeMap<Vec<u8>, ManifestEntry>,
}

impl Debug for Manifest {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        write!(
            fmt,
            "Manifest(\nfiles:\n{}\n)",
            self.files
                .iter()
                .map(|(key, value)| format!("{}: {:?}", str::from_utf8(&key).unwrap(), value))
                .collect::<Vec<_>>()
                .join("\n")
        )
    }
}

/// Manifest entry for file. Contains revision hash and file metainformation.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct ManifestEntry {
    pub id: NodeHash,
    pub details: ManifestEntryDetails,
}

impl ManifestEntry {
    fn parse(data: &[u8]) -> Result<ManifestEntry, ErrorKind> {
        let (hash, flags) = data.split_at(40);
        let id: NodeHash = str::from_utf8(hash).unwrap().parse().unwrap();

        let details = if flags.is_empty() {
            ManifestEntryDetails::File(FileType::Regular)
        } else {
            match flags[0] {
                b'l' => ManifestEntryDetails::File(FileType::Symlink),
                b'x' => ManifestEntryDetails::File(FileType::Executable),
                b't' => ManifestEntryDetails::Tree,
                unk => return Err(ErrorKind::Manifest(format!("Unknown flag {}", unk))),
            }
        };

        Ok(ManifestEntry { id, details })
    }
}

impl<'a> From<Arc<[u8]>> for Manifest {
    fn from(value: Arc<[u8]>) -> Self {
        let mut files = BTreeMap::new();
        for line in value.split(|&x| x == b'\n') {
            if line.is_empty() {
                break;
            }
            let mut parts = line.splitn(2, |&x| x == 0);
            if let (Some(file), Some(rest)) = (parts.next(), parts.next()) {
                files.insert(file.into(), ManifestEntry::parse(rest).unwrap());
            } else {
                panic!();
            }
        }
        Manifest { files }
    }
}

/// File meta information.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
pub enum ManifestEntryDetails {
    File(FileType),
    Tree,
}

impl fmt::Display for ManifestEntryDetails {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ManifestEntryDetails::Tree => write!(f, "tree"),
            ManifestEntryDetails::File(ft) => write!(f, "{}", ft),
        }
    }
}

/// File type. Can be regular, executable, or symlink.
#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub enum FileType {
    Regular,
    Executable,
    Symlink,
}

impl fmt::Display for FileType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let s = match self {
            FileType::Symlink => "symlink",
            FileType::Executable => "executable",
            FileType::Regular => "regular",
        };
        write!(f, "{}", s)
    }
}