Deserializing Serde JSON AST to Program #7801
-
Hi everyone, apologies in advance as I'm new to Rust and have been working on this project for a little while. pub fn source_to_ast<'a>(file_path: &str, allocator: &'a Allocator) -> Result<Program<'a>, String> {
if !Path::new(file_path).exists() {
return Err(format!("File does not exist"));
}
let source_text = fs::read_to_string(file_path)
.map_err(|e| format!("Failed to read the JavaScript file: {}", e))?
.into_boxed_str();
let source_text: &'a str = Box::leak(source_text);
let source_type = SourceType::from_path(file_path)
.map_err(|e| format!("Failed to determine source type: {:?}", e))?;
let parser = Parser::new(allocator, source_text, source_type).with_options(ParseOptions {
parse_regular_expression: true,
..ParseOptions::default()
});
let parse_result = parser.parse();
Ok(parse_result.program)
} And I convert the AST to JSON using the following code: let allocator = Allocator::default(); // Create an OXC allocator
match source_to_ast(&file_path, &allocator) {
Ok(ast) => {
// Attempt to serialize the AST into JSON
let ast_json_result: Result<Value, String> =
to_value(&ast).map_err(|e| format!("Serialization error: {}", e));
let (status, message) = match ast_json_result {
// If serialization is successful, modify the JSON object and return it
Ok(mut ast_json) => {
if let Value::Object(ref mut map) = ast_json {
map.insert(
"source_text".to_string(),
Value::String(ast.source_text.to_string()),
);
}
(atoms::ok(), ast_json.to_string())
}
// If serialization fails, send the error message to Elixir
Err(serialization_error) => (atoms::error(), serialization_error),
};
encode_response(env, status, atoms::source_to_ast_nif(), message)
}
// If the source_to_ast function fails, send the error message to Elixir
Err(msg) => encode_response(env, atoms::error(), atoms::source_to_ast_nif(), msg),
} Now, I want to do the reverse: convert JSON back into a Program. However, I suspect that deserialization (Deserialize) might not be supported.
Any suggestions or guidance would be greatly appreciated! 😊 |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
I'm sorry but we've not implemented Our current work in this area is around creating a framework for serialization/deserialization on top of So TDLR: This is not possible at present, and I'm sorry to say it will not be coming in immediate future. But better interop with JS/JSON is something we consider important, and we will be aiming to make progress on it in the new year. Out of interest, why are you needing to covert AST to JSON and back? |
Beta Was this translation helpful? Give feedback.
No worries. Happy to help (except that I'm not being very helpful!)
I don't know anything about Elixir, but I think I understand what you're trying to do, and great that you're looking to port Oxc.
However, I'm sorry to say I don't think what you need is possible at present.
What you've suggested is likely the best approach - using
serde
to serialize/deserialize to a format (e.g. JSON) which is supported in both Rust and Erlang.But problem is that it requires
Deserialize
to be implemented on AST types on Rust side. We will do that, but we are not in a position to do it yet. It's hard to do properly, and I'm afraid we would not want to accept a PR for a "partial" implementation, as then i…