web_static_pack/
pack.rs

1//! Pack related types. Provides [Pack] trait.
2
3use crate::{
4    common::{
5        file::{File as File_, FileArchived},
6        pack::{Pack as Pack_, PackArchived},
7    },
8    file::File,
9};
10
11/// Trait representing Pack, a container for files identified by path.
12///
13/// Most users will use [PackArchived] implementation, returned by
14/// [crate::loader::load].
15/// This trait is also implemented for non-archived [Pack_], mostly for testing
16/// purposes.
17pub trait Pack {
18    /// File type returned by this `pack`.
19    type File: File;
20
21    /// Given `pack` relative path, eg. `/dir1/dir2/file.html` returns file
22    /// associated with this path. Returns [None] if file for given path
23    /// does not exist.
24    fn get_file_by_path(
25        &self,
26        path: &str,
27    ) -> Option<&Self::File>;
28}
29impl Pack for Pack_ {
30    type File = File_;
31
32    fn get_file_by_path(
33        &self,
34        path: &str,
35    ) -> Option<&Self::File> {
36        let file = self.files_by_path.get(path)?;
37        Some(file)
38    }
39}
40impl Pack for PackArchived {
41    type File = FileArchived;
42
43    fn get_file_by_path(
44        &self,
45        path: &str,
46    ) -> Option<&Self::File> {
47        let file = self.files_by_path.get(path)?;
48        Some(file)
49    }
50}