-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathinstall.rs
More file actions
67 lines (58 loc) · 2.35 KB
/
install.rs
File metadata and controls
67 lines (58 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#[cfg(feature = "install")]
use anyhow::Result;
#[cfg(feature = "install")]
use eldritch::Interpreter;
use eldritch::assets::std::{EmbeddedAssets, StdAssetsLibrary};
use std::sync::Arc;
#[cfg(feature = "install")]
pub async fn install() -> Result<()> {
#[cfg(feature = "verbose-logging")]
log::info!("starting installation");
let asset_backend = Arc::new(EmbeddedAssets::<crate::assets::Asset>::new());
// Iterate through all embedded files using the Asset struct from assets.rs
for embedded_file_path in crate::assets::Asset::iter() {
// Find "main.eldritch" files
if embedded_file_path.ends_with("main.eldritch") {
#[cfg(feature = "verbose-logging")]
log::info!("loading tome {}", embedded_file_path);
let content = match crate::assets::Asset::get(&embedded_file_path) {
Some(f) => String::from_utf8_lossy(&f.data).to_string(),
None => {
#[cfg(feature = "verbose-logging")]
log::error!("failed to load install asset: {}", embedded_file_path);
continue;
}
};
#[cfg(feature = "verbose-logging")]
log::info!("running tome {}", embedded_file_path);
// Execute using Eldritch Interpreter
let mut locker = StdAssetsLibrary::new();
let _ = locker.add(asset_backend.clone());
let mut interpreter = Interpreter::new().with_default_libs();
interpreter.register_lib(locker);
match interpreter.interpret(&content) {
Ok(_) => {
#[cfg(feature = "verbose-logging")]
log::info!("Successfully executed {embedded_file_path}");
}
Err(_e) => {
#[cfg(feature = "verbose-logging")]
log::error!("Failed to execute {embedded_file_path}: {_e}");
}
}
}
}
Ok(())
}
#[cfg(test)]
#[cfg(feature = "install")]
mod tests {
use super::*;
#[tokio::test]
async fn test_install_execution() {
let result = install().await;
// It might fail during execution due to permissions (trying to write to /bin/imix),
// but the install function itself returns Ok(()) because we catch errors inside the loop.
assert!(result.is_ok());
}
}