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    #[cfg(feature = "compression-zstd")]
19    data_zstd: Option<&'static [u8]>,
20    hash: &'static str,
21    etag: &'static str,
22    last_modified: Option<&'static str>,
23    last_modified_timestamp: Option<i64>,
24    mime_type: Option<&'static str>,
25}
26
27impl EmbedableFile for EmbeddedFile {
28    type Data = &'static [u8];
29    type Meta = &'static str;
30
31    fn name(&self) -> Self::Meta {
32        self.name
33    }
34
35    fn data(&self) -> Self::Data {
36        self.data
37    }
38
39    fn data_gzip(&self) -> Option<Self::Data> {
40        self.data_gzip
41    }
42
43    fn data_br(&self) -> Option<Self::Data> {
44        self.data_br
45    }
46
47    #[cfg(feature = "compression-zstd")]
48    fn data_zstd(&self) -> Option<Self::Data> {
49        self.data_zstd
50    }
51
52    fn last_modified(&self) -> Option<Self::Meta> {
53        self.last_modified
54    }
55
56    fn last_modified_timestamp(&self) -> Option<i64> {
57        self.last_modified_timestamp
58    }
59
60    fn hash(&self) -> Self::Meta {
61        self.hash
62    }
63
64    fn etag(&self) -> Self::Meta {
65        self.etag
66    }
67
68    fn mime_type(&self) -> Option<Self::Meta> {
69        self.mime_type
70    }
71}
72
73impl EmbeddedFile {
74    #[doc(hidden)]
75    #[allow(clippy::too_many_arguments)]
76    /// This is used internally in derived code to create embedded file objects.
77    /// You don't want to manually use this function!
78    #[cfg(feature = "compression-zstd")]
79    pub fn __internal_make(
80        // Make sure that the order of these parameters is correct in respect to
81        // the file contents! And if you are changing or reordering any of
82        // these, make sure to update the corresponding call in `impl`
83        name: &'static str,
84        data: &'static [u8],
85        data_gzip: Option<&'static [u8]>,
86        data_br: Option<&'static [u8]>,
87        data_zstd: Option<&'static [u8]>,
88        hash: &'static str,
89        etag: &'static str,
90        last_modified: Option<&'static str>,
91        last_modified_timestamp: Option<i64>,
92        mime_type: Option<&'static str>,
93    ) -> EmbeddedFile {
94        EmbeddedFile {
95            name,
96            data,
97            data_gzip,
98            data_br,
99            data_zstd,
100            hash,
101            etag,
102            last_modified,
103            last_modified_timestamp,
104            mime_type,
105        }
106    }
107
108    #[doc(hidden)]
109    #[allow(clippy::too_many_arguments)]
110    /// This is used internally in derived code to create embedded file objects.
111    /// You don't want to manually use this function!
112    #[cfg(not(feature = "compression-zstd"))]
113    pub fn __internal_make(
114        // Make sure that the order of these parameters is correct in respect to
115        // the file contents! And if you are changing or reordering any of
116        // these, make sure to update the corresponding call in `impl`
117        name: &'static str,
118        data: &'static [u8],
119        data_gzip: Option<&'static [u8]>,
120        data_br: Option<&'static [u8]>,
121        _data_zstd: Option<&'static [u8]>, // Ignored when feature disabled
122        hash: &'static str,
123        etag: &'static str,
124        last_modified: Option<&'static str>,
125        last_modified_timestamp: Option<i64>,
126        mime_type: Option<&'static str>,
127    ) -> EmbeddedFile {
128        EmbeddedFile {
129            name,
130            data,
131            data_gzip,
132            data_br,
133            hash,
134            etag,
135            last_modified,
136            last_modified_timestamp,
137            mime_type,
138        }
139    }
140}
141
142impl Debug for EmbeddedFile {
143    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
144        f.debug_struct("EmbeddedFile")
145            .field("name", &self.name)
146            .field("hash", &self.hash)
147            .field("last_modified", &self.last_modified())
148            .field("mime_type", &self.mime_type)
149            .finish()
150    }
151}
152
153impl PartialEq for EmbeddedFile {
154    fn eq(&self, other: &Self) -> bool {
155        self.hash.eq(other.hash)
156    }
157}