How to Convert JSON Back to AST Tree in Rust? #4727
-
Hello friends. Sorry, I’m new to Rust. Recently, I’ve been working on this project, and I’m facing an issue. I can’t convert an AST structure that I’ve transformed into JSON back into a tree. For example, consider the following code: let js_code = r#"
function calculateWeight(value1, value2) {
const product = value1 * value2;
return `Your weight is ${product} kg.`;
}
"#;
let parse_result = parse_script(js_code, JsParserOptions::default());
let json_string = serde_json::to_string_pretty(&parse_result.tree()).unwrap();
println!("Generated JSON:\n{}", json_string); Now, how can I convert the Why do I need this?I want to transform a JavaScript code snippet into an AST, then convert the AST to JSON and send it to Elixir. After making the necessary modifications in Elixir, I want to send the JSON back to Rust, where I can convert it back into a tree to review the changes and handle other required tasks.
Thank you for your help! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 5 replies
-
You simply can't because our nodes don't implement the |
Beta Was this translation helpful? Give feedback.
I have a tiny bit of experience with Rustler, and I think you’re right: converting to JSON is probably your best bet.
The problem is that deserializing (whether it’s from JSON or something else) is quite a complex operation, given the data structures involved, and is indeed almost as difficult as implementing a custom parser, minus the syntax bits.
So you understand why we haven’t implemented this :)