artushak_web_assets/
lib.rs1pub mod asset_cache;
2pub mod asset_config;
3pub mod asset_filter;
4pub mod assets;
5mod test;
6
7use std::{fs::File, path::Path};
8
9use log::debug;
10
11use crate::{
12 asset_cache::{AssetCacheManifest, AssetCacheManifestVersioned},
13 asset_config::AssetConfig,
14 asset_filter::AssetFilterRegistry,
15 assets::{AssetFilterError, AssetManifest, AssetResult},
16};
17
18pub fn load_cache_manifest<E>(cache_manifest_path: &Path) -> AssetResult<AssetCacheManifest, E>
20where
21 E: AssetFilterError,
22{
23 let cache_manifest: AssetCacheManifestVersioned = if cache_manifest_path.exists() {
24 let cache_manifest_file = std::fs::File::open(cache_manifest_path)?;
25 serde_json::from_reader(cache_manifest_file)?
26 } else {
27 AssetCacheManifestVersioned::default()
28 };
29
30 match cache_manifest {
31 AssetCacheManifestVersioned::V1(cache_manifest_v1) => Ok(cache_manifest_v1),
32 }
33}
34
35pub fn pack<E>(
37 manifest_path: &Path,
38 cache_manifest_path: &Path,
39 config: &AssetConfig,
40 filter_registry: &AssetFilterRegistry<E>,
41) -> AssetResult<(), E>
42where
43 E: AssetFilterError,
44{
45 let manifest: AssetManifest;
46 {
47 let manifest_file = File::open(manifest_path)?;
48 manifest = serde_json::from_reader(manifest_file)?;
49 }
50
51 let cache_manifest: AssetCacheManifestVersioned = if cache_manifest_path.exists() {
52 let cache_manifest_file = std::fs::File::open(cache_manifest_path)?;
53 serde_json::from_reader(cache_manifest_file)?
54 } else {
55 AssetCacheManifestVersioned::default()
56 };
57
58 match cache_manifest {
59 AssetCacheManifestVersioned::V1(mut cache_manifest_v1) => {
60 debug!("Processing assets...");
61
62 let result =
63 cache_manifest_v1.process_public_assets(config, &manifest, filter_registry);
64
65 if result.is_ok() {
66 debug!("Assets were processed");
67 }
68
69 {
70 let cache_manifest_file = std::fs::File::create(cache_manifest_path)?;
71 serde_json::to_writer(
72 cache_manifest_file,
73 &AssetCacheManifestVersioned::V1(cache_manifest_v1),
74 )?;
75 }
76
77 result
78 }
79 }
80}