Building documents

These operations construct a document one node at a time. For most operations, parse a document in your selected format. Use these operations only when you cannot parse the document from text.

  1. Create a builder.
  2. Add the nodes in preorder.
  3. Seal the builder to create an owned document.

Operations

OperationReturnsSummary
builderbuilderCreate an owned document builder.
Builder.addnodebuilder.add(node) appends one node and returns its preorder ID.
Builder.add_manynonebuilder.add_many(nodes) appends nodes in order in one native call.
Builder.sealdocumentbuilder.seal() transfers the constructed document into an independent owned document.
Builder.closenoneRelease the builder handle.

builder

python53 B
def builder(*, library: str | None = None) -> Builder

Parameters

ParameterTypeOwnershipDefaultMeaning
librarystr | NoneValueNoneLoaded native library used by this call.

Errors

StatusWhen
"out_of_memory"native memory is exhausted

Notes

Add nodes in depth-first preorder. An append failure makes the builder unusable.

Examples

builder

python257 B
async with (await stardust.builder()) as builder:
    object_node = (await builder.add(stardust.NodeInput(kind="object")))
    (await builder.add(stardust.NodeInput(parent=object_node, kind="number", key="x", number=41)))
    result = (await builder.seal())
Result / 16 B
{:value {:x 41}}

C exports: stardust_document_builder_create

Builder.add

python37 B
def add(self, node: NodeInput) -> int

Requires exclusive use of the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
selfBuilderBorrowed for the callRequiredThe builder owner for this operation. Keep it alive during the call.
nodeNodeInputValueRequiredThe next node's kind, parent and typed value.

Errors

StatusWhen
"invalid_argument"the node breaks the preorder rules or has an invalid kind or number
"closed"the builder is closed
"out_of_memory"native memory is exhausted

Notes

The root has no parent. Each later parent must be an open container or one of its ancestors.

Examples

builder/add

python257 B
async with (await stardust.builder()) as builder:
    object_node = (await builder.add(stardust.NodeInput(kind="object")))
    (await builder.add(stardust.NodeInput(parent=object_node, kind="number", key="x", number=41)))
    result = (await builder.seal())
Result / 16 B
{:value {:x 41}}

Add a typed integer node

python148 B
input = stardust.NodeInput(kind="number", number=41)
async with (await stardust.builder()) as builder:
    (await builder.add(input))
result = input
Result / 137 B
{:value {:parent  nil
         :kind    "number"
         :boolean false
         :number  41
         :key     ""
         :text    ""}}

C exports: stardust_document_builder_add_many, stardust_document_builder_error

Builder.add_many

python33 B
def add_many(self, nodes) -> None

Requires exclusive use of the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
selfBuilderBorrowed for the callRequiredThe builder owner for this operation. Keep it alive during the call.
nodesNode_InputsValueRequiredNodes in depth-first preorder to append together.

Errors

StatusWhen
"invalid_argument"a node breaks the preorder rules or has an invalid kind or number
"closed"the builder is closed
"out_of_memory"native memory is exhausted

Examples

builder/add_many

python239 B
async with (await stardust.builder()) as builder:
    (await builder.add_many([
        stardust.NodeInput(kind="object"),
        stardust.NodeInput(parent=0, kind="number", key="x", number=41),
    ]))
    result = (await builder.seal())
Result / 16 B
{:value {:x 41}}

C exports: stardust_document_builder_add_many, stardust_document_builder_error

Builder.seal

python26 B
def seal(self) -> Document

Requires exclusive use of the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
selfBuilderBorrowed for the callRequiredThe builder owner for this operation. Keep it alive during the call.

Errors

StatusWhen
"invalid_argument"the builder has no root or a container is still open
"closed"the builder is closed
"out_of_memory"native memory is exhausted

Notes

The builder accepts no more nodes after sealing.

Examples

builder/seal

python257 B
async with (await stardust.builder()) as builder:
    object_node = (await builder.add(stardust.NodeInput(kind="object")))
    (await builder.add(stardust.NodeInput(parent=object_node, kind="number", key="x", number=41)))
    result = (await builder.seal())
Result / 16 B
{:value {:x 41}}

C exports: stardust_document_builder_seal, stardust_document_builder_error

Builder.close

python23 B
def close(self) -> None

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
selfBuilderConsumed ownerRequiredThe builder owner to release. Do not use it after this call.

Examples

builder/close

input_text holds this document:

edn7 B
{:x 41}
python111 B
builder = (await stardust.builder())
(await builder.close())
result = (await stardust.parse(input_text, "edn"))
Result / 16 B
{:value {:x 41}}

C exports: stardust_document_builder_release