rust_embed_for_web_utils/file/
embed.rs

1use super::common::EmbedableFile;
2use std::fmt::Debug;
3
4/// A file embedded into the binary.
5///
6/// `rust-embed-for-web` changes which type of file you get based on whether
7/// it's a debug or release build. In release builds or with the `always-embed`
8/// flag, you'll get `EmbeddedFile`s.
9///
10/// You should interface with this object using the `EmbedableFile` trait, which
11/// is implemented for both the embedded and dynamic files.
12#[derive(Clone, Copy)]
13pub struct EmbeddedFile {
14    name: &'static str,
15    data: &'static [u8],
16    data_gzip: Option<&'static [u8]>,
17    data_br: Option<&'static [u8]>,
18    hash: &'static str,
19    etag: &'static str,
20    last_modified: Option<&'static str>,
21    last_modified_timestamp: Option<i64>,
22    mime_type: Option<&'static str>,
23}
24
25impl EmbedableFile for EmbeddedFile {
26    type Data = &'static [u8];
27    type Meta = &'static str;
28
29    fn name(&self) -> Self::Meta {
30        self.name
31    }
32
33    fn data(&self) -> Self::Data {
34        self.data
35    }
36
37    fn data_gzip(&self) -> Option<Self::Data> {
38        self.data_gzip
39    }
40
41    fn data_br(&self) -> Option<Self::Data> {
42        self.data_br
43    }
44
45    fn last_modified(&self) -> Option<Self::Meta> {
46        self.last_modified
47    }
48
49    fn last_modified_timestamp(&self) -> Option<i64> {
50        self.last_modified_timestamp
51    }
52
53    fn hash(&self) -> Self::Meta {
54        self.hash
55    }
56
57    fn etag(&self) -> Self::Meta {
58        self.etag
59    }
60
61    fn mime_type(&self) -> Option<Self::Meta> {
62        self.mime_type
63    }
64}
65
66impl EmbeddedFile {
67    #[doc(hidden)]
68    #[allow(clippy::too_many_arguments)]
69    /// This is used internally in derived code to create embedded file objects.
70    /// You don't want to manually use this function!
71    pub fn __internal_make(
72        // Make sure that the order of these parameters is correct in respect to
73        // the file contents! And if you are changing or reordering any of
74        // these, make sure to update the corresponding call in `impl`
75        name: &'static str,
76        data: &'static [u8],
77        data_gzip: Option<&'static [u8]>,
78        data_br: Option<&'static [u8]>,
79        hash: &'static str,
80        etag: &'static str,
81        last_modified: Option<&'static str>,
82        last_modified_timestamp: Option<i64>,
83        mime_type: Option<&'static str>,
84    ) -> EmbeddedFile {
85        EmbeddedFile {
86            name,
87            data,
88            data_gzip,
89            data_br,
90            hash,
91            etag,
92            last_modified,
93            last_modified_timestamp,
94            mime_type,
95        }
96    }
97}
98
99impl Debug for EmbeddedFile {
100    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
101        f.debug_struct("EmbeddedFile")
102            .field("name", &self.name)
103            .field("hash", &self.hash)
104            .field("last_modified", &self.last_modified())
105            .field("mime_type", &self.mime_type)
106            .finish()
107    }
108}
109
110impl PartialEq for EmbeddedFile {
111    fn eq(&self, other: &Self) -> bool {
112        self.hash.eq(other.hash)
113    }
114}