rew_core/
lib.rs

1//! Core utilities and types for the Rew runtime system
2
3pub mod utils;
4pub mod logger;
5
6use serde::{Deserialize, Serialize};
7use std::path::PathBuf;
8use std::{
9  fs::File,
10  io::{Read, Seek, SeekFrom}
11};
12
13/// Build options for compiling Rew code
14#[derive(Debug, Clone, Default)]
15pub struct BuildOptions {
16  pub bundle_all: bool,
17  pub entry_file: Option<PathBuf>,
18}
19
20/// App configuration structure
21#[derive(Debug, Deserialize, Serialize, Clone)]
22pub struct AppManifest {
23  pub package: Option<String>,
24  pub version: Option<String>,
25  pub description: Option<String>,
26  pub entries: Option<String>,
27}
28
29#[derive(Debug, Deserialize, Serialize, Clone)]
30pub struct AppConfig {
31  pub manifest: Option<AppManifest>,
32  pub entries: Option<std::collections::HashMap<String, String>>,
33}
34
35#[derive(Debug, Clone)]
36pub struct AppInfo {
37  pub path: PathBuf,
38  pub config: AppConfig,
39}
40
41
42#[derive(Default)]
43pub struct RuntimeState {
44  pub current_dir: PathBuf,
45  pub args: Vec<String>,
46}
47
48pub fn load_embedded_script() -> Option<String> {
49  let exe = std::env::current_exe().ok()?;
50  let mut f = File::open(&exe).ok()?;
51
52  // footer = [len (u64 LE)] [magic 4 bytes]
53  f.seek(SeekFrom::End(-12)).ok()?;
54  let mut footer = [0u8; 12];
55  f.read_exact(&mut footer).ok()?;
56
57  if &footer[8..12] != b"REW!" {
58      return None; // not patched
59  }
60  let len = u64::from_le_bytes(footer[0..8].try_into().unwrap());
61
62  // read payload
63  f.seek(SeekFrom::End(-(12 + len as i64))).ok()?;
64  let mut data = vec![0u8; len as usize];
65  f.read_exact(&mut data).ok()?;
66
67  String::from_utf8(data).ok()
68}
69