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
use crate::ImportableName;
use hydrate_schema::{DataSetError, DataSetResult};
use siphasher::sip128::Hasher128;
use std::fmt::{Display, Formatter};
use std::hash::Hash;
use std::path::{Path, PathBuf};
use uuid::Uuid;

pub trait PathReferenceNamespaceResolver {
    // Given the namespace, return the path associated with it
    fn namespace_root(
        &self,
        namespace: &str,
    ) -> Option<PathBuf>;

    // Given the canonicalized absolute path, if it can be expressed as a namespace and path within the namepsace, return that
    fn simplify_path(
        &self,
        path: &Path,
    ) -> Option<(String, PathBuf)>;
}

pub fn canonicalized_absolute_path(
    namespace: &String,
    referenced_path: &String,
    importable_name: &ImportableName,
    namespace_resolver: &dyn PathReferenceNamespaceResolver,
    source_file_path: &Path,
) -> DataSetResult<PathReference> {
    let canonical_absolute_path = if namespace.is_empty() {
        if Path::new(referenced_path).is_relative() {
            source_file_path
                .parent()
                .unwrap()
                .join(Path::new(referenced_path))
                .canonicalize()
                .map_err(|_| DataSetError::InvalidPath)?
        } else {
            PathBuf::from(referenced_path)
                .canonicalize()
                .map_err(|_| DataSetError::InvalidPath)?
        }
    } else {
        let namespace_root = namespace_resolver
            .namespace_root(namespace)
            .ok_or(DataSetError::UnknownPathNamespace)?;
        namespace_root
            .join(referenced_path)
            .canonicalize()
            .map_err(|_| DataSetError::InvalidPath)?
    };

    Ok(PathReference {
        namespace: "".to_string(),
        path: canonical_absolute_path.to_string_lossy().to_string(),
        importable_name: importable_name.clone(),
    })
}

// Given any path, parsed as a PathReference, the same CanonicalPathReference will be produced, and it is comparable,
// hashable, etc.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct CanonicalPathReference {
    namespace: String,
    path: String,
    importable_name: ImportableName,
}

impl Display for CanonicalPathReference {
    fn fmt(
        &self,
        f: &mut Formatter<'_>,
    ) -> std::fmt::Result {
        if self.namespace.is_empty() {
            if let Some(importable_name) = self.importable_name.name() {
                write!(f, "{}#{}", self.path, importable_name)
            } else {
                write!(f, "{}", self.path)
            }
        } else {
            if let Some(importable_name) = self.importable_name.name() {
                write!(f, "{}://{}#{}", self.namespace, self.path, importable_name)
            } else {
                write!(f, "{}://{}", self.namespace, self.path)
            }
        }
    }
}

impl CanonicalPathReference {
    pub fn new(
        namespace_resolver: &dyn PathReferenceNamespaceResolver,
        namespace: String,
        path: String,
        importable_name: ImportableName,
    ) -> Self {
        PathReference {
            namespace,
            path,
            importable_name,
        }
        .simplify(namespace_resolver)
    }

    pub fn namespace(&self) -> &str {
        &self.namespace
    }

    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn importable_name(&self) -> &ImportableName {
        &self.importable_name
    }

    pub fn canonicalized_absolute_path(
        &self,
        namespace_resolver: &dyn PathReferenceNamespaceResolver,
        source_file_path: &Path,
    ) -> DataSetResult<PathReference> {
        canonicalized_absolute_path(
            &self.namespace,
            &self.path,
            &self.importable_name,
            namespace_resolver,
            source_file_path,
        )
    }
}

// A hash of a potentially non-canonical path. Be very careful how these are used to check equality. It should really
// only be done to look up a path identified during a scan/import.
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct PathReferenceHash(pub Uuid);

// This path reference is good for parsing from string and representing a path other than the canonical path reference
// (i.e. an absolute path when it could be represented relative to a namespace.)
#[derive(Debug, Clone)]
pub struct PathReference {
    namespace: String,
    path: String,
    importable_name: ImportableName,
}

