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
stardust_document_builder_createStatus. builder through outputCreate an owned document builder.
addUnavailablebuilder.add(node) appends one node and returns its preorder ID.
stardust_document_builder_add_manyStatus. none through outputbuilder.add_many(nodes) appends nodes in order in one native call.
stardust_document_builder_sealStatus. document through outputbuilder.seal() transfers the constructed document into an independent owned document.
stardust_document_builder_releasevoidRelease the builder handle.

stardust_document_builder_create

c78 B
uint32_t stardust_document_builder_create(stardust_document_builder **output);

Parameters

ParameterTypeOwnershipDefaultMeaning
outputstardust_document_builder **Owned outputRequiredReceives a library-owned handle. Release it after use.

Errors

StatusWhen
STARDUST_OUT_OF_MEMORYnative memory is exhausted
STARDUST_INVALID_ARGUMENTa required input or output pointer is NULL, or an input owner is not ready

Notes

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

Examples

builder

c692 B
stardust_document_builder *builder = NULL;
stardust_document *result = NULL;
uint32_t status = stardust_document_builder_create(&builder);
stardust_node_input nodes[] = {
    {.parent = STARDUST_DOCUMENT_NO_NODE, .kind = STARDUST_OBJECT},
    {.parent = 0, .kind = STARDUST_NUMBER, .key = {(const uint8_t *)"x", 1},
     .number = STARDUST_NUMBER_INTEGER, .integer = 41},
};
if (status == STARDUST_OK)
    status = stardust_document_builder_add_many(builder, nodes, 2);
if (status == STARDUST_OK)
    status = stardust_document_builder_seal(builder, &result);
stardust_document_builder_release(builder);
if (status != STARDUST_OK) {
    stardust_document_release(result);
    return status;
}
Result / 16 B
{:value {:x 41}}

add

Call stardust_document_builder_add_many with one node.

Examples

Add a typed integer node

c452 B
stardust_node_input input = {.parent=STARDUST_DOCUMENT_NO_NODE, .kind=STARDUST_NUMBER, .number=STARDUST_NUMBER_INTEGER, .integer=41};
stardust_document_builder *builder = NULL;
uint32_t status = stardust_document_builder_create(&builder);
if (status == STARDUST_OK) status = stardust_document_builder_add_many(builder, &input, 1);
stardust_document_builder_release(builder);
if (status != STARDUST_OK) return status;
stardust_node_input result = input;
Result / 137 B
{:value {:parent  nil
         :kind    "number"
         :boolean false
         :number  41
         :key     ""
         :text    ""}}

Also calls: stardust_document_builder_add_many, stardust_document_builder_error

stardust_document_builder_add_many

c129 B
uint32_t stardust_document_builder_add_many(stardust_document_builder *builder, const stardust_node_input *nodes, int64_t count);
c117 B
uint32_t stardust_document_builder_error(const stardust_document_builder *builder, stardust_diagnostic_view *output);

Requires exclusive use of the receiver handle.

Parameters

stardust_document_builder_add_many

ParameterTypeOwnershipDefaultMeaning
builderstardust_document_builder *Borrowed handleRequiredKeep the handle alive until the call returns.
nodesconst stardust_node_input *Borrowed arrayNULLNodes in depth-first preorder to append together.
countint64_tValueRequiredValue passed to the call.

stardust_document_builder_error

ParameterTypeOwnershipDefaultMeaning
builderconst stardust_document_builder *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_diagnostic_view *Caller-owned outputRequiredReceives a record. Its pointer fields follow the record's ownership contract.

Errors

StatusWhen
STARDUST_INVALID_ARGUMENTa node breaks the preorder rules or has an invalid kind or number
STARDUST_OUT_OF_MEMORYnative memory is exhausted

Examples

builder/add_many

c692 B
stardust_document_builder *builder = NULL;
stardust_document *result = NULL;
uint32_t status = stardust_document_builder_create(&builder);
stardust_node_input nodes[] = {
    {.parent = STARDUST_DOCUMENT_NO_NODE, .kind = STARDUST_OBJECT},
    {.parent = 0, .kind = STARDUST_NUMBER, .key = {(const uint8_t *)"x", 1},
     .number = STARDUST_NUMBER_INTEGER, .integer = 41},
};
if (status == STARDUST_OK)
    status = stardust_document_builder_add_many(builder, nodes, 2);
