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
use errors::Result;
use std::fmt;
use std::fs::File;
use std::io::{self, Cursor, Read};
use std::path::{Path, PathBuf};
use std::sync::Arc;

pub trait Object: Send + fmt::Display + fmt::Debug {
    /// Get a path to the object, if one exists.
    fn path(&self) -> Option<&Path>;

    /// Open a reader to the object.
    fn read(&self) -> Result<Box<Read>>;

    /// Lightweight cloning of this object.
    fn clone_object(&self) -> Box<Object>;

    /// Convert the current object with the given name.
    fn with_name(&self, name: String) -> Box<Object>;
}

/// An empty object.
#[derive(Debug)]
pub struct EmptyObject {
    name: Arc<String>,
}

impl EmptyObject {
    /// Create a new empty object with the given name.
    pub fn new<S: AsRef<str>>(name: S) -> EmptyObject {
        EmptyObject {
            name: Arc::new(name.as_ref().to_string()),
        }
    }
}

impl Object for EmptyObject {
    fn path(&self) -> Option<&Path> {
        None
    }

    fn read(&self) -> Result<Box<Read>> {
        Ok(Box::new(Cursor::new(&[])))
    }

    fn clone_object(&self) -> Box<Object> {
        Box::new(EmptyObject {
            name: Arc::clone(&self.name),
        })
    }

    fn with_name(&self, name: String) -> Box<Object> {
        Box::new(EmptyObject {
            name: Arc::new(name),
        })
    }
}

impl fmt::Display for EmptyObject {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "<{} (empty)>", self.name)
    }
}

/// An named object containing a fixed set of bytes.
#[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,
        }
    }
}

/// Adapt a vector in an Arc to be used in a Cursor.
struct ArcCursor(Arc<Vec<u8>>);

impl AsRef<[u8]> for ArcCursor {
    fn as_ref(&self) -> &[u8] {
        self.0.as_ref()
    }
}

impl Object for BytesObject {
    fn path(&self) -> Option<&Path> {
        None
    }

    fn read(&self) -> Result<Box<Read>> {
        Ok(Box::new(Cursor::new(ArcCursor(Arc::clone(&self.bytes)))))
    }

    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)
    }
}

pub struct StdinObject {
    name: Arc<String>,
}

impl StdinObject {
    pub fn new() -> Self {
        Self {
            name: Arc::new("stdin".to_string()),
        }
    }
}

impl Object for StdinObject {
    fn path(&self) -> Option<&Path> {
        None
    }

    fn read(&self) -> Result<Box<Read>> {
        Ok(Box::new(io::stdin()))
    }

    fn clone_object(&self) -> Box<Object> {
        Box::new(StdinObject {
            name: Arc::clone(&self.name),
        })
    }

    fn with_name(&self, name: String) -> Box<Object> {
        Box::new(StdinObject {
            name: Arc::new(name),
        })
    }
}

impl fmt::Debug for StdinObject {
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.debug_struct("StdinObject")
            .field("name", &self.name)
            .finish()
    }
}

impl fmt::Display for StdinObject {
    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        write!(formatter, "{}", self.name)
    }
}