actix_web_rust_embed_responder/
rust_embed_for_web.rs

1use crate::embed::{EmbedRespondable, EmbedResponse, IntoResponse};
2use actix_web::body::MessageBody;
3use rust_embed_for_web::{DynamicFile, EmbedableFile, EmbeddedFile};
4
5#[cfg(all(debug_assertions, not(feature = "always-embed")))]
6/// This is an alias that changes whether it refers to a `DynamicFile` or
7/// `EmbeddedFile` based on whether it's in debug or release mode.
8///
9/// This is necessary if you are trying to avoid using `dyn` trait objects.
10/// Check [this example](https://github.com/SeriousBug/actix-web-rust-embed-responder/blob/main/examples/rust_embed_for_web.rs)
11/// for details.
12pub type EmbedableFileResponse = WebEmbedableFile<DynamicFile>;
13
14// --> If you update the docs above, copy and paste it below too!
15
16#[cfg(any(not(debug_assertions), feature = "always-embed"))]
17/// This is an alias that changes whether it refers to a `DynamicFile` or
18/// `EmbeddedFile` based on whether it's in debug or release mode.
19///
20/// This is necessary if you are trying to avoid using `dyn` trait objects.
21/// Check [this example](https://github.com/SeriousBug/actix-web-rust-embed-responder/blob/main/examples/rust_embed_for_web.rs)
22/// for details.
23pub type EmbedableFileResponse = WebEmbedableFile<EmbeddedFile>;
24
25impl From<EmbeddedFile> for EmbedResponse<WebEmbedableFile<EmbeddedFile>> {
26    fn from(file: EmbeddedFile) -> Self {
27        EmbedResponse {
28            file: Some(WebEmbedableFile(file)),
29            compress: Default::default(),
30        }
31    }
32}
33
34impl From<Option<EmbeddedFile>> for EmbedResponse<WebEmbedableFile<EmbeddedFile>> {
35    fn from(file: Option<EmbeddedFile>) -> Self {
36        EmbedResponse {
37            file: file.map(WebEmbedableFile),
38            compress: Default::default(),
39        }
40    }
41}
42
43impl IntoResponse<WebEmbedableFile<EmbeddedFile>> for EmbeddedFile {
44    fn into_response(self) -> EmbedResponse<WebEmbedableFile<EmbeddedFile>> {
45        self.into()
46    }
47}
48
49impl IntoResponse<WebEmbedableFile<EmbeddedFile>> for Option<EmbeddedFile> {
50    fn into_response(self) -> EmbedResponse<WebEmbedableFile<EmbeddedFile>> {
51        self.into()
52    }
53}
54
55impl From<DynamicFile> for EmbedResponse<WebEmbedableFile<DynamicFile>> {
56    fn from(file: DynamicFile) -> Self {
57        EmbedResponse {
58            file: Some(WebEmbedableFile(file)),
59            compress: Default::default(),
60        }
61    }
62}
63
64impl From<Option<DynamicFile>> for EmbedResponse<WebEmbedableFile<DynamicFile>> {
65    fn from(file: Option<DynamicFile>) -> Self {
66        EmbedResponse {
67            file: file.map(WebEmbedableFile),
68            compress: Default::default(),
69        }
70    }
71}
72
73impl IntoResponse<WebEmbedableFile<DynamicFile>> for DynamicFile {
74    fn into_response(self) -> EmbedResponse<WebEmbedableFile<DynamicFile>> {
75        self.into()
76    }
77}
78
79impl IntoResponse<WebEmbedableFile<DynamicFile>> for Option<DynamicFile> {
80    fn into_response(self) -> EmbedResponse<WebEmbedableFile<DynamicFile>> {
81        self.into()
82    }
83}
84
85/// A wrapper around the 2 types of embedable files that `rust-embed-for-web` provides.
86///
87/// You shouldn't manually create objects of this struct, you should rely on
88/// `.into_response()` or `.into()` to create these from `DynamicFile`s or
89/// `EmbeddedFile`s you get from your `RustEmbed`.
90pub struct WebEmbedableFile<T: EmbedableFile>(T);
91
92impl<T: EmbedableFile> EmbedRespondable for WebEmbedableFile<T>
93where
94    T::Data: MessageBody,
95{
96    type Data = T::Data;
97    type DataGzip = T::Data;
98    type DataBr = T::Data;
99    type DataZstd = T::Data;
100    type ETag = T::Meta;
101    type LastModified = T::Meta;
102    type MimeType = T::Meta;
103
104    fn data(&self) -> Self::Data {
105        self.0.data()
106    }
107
108    fn data_gzip(&self) -> Option<Self::DataGzip> {
109        self.0.data_gzip()
110    }
111
112    fn data_br(&self) -> Option<Self::DataBr> {
113        self.0.data_br()
114    }
115
116    fn data_zstd(&self) -> Option<Self::DataZstd> {
117        self.0.data_zstd()
118    }
119
120    fn last_modified(&self) -> Option<Self::LastModified> {
121        self.0.last_modified()
122    }
123
124    fn last_modified_timestamp(&self) -> Option<i64> {
125        self.0.last_modified_timestamp()
126    }
127
128    fn etag(&self) -> Self::ETag {
129        self.0.etag()
130    }
131
132    fn mime_type(&self) -> Option<Self::MimeType> {
133        self.0.mime_type()
134    }
135}