about summary refs log tree commit diff
path: root/src/files.rs
blob: aceb68471830c2506c48ad30f66644d09ec622b8 (plain)
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
use std::path::{Path, PathBuf};

use anyhow::{Context, Error, Result};

use crate::{
    actions::ActionOptions,
    filesystem::{Fs, FsEntry},
};

pub struct Locations {
    pub repository_path: PathBuf,
    pub ka_path: PathBuf,
    pub ka_files_path: PathBuf,
}

impl Locations {
    pub fn get_repository_index_path(&self) -> PathBuf {
        return self.ka_path.join("index");
    }

    pub fn get_repository_files<FS: Fs>(&self, fs: &mut FS) -> Result<Vec<FileState>, Error> {
        let working_entries = fs
            .read_directory(&self.repository_path)
            .context("Failed reading working file entries.")?
            .into_iter()
            .filter(|e| e.path() == self.ka_path)
            .collect();
        let history_entries = fs
            .read_directory(&self.ka_files_path)
            .context("Failed reading history file entries.")?;

        let working_files = Self::walk_directory(fs, working_entries, &|entry| {
            FileState::from_working(self, &entry.path()).ok()
        })?;

        let deleted_files = Self::walk_directory(fs, history_entries, &|entry| {
            let file_path = entry.path();
            let file = FileState::from_history(self, &file_path).ok()?;
            match file {
                FileState::Deleted { .. } => Some(file),
                FileState::Tracked { .. } => None,
                _ => unreachable!(),
            }
        })?;

        let mut all_files = working_files;
        all_files.extend(deleted_files);

        Ok(all_files)
    }

    pub fn working_from_history(&self, history_file_path: &Path) -> Result<PathBuf> {
        let raw_path = history_file_path.strip_prefix(&self.ka_files_path)?;
        Ok(self.repository_path.join(raw_path))
    }

    pub fn history_from_working(&self, working_file_path: &Path) -> Result<PathBuf> {
        let raw_path = working_file_path.strip_prefix(&self.repository_path)?;
        Ok(self.ka_files_path.join(raw_path))
    }

    fn walk_directory<FS: Fs>(
        fs: &mut FS,
        directory: Vec<FS::Entry>,
        filter_map: &dyn Fn(&FS::Entry) -> Option<FileState>,
    ) -> Result<Vec<FileState>> {
        let mut entries = Vec::new();

        for entry in directory {
            if entry.is_directory()? {
                let nested_directory = fs.read_directory(&entry.path())?;
                let nested_files = Self::walk_directory(fs, nested_directory, filter_map)?;
                entries.extend(nested_files);
            } else if let Some(states) = filter_map(&entry) {
                entries.push(states);
            }
        }

        Ok(entries)
    }
}

impl From<&ActionOptions> for Locations {
    fn from(options: &ActionOptions) -> Self {
        let ka_path = options.repository_path.join(".ka");
        let ka_files_path = ka_path.join("files");

        Self {
            repository_path: options.repository_path.clone(),
            ka_path,
            ka_files_path,
        }
    }
}

pub enum FileState {
    Deleted(FileDeleted),
    Untracked(FileUntracked),
    Tracked(FileTracked),
}

impl FileState {
    pub fn from_history(locations: &Locations, history_file_path: &Path) -> Result<Self> {
        let working_path = locations.working_from_history(history_file_path)?;
        Ok(if !working_path.exists() {
            FileState::Deleted(FileDeleted {
                history_path: history_file_path.to_path_buf(),
            })
        } else {
            FileState::Tracked(FileTracked {
                history_path: history_file_path.to_path_buf(),
                working_path,
            })
        })
    }

    pub fn from_working(locations: &Locations, working_file_path: &Path) -> Result<Self> {
        let history_path = locations.history_from_working(working_file_path)?;
        // FIXME: Path::exists wouldn't work with Fs abstraction.
        // TODO: Think whether abstracting Path would be needed for Fs abstraction.
        Ok(if !history_path.exists() {
            FileState::Untracked(FileUntracked {
                path: working_file_path.to_path_buf(),
            })
        } else {
            FileState::Tracked(FileTracked {
                history_path,
                working_path: working_file_path.to_path_buf(),
            })
        })
    }

    pub fn get_working_path(&self, locations: &Locations) -> Result<PathBuf> {
        match self {
            FileState::Deleted(deleted) => locations.working_from_history(&deleted.history_path),
            FileState::Untracked(untracked) => Ok(untracked.path.clone()),
            FileState::Tracked(tracked) => Ok(tracked.working_path.clone()),
        }
    }
}

pub struct FileDeleted {
    pub history_path: PathBuf,
}

impl FileDeleted {
    pub fn load_history_file<FS: Fs>(&self, fs: &mut FS) -> Result<FS::File> {
        fs.open_writable_file(&self.history_path)
    }

    pub fn create_working_file<FS: Fs>(
        &self,
        fs: &mut FS,
        locations: &Locations,
    ) -> Result<FS::File> {
        let working_path = locations.working_from_history(&self.history_path)?;
        fs.create_file(&working_path)
    }
}

pub struct FileUntracked {
    pub path: PathBuf,
}

impl FileUntracked {
    pub fn load_file<FS: Fs>(&self, fs: &mut FS) -> Result<FS::File> {
        fs.open_readable_file(&self.path)
    }

    pub fn create_history_file<FS: Fs>(
        &self,
        fs: &mut FS,
        locations: &Locations,
    ) -> Result<FS::File> {
        let history_path = locations.history_from_working(&self.path)?;
        Ok(fs.create_file(&history_path)?)
    }
}

pub struct FileTracked {
    pub history_path: PathBuf,
    pub working_path: PathBuf,
}

impl FileTracked {
    pub fn load_history_file<FS: Fs>(&self, fs: &mut FS) -> Result<FS::File> {
        fs.open_writable_file(&self.history_path)
    }

    pub fn load_working_file<FS: Fs>(&self, fs: &mut FS) -> Result<FS::File> {
        fs.open_readable_file(&self.working_path)
    }

    pub fn create_working_file<FS: Fs>(&self, fs: &mut FS) -> Result<FS::File> {
        fs.create_file(&self.working_path)
    }
}