Skip to main content

darklua_core/frontend/
mod.rs

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