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
use errors::*;
use std::fmt;
use std::fs::File;
use std::io::{Cursor, Read};
use std::path::{Path, PathBuf};
use std::sync::Arc;
pub trait Object: Send + fmt::Display + fmt::Debug {
fn path(&self) -> Option<&Path>;
fn read<'a>(&'a self) -> Result<Box<Read + 'a>>;
fn clone_object(&self) -> Box<Object>;
fn with_name(&self, name: String) -> Box<Object>;
}
#[derive(Debug)]
pub struct BytesObject {
name: Arc<String>,
bytes: Arc<Vec<u8>>,
}
impl BytesObject {
pub fn new(name: String, bytes: Arc<Vec<u8>>) -> BytesObject {
BytesObject {
name: Arc::new(name),
bytes: bytes,
}
}
}
impl Object for BytesObject {
fn path(&self) -> Option<&Path> {
None
}
fn read<'a>(&'a self) -> Result<Box<Read + 'a>> {
Ok(Box::new(Cursor::new(self.bytes.as_ref())))
}
fn clone_object(&self) -> Box<Object> {
Box::new(BytesObject {
name: Arc::clone(&self.name),
bytes: Arc::clone(&self.bytes),
})
}
fn with_name(&self, name: String) -> Box<Object> {
Box::new(BytesObject {
name: Arc::new(name),
bytes: Arc::clone(&self.bytes),
})
}
}
impl fmt::Display for BytesObject {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "<{}>", self.name)
}
}
#[derive(Debug)]
pub struct PathObject {
name: Option<Arc<String>>,
path: Arc<PathBuf>,
}
impl PathObject {
pub fn new<P: AsRef<Path>>(name: Option<String>, path: P) -> PathObject {
PathObject {
name: name.map(Arc::new),
path: Arc::new(path.as_ref().to_owned()),
}
}
}
impl Object for PathObject {
fn path(&self) -> Option<&Path> {
Some(self.path.as_ref())
}
fn read(&self) -> Result<Box<Read>> {
Ok(Box::new(File::open(self.path.as_path())?))
}
fn clone_object(&self) -> Box<Object> {
Box::new(PathObject {
name: self.name.clone(),
path: Arc::clone(&self.path),
})
}
fn with_name(&self, name: String) -> Box<Object> {
Box::new(PathObject {
name: Some(Arc::new(name)),
path: Arc::clone(&self.path),
})
}
}
impl fmt::Display for PathObject {
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
if let Some(ref name) = self.name {
write!(formatter, "{}", name)
} else {
write!(formatter, "{}", self.path.display())
}
}
}
impl From<PathObject> for Box<Object> {
fn from(value: PathObject) -> Box<Object> {
Box::new(value)
}
}