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 contents of the file, compressed with zstd.
41 ///
42 /// This is `Some` if precompression has been done. `None` if the file was
43 /// not precompressed, either because the file doesn't benefit from
44 /// compression or because zstd was disabled with `#[zstd = false]`.
45 #[cfg(feature = "compression-zstd")]
46 fn data_zstd(&self) -> Option<Self::Data>;
47
48 /// The contents of the file, compressed with zstd.
49 ///
50 /// Always returns `None` when the compression-zstd feature is disabled.
51 #[cfg(not(feature = "compression-zstd"))]
52 fn data_zstd(&self) -> Option<Self::Data> {
53 None
54 }
55 /// The UNIX timestamp of when the file was last modified.
56 fn last_modified_timestamp(&self) -> Option<i64>;
57 /// The rfc2822 encoded last modified date. This is the format you use for
58 /// `Last-Modified` headers.
59 fn last_modified(&self) -> Option<Self::Meta>;
60 /// The hash value for the file. This is a base85 encoded sha256 hash.
61 fn hash(&self) -> Self::Meta;
62 /// The ETag value for the file. This is just the file hash, wrapped with
63 /// quote symbols.
64 fn etag(&self) -> Self::Meta;
65 /// The mime type for the file, if one can be guessed from the file
66 /// extension.
67 fn mime_type(&self) -> Option<Self::Meta>;
68}