use crate::json::{Object, Value};
use crate::node::LexicalNode;
use std::cell::Cell;
thread_local! {
static COMPACT_EXPORT: Cell<bool> = const { Cell::new(false) };
}
pub fn with_compact_export<R>(compact: bool, f: impl FnOnce() -> R) -> R {
struct Restore(bool);
impl Drop for Restore {
fn drop(&mut self) {
COMPACT_EXPORT.with(|c| c.set(self.0));
}
}
let _restore = Restore(COMPACT_EXPORT.with(|c| c.replace(compact)));
f()
}
pub fn is_compact_export() -> bool {
COMPACT_EXPORT.with(Cell::get)
}
pub fn export_node_json(node: &dyn LexicalNode) -> Object {
let serialized_node = crate::node_state::export_json_with_state(node, is_compact_export());
if serialized_node.get("type").and_then(Value::as_str) != Some(node.get_type()) {
panic!(
"LexicalNode: Node {} does not match the serialized type. Check if .exportJSON() is implemented and it is returning the correct type.",
node.get_type()
);
}
if node.element().is_some()
&& serialized_node
.get("children")
.and_then(Value::as_array)
.is_none()
{
panic!(
"LexicalNode: Node {} is an element but .exportJSON() does not have a children array.",
node.get_type()
);
}
serialized_node
}