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
use rocket::http::{Status, ContentType};
use rocket::response::{Response, Responder, NamedFile};
use rocket::request::Request;

use super::CachedFile;


/// Wrapper around types that represent files and implement Responder<'static>.
#[derive(Debug)]
pub enum ResponderFile {
    Cached(CachedFile),
    FileSystem(NamedFile)
}


impl From<CachedFile> for ResponderFile {
    fn from(cached_file: CachedFile) -> Self {
        ResponderFile::Cached(cached_file)
    }
}

impl From<NamedFile> for ResponderFile {
    fn from(named_file: NamedFile) -> Self {
        ResponderFile::FileSystem(named_file)
    }
}

impl Responder<'static> for ResponderFile {
    fn respond_to(self, request: &Request) -> Result<Response<'static>, Status> {

        match self {
            ResponderFile::Cached(cached_file) => cached_file.respond_to(request),
            ResponderFile::FileSystem(named_file) => named_file.respond_to(request)
        }
    }
}


impl PartialEq for ResponderFile {
    fn eq(&self, other: &ResponderFile) -> bool {
        match *self {
            ResponderFile::Cached(ref lhs_cached_file) => {
                match *other {
                    ResponderFile::Cached(ref rhs_cached_file) => {
                        *rhs_cached_file == *lhs_cached_file
                    }
                    ResponderFile::FileSystem(_) => {
                        false
                    }
                }
            }
            ResponderFile::FileSystem(ref lhs_named_file) => {
                match *other {
                    ResponderFile::Cached(_) => {
                        false
                    }
                    ResponderFile::FileSystem(ref rhs_named_file) => {
                        // Since all we have is a file handle this will settle for just comparing the paths for now
                        *lhs_named_file.path() == *rhs_named_file.path()
                    }
                }
            }
        }

    }
}