amql-mutate 0.0.0-alpha.0

Pure source code mutation operations
Documentation
//! WASM entry point — thin wrappers over the mutation API.

use wasm_js_bridge::wasm_export;

use crate::{
    insert_source as insert_source_impl, move_node as move_node_impl,
    remove_node as remove_node_impl, replace_node as replace_node_impl, InsertPosition,
    MutationResult, NodeRef, RelativePath, RemoveResult,
};

/// Remove a node from the source text.
#[wasm_export]
#[must_use = "remove result contains modified source and detached text"]
pub fn remove_node(source: &str, node: &NodeRef) -> Result<RemoveResult, String> {
    remove_node_impl(source, node)
}

/// Insert source text relative to a target node.
#[wasm_export]
#[must_use = "insert result contains modified source and new node ref"]
pub fn insert_source(
    source: &str,
    file: &RelativePath,
    target: &NodeRef,
    position: InsertPosition,
    new_source: &str,
) -> Result<MutationResult, String> {
    insert_source_impl(source, file, target, position, new_source)
}

/// Replace a node's source text with new content.
#[wasm_export]
#[must_use = "replace result contains modified source and new node ref"]
pub fn replace_node(
    source: &str,
    file: &RelativePath,
    node: &NodeRef,
    new_source: &str,
) -> Result<MutationResult, String> {
    replace_node_impl(source, file, node, new_source)
}

/// Move a node to a new position relative to a target.
#[wasm_export]
#[must_use = "move result contains modified source"]
pub fn move_node(
    source: &str,
    file: &RelativePath,
    node: &NodeRef,
    target: &NodeRef,
    position: InsertPosition,
) -> Result<MutationResult, String> {
    move_node_impl(source, file, node, target, position)
}

wasm_js_bridge::bundle! {
    types   = [NodeKind, RelativePath, NodeRef, InsertPosition, MutationResult, RemoveResult],
    fns     = [remove_node, insert_source, replace_node, move_node],
    aliases = [],
    opaque  = [("NodeKind", Some("string")), ("RelativePath", Some("string"))],
}