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
#![allow(unused_imports)]
#![allow(unused_variables)]
#[macro_use]
mod macros {
use std::io::{Result, Error, ErrorKind};
use std;
fn to_io_error<E: std::error::Error>(error: E) -> Error {
Error::new(ErrorKind::Other, error.description())
}
pub fn to_io_result<T, E: std::error::Error>(result: std::result::Result<T, E>) -> Result<T> {
match result {
Ok(result) => Ok(result),
Err(error) => Err(to_io_error(error)),
}
}
macro_rules! ctry {
($result:expr) => (try!($crate::macros::to_io_result($result)));
}
}
pub mod physical;
pub use physical::PhysicalFS;
pub mod memory;
pub use memory::MemoryFS;
pub mod util;
use std::path::Path;
use std::convert::AsRef;
use std::fmt::Debug;
use std::io::{Read, Write, Seek, Result};
use std::borrow::Cow;
pub trait VPath: Debug + std::marker::Send + std::marker::Sync {
fn open_with_options(&self, openOptions: &OpenOptions) -> Result<Box<VFile>>;
fn open(&self) -> Result<Box<VFile>> {
self.open_with_options(OpenOptions::new().read(true))
}
fn create(&self) -> Result<Box<VFile>> {
self.open_with_options(OpenOptions::new().write(true).create(true).truncate(true))
}
fn append(&self) -> Result<Box<VFile>> {
self.open_with_options(OpenOptions::new().write(true).create(true).append(true))
}
fn mkdir(&self) -> Result<()>;
fn file_name(&self) -> Option<String>;
fn extension(&self) -> Option<String>;
fn resolve(&self, path: &String) -> Box<VPath>;
fn parent(&self) -> Option<Box<VPath>>;
fn exists(&self) -> bool;
fn metadata(&self) -> Result<Box<VMetadata>>;
fn read_dir(&self) -> Result<Box<Iterator<Item = Result<Box<VPath>>>>>;
fn to_string(&self) -> Cow<str>;
fn box_clone(&self) -> Box<VPath>;
}
impl Clone for Box<VPath> {
fn clone(&self) -> Box<VPath> {
self.box_clone()
}
}
pub fn resolve<S: Into<String>>(base: &VPath, path: S) -> Box<VPath> {
base.resolve(&path.into())
}
pub trait VFile: Read + Write + Seek + Debug {}
impl<T> VFile for T where T: Read + Write + Seek + Debug {}
pub trait VMetadata {
fn is_dir(&self) -> bool;
fn is_file(&self) -> bool;
fn len(&self) -> u64;
}
pub trait VFS {
type PATH: VPath;
type FILE: VFile;
type METADATA: VMetadata;
fn path<T: Into<String>>(&self, path: T) -> Self::PATH;
}
#[derive(Debug, Default)]
pub struct OpenOptions {
read: bool,
write: bool,
create: bool,
append: bool,
truncate: bool,
}
impl OpenOptions {
pub fn new() -> OpenOptions {
Default::default()
}
pub fn read(&mut self, read: bool) -> &mut OpenOptions {
self.read = read;
self
}
pub fn write(&mut self, write: bool) -> &mut OpenOptions {
self.write = write;
self
}
pub fn create(&mut self, create: bool) -> &mut OpenOptions {
self.create = create;
self
}
pub fn append(&mut self, append: bool) -> &mut OpenOptions {
self.append = append;
self
}
pub fn truncate(&mut self, truncate: bool) -> &mut OpenOptions {
self.truncate = truncate;
self
}
}