if (status == STARDUST_OK)
    status = stardust_document_builder_seal(builder, &result);
stardust_document_builder_release(builder);
if (status != STARDUST_OK) {
    stardust_document_release(result);
    return status;
}
Result / 16 B
{:value {:x 41}}

builder/add_many

c692 B
stardust_document_builder *builder = NULL;
stardust_document *result = NULL;
uint32_t status = stardust_document_builder_create(&builder);
stardust_node_input nodes[] = {
    {.parent = STARDUST_DOCUMENT_NO_NODE, .kind = STARDUST_OBJECT},
    {.parent = 0, .kind = STARDUST_NUMBER, .key = {(const uint8_t *)"x", 1},
     .number = STARDUST_NUMBER_INTEGER, .integer = 41},
};
if (status == STARDUST_OK)
    status = stardust_document_builder_add_many(builder, nodes, 2);
if (status == STARDUST_OK)
    status = stardust_document_builder_seal(builder, &result);
stardust_document_builder_release(builder);
if (status != STARDUST_OK) {
    stardust_document_release(result);
    return status;
}
Result / 16 B
{:value {:x 41}}

Also calls: stardust_document_builder_error

stardust_document_builder_seal

c104 B
uint32_t stardust_document_builder_seal(stardust_document_builder *builder, stardust_document **output);
c117 B
uint32_t stardust_document_builder_error(const stardust_document_builder *builder, stardust_diagnostic_view *output);

Requires exclusive use of the receiver handle.

Parameters

stardust_document_builder_seal

ParameterTypeOwnershipDefaultMeaning
builderstardust_document_builder *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_document **Owned outputRequiredReceives a library-owned handle. Release it after use.

stardust_document_builder_error

ParameterTypeOwnershipDefaultMeaning
builderconst stardust_document_builder *Borrowed handleRequiredKeep the handle alive until the call returns.
outputstardust_diagnostic_view *Caller-owned outputRequiredReceives a record. Its pointer fields follow the record's ownership contract.

Errors

StatusWhen
STARDUST_INVALID_ARGUMENTthe builder has no root or a container is still open
STARDUST_OUT_OF_MEMORYnative memory is exhausted

Notes

The builder accepts no more nodes after sealing.

Examples

builder/seal

c692 B
stardust_document_builder *builder = NULL;
stardust_document *result = NULL;
uint32_t status = stardust_document_builder_create(&builder);
stardust_node_input nodes[] = {
    {.parent = STARDUST_DOCUMENT_NO_NODE, .kind = STARDUST_OBJECT},
    {.parent = 0, .kind = STARDUST_NUMBER, .key = {(const uint8_t *)"x", 1},
     .number = STARDUST_NUMBER_INTEGER, .integer = 41},
};
if (status == STARDUST_OK)
    status = stardust_document_builder_add_many(builder, nodes, 2);
if (status == STARDUST_OK)
    status = stardust_document_builder_seal(builder, &result);
stardust_document_builder_release(builder);
if (status != STARDUST_OK) {
    stardust_document_release(result);
    return status;
}
Result / 16 B
{:value {:x 41}}

Also calls: stardust_document_builder_error

stardust_document_builder_release

c75 B
void stardust_document_builder_release(stardust_document_builder *builder);

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
builderstardust_document_builder *Consumed handleNULLEnds ownership. Do not use the handle after this call.

Examples

builder/close

input_text holds this document:

edn7 B
{:x 41}
c363 B
stardust_document_builder *builder = NULL;
uint32_t status = stardust_document_builder_create(&builder);
stardust_document_builder_release(builder);
if (status != STARDUST_OK) return status;
stardust_document *result = NULL;
status = stardust_parse(input_text, STARDUST_EDN, &result);
if (status != STARDUST_OK) {stardust_document_release(result); return status;}
Result / 16 B
{:value {:x 41}}