impl Display for PathReference {
    fn fmt(
        &self,
        f: &mut Formatter<'_>,
    ) -> std::fmt::Result {
        if self.namespace.is_empty() {
            if let Some(importable_name) = self.importable_name.name() {
                write!(f, "{}#{}", self.path, importable_name)
            } else {
                write!(f, "{}", self.path)
            }
        } else {
            if let Some(importable_name) = self.importable_name.name() {
                write!(f, "{}://{}#{}", self.namespace, self.path, importable_name)
            } else {
                write!(f, "{}://{}", self.namespace, self.path)
            }
        }
    }
}

impl PathReference {
    pub fn new(
        namespace: String,
        path: String,
        importable_name: ImportableName,
    ) -> Self {
        PathReference {
            namespace,
            path,
            importable_name,
        }
    }

    pub fn path_reference_hash(&self) -> PathReferenceHash {
        let mut hasher = siphasher::sip128::SipHasher::default();
        self.to_string().hash(&mut hasher);
        let path_reference_hash = Uuid::from_u128(hasher.finish128().as_u128());
        PathReferenceHash(path_reference_hash)
    }

    pub fn namespace(&self) -> &str {
        &self.namespace
    }

    pub fn path(&self) -> &str {
        &self.path
    }

    pub fn importable_name(&self) -> &ImportableName {
        &self.importable_name
    }

    pub fn canonicalized_absolute_path(
        &self,
        namespace_resolver: &dyn PathReferenceNamespaceResolver,
        source_file_path: &Path,
    ) -> DataSetResult<PathReference> {
        canonicalized_absolute_path(
            &self.namespace,
            &self.path,
            &self.importable_name,
            namespace_resolver,
            source_file_path,
        )
    }

    pub fn simplify(
        self,
        namespace_resolver: &dyn PathReferenceNamespaceResolver,
    ) -> CanonicalPathReference {
        if !self.namespace.is_empty() {
            // If it has a namespace it can't be simplified
        } else if Path::new(&self.path).is_relative() {
            // If it's a relative path it can't be simplified
        } else {
            // If it's an absolute path, see if it is in a namespace, if it is, we can return a PathReference relative
            // to the namespace
            let canonicalized_path = PathBuf::from(&self.path).canonicalize().unwrap();

            if let Some((namespace, prefix)) = namespace_resolver.simplify_path(&canonicalized_path)
            {
                return CanonicalPathReference {
                    namespace,
                    path: prefix.to_string_lossy().to_string(),
                    importable_name: self.importable_name,
                };
            }
        }

        CanonicalPathReference {
            namespace: self.namespace,
            path: self.path,
            importable_name: self.importable_name,
        }
    }
}

impl From<&str> for PathReference {
    fn from(s: &str) -> PathReference {
        let namespace_delimeter_position = s.rfind("://");
        let importable_name_delimeter_position = s.rfind('#');

        let (path_start_position, namespace) =
            if let Some(namespace_delimeter_position) = namespace_delimeter_position {
                (
                    namespace_delimeter_position + 3,
                    s[0..namespace_delimeter_position].to_string(),
                )
            } else {
                (0, String::default())
            };

        let (path, importable_name) =
            if let Some(importable_name_delimeter_position) = importable_name_delimeter_position {
                let path = s[path_start_position..importable_name_delimeter_position].to_string();
                let importable_name = &s[importable_name_delimeter_position + 1..];
                let importable_name = if !importable_name.is_empty() {
                    ImportableName::new(importable_name.to_string())
                } else {
                    ImportableName::default()
                };
                (path, importable_name)
            } else {
                (
                    s[path_start_position..].to_string(),
                    ImportableName::default(),
                )
            };

        PathReference {
            namespace,
            path,
            importable_name,
        }
    }
}

impl From<String> for PathReference {
    fn from(path: String) -> PathReference {
        let str: &str = &path;
        PathReference::from(str)
    }
}

impl From<&String> for PathReference {
    fn from(path: &String) -> PathReference {
        let str: &str = &path;
        PathReference::from(str)
    }
}

impl From<&Path> for PathReference {
    fn from(path: &Path) -> PathReference {
        let str: &str = path.to_str().unwrap();
        PathReference::from(str)
    }
}

impl From<&PathBuf> for PathReference {
    fn from(path: &PathBuf) -> PathReference {
        let str: &str = path.to_str().unwrap();
        PathReference::from(str)
    }
}

impl From<PathBuf> for PathReference {
    fn from(path: PathBuf) -> PathReference {
        let str: &str = path.to_str().unwrap();
        PathReference::from(str)
    }
}