rust_embed_for_web_utils/file/common.rs
1/// An embedable file.
2///
3/// The file is embedded into the program for release builds, and dynamically
4/// read for debug builds. This trait allows common access for both types.
5///
6/// There are associated types for each file which you can get with
7/// `EmbeddedFile::Data`/`EmbeddedFile::Meta` or
8/// `DynamicFile::Data`/`DynamicFile::Meta`. The data of the file depends on
9/// this associated types because embedded files use static types while dynamic
10/// files have to use owned types.
11///
12/// You can access the data by calling the `as_ref` function through the `AsRef`
13/// trait. For example:
14/// ```
15/// # use rust_embed_for_web_utils::EmbedableFile;
16/// fn print_hash<T: EmbedableFile>(file: T) {
17/// println!("The file hash is {}", file.hash().as_ref());
18/// }
19/// ```
20pub trait EmbedableFile {
21 type Data: 'static + AsRef<[u8]>;
22 type Meta: 'static + AsRef<str>;
23
24 /// The name of the embedded file.
25 fn name(&self) -> Self::Meta;
26 /// The contents of the embedded file.
27 fn data(&self) -> Self::Data;
28 /// The contents of the file, compressed with gzip.
29 ///
30 /// This is `Some` if precompression has been done. `None` if the file was
31 /// not precompressed, either because the file doesn't benefit from
32 /// compression or because gzip was disabled with `#[gzip = false]`.
33 fn data_gzip(&self) -> Option<Self::Data>;
34 /// The contents of the file, compressed with brotli.
35 ///
36 /// This is `Some` if precompression has been done. `None` if the file was
37 /// not precompressed, either because the file doesn't benefit from
38 /// compression or because gzip was disabled with `#[br = false]`.
39 fn data_br(&self) -> Option<Self::Data>;
40 /// The UNIX timestamp of when the file was last modified.
41 fn last_modified_timestamp(&self) -> Option<i64>;
42 /// The rfc2822 encoded last modified date. This is the format you use for
43 /// `Last-Modified` headers.
44 fn last_modified(&self) -> Option<Self::Meta>;
45 /// The hash value for the file. This is a base85 encoded sha256 hash.
46 fn hash(&self) -> Self::Meta;
47 /// The ETag value for the file. This is just the file hash, wrapped with
48 /// quote symbols.
49 fn etag(&self) -> Self::Meta;
50 /// The mime type for the file, if one can be guessed from the file
51 /// extension.
52 fn mime_type(&self) -> Option<Self::Meta>;
53}