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
use std::ffi::OsString;
use std::fmt::Formatter;
use std::path::PathBuf;
use anyhow::{Result};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize, Hash)]
pub enum ArchiveType {
    Tar,
    Zip,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum PathTarget {
    File,
    // Archive(ArchiveType),
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub struct PathComponent {
    pub path: PathBuf,
    pub target: PathTarget,
}

#[derive(Debug, Clone, Serialize, Deserialize, Hash)]
pub struct FilePath {
    pub path: Vec<PathComponent>
}

impl FilePath {
    pub fn from_vec(path: Vec<PathComponent>) -> Self {
        FilePath {
            path
        }
    }
    
    pub fn from_path(path: PathBuf, target: PathTarget) -> Self {
        FilePath {
            path: vec![PathComponent {
                path,
                target
            }]
        }
    }
    
    pub fn join(&mut self, path: PathBuf, target: PathTarget) {
        self.path.push(PathComponent {
            path,
            target
        });
    }
    
    pub fn extract_parent(&self, _temp_directory: &PathBuf) {
        todo!("implement")
    }
    
    pub fn delete_parent(&self, _temp_directory: &PathBuf) {
        todo!("implement")
    }
    
    pub fn resolve_file(&self) -> Result<PathBuf> {
        if self.path.len() == 1 {
            match self.path[0].target {
                PathTarget::File => Ok(self.path[0].path.clone()),
            }
        } else {
            todo!("implement")
        }
    }

    pub fn child_real(&self, child_name: OsString) -> FilePath {
        let mut result = FilePath {
            path: self.path.clone()
        };
        
        let component = PathBuf::from(child_name);
        
        match result.path.last_mut() {
            Some(last) => {
                last.path.push(component);
            },
            None => {
                result.path.push(PathComponent {
                    path: component,
                    target: PathTarget::File
                });
            }
        }
        
        return result;
    }
    
    pub fn parent(&self) -> Option<FilePath> {
        let last = self.path.last();
        
        match last { 
            None => None,
            Some(last) => {
                let parent = last.path.parent();
                
                match parent {
                    Some(parent) => {
                        let mut result = FilePath {
                            path: self.path.clone()
                        };
                        let last = result.path.last_mut().unwrap();
                        last.path = parent.to_path_buf();
                        
                        Some(result)
                    },
                    None => {
                        if self.path.len() == 1 {
                            None
                        } else {
                            let mut result = FilePath {
                                path: self.path.clone()
                            };
                            result.path.pop();
                            Some(result)
                        }
                    }
                }
            }
        }
    }
}

impl PartialEq for FilePath {
    fn eq(&self, other: &Self) -> bool {
        self.path.len() == other.path.len() && self.path.iter().zip(other.path.iter()).all(|(a, b)| a == b)
    }
}

impl Eq for FilePath {}

impl std::fmt::Display for FilePath {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let mut result = String::new();
        
        let mut first = true; 
        for component in &self.path {
            if first {
                first = false;
            } else {
                result.push_str("| ");
            }
            
            result.push_str(component.path.to_str().unwrap_or_else(|| "<invalid path>"));
        }
        
        write!(f, "{}", result)
    }
}