seaofvoices / darklua

A command line tool that transforms Lua code
https://darklua.com/
MIT License
71 stars 9 forks source link

Process File from string into string #198

Closed Bobrokus closed 3 months ago

Bobrokus commented 3 months ago

It would be super useful to have the ability to process source code from a string into a string.

I plan on using this library in my project and need to process source code as a string. Reading the source code I've so far only found that it's only possible to process a file at a path.

I believe there must be code for processing string, but I haven't found it yet. I'll be looking further but I'm worried it'll be private. I might try to implement this myself if I don't find anything.

Bobrokus commented 3 months ago

Ok. I did find a way to minify code from a string:

pub fn minify(code: &str) -> anyhow::Result<String> {
    let mut generator = darklua_core::generator::DenseLuaGenerator::new(80);

    let parser = darklua_core::Parser::default();

    let block = parser
        .parse(code)
        .map_err(|parser_err| anyhow!(parser_err))?;

    generator.write_block(&block);
    let lua_code = generator.into_string();

    Ok(lua_code)
}
jeparlefrancais commented 3 months ago

It seems like you've found what you needed so I'll close this issue 🙂

Bobrokus commented 2 months ago

It seems like you've found what you needed so I'll close this issue 🙂

I have not actually. It did solve part of the problem but I still don't know how to apply custom rules, bundle, etc.