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
|
/*
* We implement most aspects of paths independently, not relying on
* std::path, on the theory that path syntax is such an important part of a
* shell that it doesn't make sense to try to integrate with non-Unix syntaxes.
* However, we do use std::path to print individual path components, in order
* to get the benefit of its functionality for handling non-Unicode filenames.
*/
use crate::prelude::*;
use crate::path::error::*;
use std::str::FromStr;
lalrpop_mod!(pub parser, "/path/parser.rs");
pub mod error;
#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub struct AbsoluteDirectoryPath {
directory_names: Vec<DirectoryName>,
}
#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub struct AbsoluteFilePath {
directory_names: Vec<DirectoryName>,
file_name: FileName,
}
#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub struct FileName(String);
#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub struct DirectoryName(String);
#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub struct GenericPath {
components: Vec<GenericPathComponent>,
starts_with_slash: bool,
ends_with_slash: bool,
}
#[derive(Clone,Debug,Eq,Hash,Ord,PartialEq,PartialOrd)]
pub enum GenericPathComponent {
FileOrDirectoryName(String),
CurrentDirectory,
ParentDirectory,
}
impl std::fmt::Display for AbsoluteDirectoryPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for directory_name in &self.directory_names {
f.write_str("/")?;
directory_name.fmt(f)?;
}
f.write_str("/")?;
Ok(())
}
}
impl std::fmt::Display for AbsoluteFilePath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for directory_name in &self.directory_names {
f.write_str("/")?;
directory_name.fmt(f)?;
}
f.write_str("/")?;
self.file_name.fmt(f)?;
Ok(())
}
}
impl std::fmt::Display for FileName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
FileName(name) => {
let std_path = std::path::Path::new(&name);
f.write_fmt(format_args!("{}", std_path.display()))?;
},
}
Ok(())
}
}
impl std::fmt::Display for DirectoryName {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DirectoryName(name) => {
let std_path = std::path::Path::new(&name);
f.write_fmt(format_args!("{}", std_path.display()))?;
},
}
Ok(())
}
}
impl std::fmt::Display for GenericPath {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.starts_with_slash {
f.write_str("/")?;
}
let mut is_first = true;
for component in &self.components {
if !is_first {
f.write_str("/")?;
}
component.fmt(f)?;
is_first = false;
}
if self.ends_with_slash {
f.write_str("/")?;
}
Ok(())
}
}
impl std::fmt::Display for GenericPathComponent {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
GenericPathComponent::FileOrDirectoryName(name) => {
let std_path = std::path::Path::new(&name);
f.write_fmt(format_args!("{}", std_path.display()))?;
},
GenericPathComponent::CurrentDirectory => {
f.write_str(".")?;
},
GenericPathComponent::ParentDirectory => {
f.write_str("..")?;
},
}
Ok(())
}
}
impl FromStr for FileName {
type Err = FileNameError;
fn from_str(input: &str) -> std::result::Result<Self, Self::Err> {
if input.find('/').is_some() {
Err(FileNameError::ContainsSlash(input.to_string()))
} else {
Ok(FileName(input.to_string()))
}
}
}
impl FromStr for DirectoryName {
type Err = DirectoryNameError;
fn from_str(input: &str) -> std::result::Result<Self, Self::Err> {
if input.find('/').is_some() {
Err(DirectoryNameError::ContainsSlash(input.to_string()))
} else {
Ok(DirectoryName(input.to_string()))
}
}
}
impl FromStr for GenericPathComponent {
type Err = PathError;
fn from_str(input: &str) -> std::result::Result<Self, Self::Err> {
parser::PathComponentParser::new().parse(input).map_err(
|e| PathError::Parse(e.to_string()))
}
}
impl AbsoluteDirectoryPath {
pub fn to_sys_path(&self) -> std::path::PathBuf {
let mut result = std::path::PathBuf::new();
result.push(format!("{}", self));
result
}
pub fn concat_file_name(&self, file_name: &FileName) -> AbsoluteFilePath {
AbsoluteFilePath {
directory_names: self.directory_names.clone(),
file_name: file_name.clone(),
}
}
}
impl AbsoluteFilePath {
pub fn to_sys_path(&self) -> std::path::PathBuf {
let mut result = std::path::PathBuf::new();
result.push(format!("{}", self));
result
}
}
pub fn parse_path_list(path_list: &str)
-> Result<Vec<AbsoluteDirectoryPath>>
{
match parser::PathListParser::new().parse(path_list) {
Ok(parsed_paths) => {
let mut result = Vec::new();
for generic_path in parsed_paths {
let path = absolute_directory_path(generic_path)?;
result.push(path);
}
Ok(result)
},
Err(original_error) => {
match parser::PathListAllowingEmptyPathsParser::new()
.parse(path_list)
{
Ok(_) => {
Err(Error::PathListHasEmptyComponents(path_list.to_string()))
},
Err(_) => {
Err(Error::Parse(original_error.to_string()))
},
}
},
}
}
pub fn absolute_directory_path(generic_path: GenericPath)
-> Result<AbsoluteDirectoryPath>
{
if !generic_path.starts_with_slash {
return Err(Error::PathLexicallyRelative(generic_path));
}
let mut flattened_components = Vec::new();
for component in &generic_path.components {
match component {
GenericPathComponent::CurrentDirectory => { },
GenericPathComponent::ParentDirectory => {
if flattened_components.len() > 0 {
flattened_components.pop();
} else {
return Err(Error::PathLexicallyInvalid(generic_path));
}
},
GenericPathComponent::FileOrDirectoryName(name) => {
flattened_components.push(DirectoryName(name.to_string()));
},
}
}
Ok(AbsoluteDirectoryPath {
directory_names: flattened_components,
})
}
pub fn absolute_file_path(generic_path: GenericPath)
-> Result<AbsoluteFilePath>
{
if !generic_path.starts_with_slash {
return Err(Error::PathLexicallyRelative(generic_path));
}
if generic_path.ends_with_slash {
return Err(Error::PathLexicallyDirectory(generic_path));
}
let mut iterator = generic_path.components.iter();
let file_name = match iterator.next_back() {
Some(GenericPathComponent::FileOrDirectoryName(name)) => {
FileName(name.to_string())
}
_ => {
return Err(Error::PathLexicallyInvalid(generic_path));
}
};
let mut flattened_components = Vec::new();
for component in &generic_path.components {
match component {
GenericPathComponent::CurrentDirectory => { },
GenericPathComponent::ParentDirectory => {
if flattened_components.len() > 0 {
flattened_components.pop();
} else {
return Err(Error::PathLexicallyInvalid(generic_path));
}
},
GenericPathComponent::FileOrDirectoryName(name) => {
flattened_components.push(DirectoryName(name.to_string()));
},
}
}
Ok(AbsoluteFilePath {
directory_names: flattened_components,
file_name: file_name,
})
}
|