1use crate::config::{
22 AssetSource,
23 WasmModule,
24 CompressionType,
25 PackerConfig,
26};
27use crate::cli::{YamlRoot, Cli};
28use crate::encoder::{Base};
29use crate::encoder;
30use crate::wasmbuilder;
31use crate::html;
32use crate::fetcher;
33
34use std::error::Error;
36use std::fs;
37use std::path::PathBuf;
38
39use clap::{Parser};
40use base64::prelude::*;
41use sha2::{Sha256, Digest};
42
43const RUNTIME_ICON: &str = include_str!("../core/icon.svg");
45const RUNTIME_CORE_JS: &str = include_str!("../core/core.js");
46const RUNTIME_DECODER_JS: &str = include_str!("../core/wasm_decoder.js");
47const RUNTIME_DECODER_WASM: &[u8] = include_bytes!("../core/wasm_decoder_bg.wasm");
48
49pub async fn run() -> Result<(), Box<dyn Error>> {
55 let cli = Cli::parse();
57 println!("Config: {}", cli.config.display());
58 println!("Output: {}", cli.output.display());
59
60 let config = load_config(cli.config).await?;
61 pack(config, cli.output).await?;
62 Ok(())
63}
64
65pub async fn load_config(
67 config_path: PathBuf,
68) -> Result<PackerConfig, Box<dyn Error>> {
69 let yaml_text = fs::read_to_string(config_path)?;
70 let yaml_root: YamlRoot = serde_yaml::from_str(&yaml_text)?;
71 println!("Extracted yaml");
72 let config = crate::cli::set_config_from_yaml(yaml_root.pack).await?;
75 println!("Loaded config from yaml");
76 Ok(config)
77}
78
79fn default_runtime(
81icons: &mut Vec<String>,
83 scripts: &mut Vec<String>,
84 bin: &mut Vec<Base>,
85) {
86 println!("Default runtime is enabled. \
87 Adding icon, core.js and wasm_decoder");
88
89 let encoded_icon_string =
91 BASE64_STANDARD.encode(RUNTIME_ICON.as_bytes());
92 icons.insert(0, encoded_icon_string);
93
94 scripts.push(RUNTIME_DECODER_JS.to_string());
96 scripts.push(RUNTIME_CORE_JS.to_string());
97
98 let wasm_hash = Sha256::digest(RUNTIME_DECODER_WASM);
100 let wasm_hash_string = format!("{:x}", wasm_hash);
101 let wasm_encoded_text = BASE64_STANDARD.encode(RUNTIME_DECODER_WASM);
102 let decoder_module = Base {
103 id: "bin-wasm-decoder".to_string(),
104 hash: wasm_hash_string,
105 text: wasm_encoded_text,
106 };
107
108 bin.push(decoder_module);
109}
110
111pub async fn pack(
114 config: PackerConfig,
115 output: PathBuf,
116) -> Result<(), Box<dyn Error>> {
117 if let Some(ref modules) = config.wasm {
120 wasmbuilder::compile_wasm_modules(modules).await?;
121 }
122
123
124 let icon_sources = match config.favicon {
126 Some(source) => get_icons(source).await?,
127 None => vec![],
128 };
129 let mut icons = vec![];
131 if !icon_sources.is_empty() {
132 icons.push(icon_sources[0].clone());
133 }
134
135 let styles_text = match config.styles {
138 Some(source) => get_styles_text(source).await?,
139 None => "".to_string(),
140 };
141
142 let mut scripts = match config.scripts {
144 Some(source) => get_sources(source).await?,
145 None => vec![],
146 };
147
148 let mut bin = match config.wasm {
161 Some(source) => get_wasm(source)?,
162 None => vec![],
163 };
164
165 if config.runtime.enabled {
167 default_runtime(
168 &mut icons,
169 &mut scripts,
170 &mut bin,
171 );
172 }
173
174
175 let markup = html::page(
176 styles_text,
177 icons,
178 scripts,
179 bin,
180 );
181
182 let html = markup.into_string();
183 html::save_html(html, output)?;
185
186 Ok(())
187}
188
189async fn get_icons(
190 icon_sources: Vec<AssetSource>
191) -> Result<Vec<String>, Box<dyn Error>> {
192 let mut icons: Vec<String> = vec![];
193 for icon in icon_sources {
194 let path_str = match icon {
197 AssetSource::Local(path) => path.to_str().ok_or("Invalid favicon path")?.to_string(),
198 AssetSource::Remote(_) => return Err("Remote favicons not yet supported".into()),
199 };
200 let encoded_icon = crate::encoder::encode_base64(&path_str, "favicon")?;
202 icons.push(encoded_icon.text);
203 }
204 Ok(icons)
205}
206
207async fn get_styles_text(
209 style_sources: Vec<AssetSource>
210) -> Result<String, Box<dyn Error>> {
211 let mut styles_text = String::from("");
214 for source in style_sources {
215 let text = match source {
216 AssetSource::Local(path) => fetcher::get_local_file(&path)?,
217 AssetSource::Remote(url) => fetcher::get_remote_file(url).await?,
218 };
219 styles_text.push_str(&text);
221 }
222 Ok(styles_text)
223}
224
225async fn get_sources(
226 sources: Vec<AssetSource>
227) -> Result<Vec<String>, Box<dyn Error>> {
228 let mut source_text_list: Vec<String> = vec![];
229 for source in sources {
230 let text = match source {
231 AssetSource::Local(path) => fetcher::get_local_file(&path)?,
232 AssetSource::Remote(url) => fetcher::get_remote_file(url).await?,
233 };
234 source_text_list.push(text);
236 }
237 Ok(source_text_list)
238}
239
240fn get_wasm(
241 wasm_modules: Vec<WasmModule>
242) -> Result<Vec<Base>, Box<dyn Error>> {
243 let mut bin: Vec<Base> = vec![];
244 for module in wasm_modules {
245 let path_str = match module.source{
248 AssetSource::Local(path) => path.to_str().ok_or("Invalid WASM path")?.to_string(),
249 AssetSource::Remote(_) => return Err("Remote WASM modules not yet supported".into()),
250 };
251 let encoded_module = match module.compression {
253 CompressionType::Brotli => encoder::encode_brotli_base64(&path_str, &module.id)?,
254 CompressionType::None => encoder::encode_base64(&path_str, &module.id)?,
255 };
256 bin.push(encoded_module);
257 }
258 Ok(bin)
259}
260
261
262
263
264