rhaiscript / rhai

Rhai - An embedded scripting language for Rust.
https://crates.io/crates/rhai
Apache License 2.0
3.8k stars 177 forks source link

is there any way to serialize and deserialize rhai ast from and to string? #547

Closed OfekShochat closed 2 years ago

OfekShochat commented 2 years ago

hello! Im trying to cache asts but I don't see any way to do this. I don't think #161 is finished, but idk. is there any standard way of doing this or should I try and take all the info and then reconstruct the ast? thanks! ps great project

schungx commented 2 years ago

Well, I actually tried something like this in my on fork, in a branch called ast_serde. It is very old though.

https://github.com/schungx/rhai/tree/ast_serde

At half ways, I realized that there really is no point in doing this, as persisting the original script is easier and quicker than generating a string serialization from an AST.

Therefore, the standard way is to persist the script text itself (which can be compressed easily if you're really pressed for space).

Parsing is reasonably fast so text -> AST shouldn't be a problem if you do it every time. It is certainly not going to be more costly than deserializing with serde.

OfekShochat commented 2 years ago

sure, cool (not going to deserialize it every time tho, I have a cache that is initialized from a db)