web_static_pack/
file.rs

1//! Single file related types. Provides [File] trait.
2
3use crate::{
4    cache_control::CacheControl,
5    common::file::{File as File_, FileArchived},
6};
7use http::HeaderValue;
8
9/// Trait for single file inside a `pack`. Consists of body in different
10/// encodings (`identity` aka `normal`, `gzip`, `brotli`), some precomputed
11/// header values etc.
12///
13/// Most users will indirectly use [FileArchived] implementation, obtained from
14/// [crate::pack::Pack::get_file_by_path] (implemented by
15/// [crate::common::pack::PackArchived]).
16/// This trait is also implemented for non-archived [File_], mostly for testing
17/// purposes.
18pub trait File {
19    // content with different types
20    /// Accesses file content in original (`identity`) encoding.
21    fn content(&self) -> &[u8];
22    /// Accesses file content in `gzip` encoding if available.
23    fn content_gzip(&self) -> Option<&[u8]>;
24    /// Accesses file content in `brotli` encoding if available.
25    fn content_brotli(&self) -> Option<&[u8]>;
26
27    // headers
28    /// Accesses `content-type` header contents for this file.
29    fn content_type(&self) -> HeaderValue;
30    /// Accesses `ETag` header contents for this file.
31    fn etag(&self) -> HeaderValue;
32    /// Accesses [CacheControl] for this file.
33    fn cache_control(&self) -> CacheControl;
34}
35impl File for File_ {
36    fn content(&self) -> &[u8] {
37        &self.content
38    }
39    fn content_gzip(&self) -> Option<&[u8]> {
40        self.content_gzip.as_deref()
41    }
42    fn content_brotli(&self) -> Option<&[u8]> {
43        self.content_brotli.as_deref()
44    }
45
46    fn content_type(&self) -> HeaderValue {
47        HeaderValue::from_str(&self.content_type).unwrap()
48    }
49    fn etag(&self) -> HeaderValue {
50        HeaderValue::from_str(&self.etag).unwrap()
51    }
52    fn cache_control(&self) -> CacheControl {
53        CacheControl::from(self.cache_control)
54    }
55}
56impl File for FileArchived {
57    fn content(&self) -> &[u8] {
58        &self.content
59    }
60    fn content_gzip(&self) -> Option<&[u8]> {
61        self.content_gzip.as_deref()
62    }
63    fn content_brotli(&self) -> Option<&[u8]> {
64        self.content_brotli.as_deref()
65    }
66
67    fn content_type(&self) -> HeaderValue {
68        HeaderValue::from_str(&self.content_type).unwrap()
69    }
70    fn etag(&self) -> HeaderValue {
71        HeaderValue::from_str(&self.etag).unwrap()
72    }
73    fn cache_control(&self) -> CacheControl {
74        CacheControl::from(self.cache_control)
75    }
76}