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
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 {
    /// Get a path to the object, if one exists.
    fn path(&self) -> Option<&Path>;

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

    fn clone(&self) -> Box<Object>;
}

#[derive(Debug)]
pub struct BytesObject {
    name: String,
    bytes: Arc<Vec<u8>>,
}

impl BytesObject {
    pub fn new(name: String, bytes: Arc<Vec<u8>>) -> BytesObject {
        BytesObject {
            name: 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(&self) -> Box<Object> {
        Box::new(BytesObject {
            name: self.name.clone(),
            bytes: self.bytes.clone(),
        })
    }
}

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

#[derive(Debug)]
pub struct PathObject {
    path: PathBuf,
}

impl PathObject {
    pub fn new<P: AsRef<Path>>(path: P) -> PathObject {
        PathObject { path: 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)?))
    }

    fn clone(&self) -> Box<Object> {
        Box::new(PathObject { path: self.path.clone() })
    }
}

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

impl From<PathObject> for Box<Object> {
    fn from(value: PathObject) -> Box<Object> {
        Box::new(value)
    }
}