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;
}
TRON33 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
041C 78txt "x"
0602 29000000000000 00i64 41
0F0F 0A 04000000 0600 0000map leaf, size 10: @4 → @6

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;
TRON158 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
043C 6B6579txt "key"
080Ctxt ""
090F 0A 04000000 0800 0000map leaf, size 10: @4 → @8
134C 6B696E64txt "kind"
186C 6E756D626572txt "number"
1F0F 0A 13000000 1800 0000map leaf, size 10: @13 → @18
296C 6E756D626572txt "number"
3002 29000000000000 00i64 41
390F 0A 29000000 3000 0000map leaf, size 10: @29 → @30
436C 706172656E74txt "parent"
4A00nil
4B0F 0A 43000000 4A00 0000map leaf, size 10: @43 → @4A
554C 74657874txt "text"
5A0Ctxt ""
5B0F 0A 55000000 5A00 0000map leaf, size 10: @55 → @5A
657C 626F6F6C65616Etxt "boolean"
6D01bit false
6E0F 0A 65000000 6D00 0000map leaf, size 10: @65 → @6D
7807 1E A1190000 0900 0000 1F000000 3900 0000 4B000000 5B00 0000 6E000000map branch, size 30, depth 0 (root), slots 0, 5, 7, 8, 11, 12: @9 → @1F → @39 → @4B → @5B → @6E

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;
}
TRON33 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
041C 78txt "x"
0602 29000000000000 00i64 41
0F0F 0A 04000000 0600 0000map leaf, size 10: @4 → @6

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;
}
TRON33 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
041C 78txt "x"
0602 29000000000000 00i64 41
0F0F 0A 04000000 0600 0000map leaf, size 10: @4 → @6

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;
}
TRON33 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
041C 78txt "x"
0602 29000000000000 00i64 41
0F0F 0A 04000000 0600 0000map leaf, size 10: @4 → @6

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:

TRON33 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
041C 78txt "x"
0602 29000000000000 00i64 41
0F0F 0A 04000000 0600 0000map leaf, size 10: @4 → @6
c364 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_TRON, &result);
if (status != STARDUST_OK) {stardust_document_release(result); return status;}
TRON33 B
Offset (hex)Bytes (hex)Meaning
0054 52 4F 4Emagic TRON
041C 78txt "x"
0602 29000000000000 00i64 41
0F0F 0A 04000000 0600 0000map leaf, size 10: @4 → @6