Skip to main content

lua_assembler/formats/luac/
mod.rs

1use crate::program::{LuaProgram, LuaVersion};
2use gaia_types::{
3    helpers::{open_file, Url},
4    GaiaError,
5};
6use std::{fmt::Debug, io::BufReader, path::Path};
7
8pub mod reader;
9pub mod view;
10pub mod writer;
11
12#[derive(Clone, Debug, PartialEq, Eq)]
13pub struct LuacReadConfig {
14    pub url: Option<Url>,
15    pub version: LuaVersion,
16    pub check_magic_head: bool,
17}
18
19#[derive(Copy, Clone, Debug, PartialEq, Eq)]
20pub struct LuacWriteConfig {}
21
22impl Default for LuacReadConfig {
23    fn default() -> Self {
24        Self { url: None, version: LuaVersion::Unknown, check_magic_head: true }
25    }
26}
27
28pub fn luac_read_path(path: &Path) -> Result<LuaProgram, GaiaError> {
29    let (file, url) = open_file(path)?;
30    let mut config = LuacReadConfig::default();
31    config.url = Some(url);
32    let reader = config.as_reader(BufReader::new(file));
33    let result = reader.finish();
34    result.result
35}
36
37// 为了兼容文档测试,提供简化的函数别名
38pub fn read_luac_file(path: &Path) -> Result<LuaProgram, GaiaError> {
39    luac_read_path(path)
40}
41
42pub fn write_luac_file(_path: &Path, _program: &LuaProgram) -> Result<(), GaiaError> {
43    // TODO: 实现写入功能
44    Err(GaiaError::custom_error("write_luac_file not implemented yet"))
45}