darklua_core/frontend/
mod.rs

1mod configuration;
2mod error;
3mod options;
4mod resources;
5mod utils;
6mod work_cache;
7mod work_item;
8mod worker;
9mod worker_tree;
10
11pub use configuration::{BundleConfiguration, Configuration, GeneratorParameters};
12pub use error::{DarkluaError, DarkluaResult};
13pub use options::Options;
14pub use resources::Resources;
15use serde::Serialize;
16use work_item::WorkItem;
17use worker::Worker;
18pub use worker_tree::WorkerTree;
19
20use crate::{
21    generator::{DenseLuaGenerator, LuaGenerator},
22    nodes::{Block, ReturnStatement},
23    process::to_expression,
24    utils::normalize_path,
25};
26
27/// Convert serializable data into a Lua module.
28///
29/// This function takes any value that implements `Serialize` and converts it into a Lua module
30/// that returns the serialized data. The resulting Lua code will be a module that returns
31/// a table containing the serialized data.
32///
33/// # Example
34///
35/// ```rust
36/// # use serde::Serialize;
37/// # use darklua_core::convert_data;
38/// #[derive(Serialize)]
39/// struct ExampleData {
40///     name: String,
41///     value: i32,
42/// }
43///
44/// let config = ExampleData {
45///     name: "test".to_string(),
46///     value: 42,
47/// };
48///
49/// let lua_code = convert_data(config).unwrap();
50///
51/// assert_eq!(lua_code, "return{name='test',value=42}");
52/// ```
53pub fn convert_data(value: impl Serialize) -> Result<String, DarkluaError> {
54    let expression = to_expression(&value).map_err(DarkluaError::from)?;
55
56    let block = Block::default()
57        .with_last_statement(ReturnStatement::default().with_expression(expression));
58
59    let mut generator = DenseLuaGenerator::default();
60    generator.write_block(&block);
61    Ok(generator.into_string())
62}
63
64/// Process resources according to the given options.
65///
66/// This function is the main entry point for processing resources. It creates a [`WorkerTree`],
67/// collects work items based on the provided resources and options, and then processes them.
68pub fn process(resources: &Resources, options: Options) -> DarkluaResult<WorkerTree> {
69    let mut worker_tree = WorkerTree::default();
70
71    worker_tree.collect_work(resources, &options)?;
72    worker_tree.process(resources, options)?;
73
74    Ok(worker_tree)
75}