Documents

The example parses the people document in your selected format. It then reads Ada's age through the document nodes. Typed reads give you these values without serializing the document.

Example

Read Ada's age

people_text holds this document:

dust232 B
#_ada {name Ada
       role engineer
       age  36}
#_bob {name   Bob
       role   engineer
       age    41
       mentor {#link #_ada}}
#_cy  {name Cy
       role designer
       age  29}
#_tx  {author smoke
       reason seed}
odin280 B
people := stardust.parse(people_text, .DUST) or_return
defer stardust.close(&people)

root := stardust.root(people) or_return
ada := stardust.member(people, root, "#_ada") or_return
age := stardust.member(people, ada, "age") or_return
result := stardust.i64(people, age) or_return
Result / 2 B
36

Numbers

A document number is an i64 integer or a finite f64 value. The number type defines both.

  • An integer read requires an integer node. A floating-point node can cause the read to fail.
  • Choose the typed read that matches the node that you read.

Operations

OperationReturnsSummary
validate_schemanonevalidate_schema(bundle, instance, node) validates a native subtree with JSON Schema Draft 7.
parsedocumentparse(text, format) returns an owned document.
rootnodedocument.root() returns the root node ID.
countcountdocument.count() returns the number of nodes.
readnode_viewdocument.read(id) returns a node view: kind, boolean, parent, position, first_child, next_sibling, key and text.
read_manynode_viewsdocument.read_many(start, capacity) reads consecutive node views.
membernodedocument.member(id, key) returns the first matching object member ID.
pointerfound_nodedocument.pointer(id, path) returns the node that a path array or JSON Pointer text selects from id, or no node when the path misses.
pointer_parsepathpointer.parse(text) returns the path array of JSON Pointer text.
pointer_formatnamepointer.format(path) returns the JSON Pointer text of a path array.
child_iteratornode_viewsdocument.children(id) iterates the node views of a node's children in order.
membersUnavailabledocument.members(id) iterates the members of an object node in order, each with its key.
i64integerRead an exact signed integer.
f64realRead a finite floating-point number.
closenoneRelease the document handle.

validate_schema

odin117 B
validate_schema :: proc(
	bundle, instance: Document,
	node: builtin.i64,
	allocator := context.allocator,
) -> Error

Parameters

ParameterTypeOwnershipDefaultMeaning
bundleDocumentBorrowed for the callRequiredThe document containing the root schema name, schema resources and optional schemaPath.
instanceDocumentBorrowed for the callRequiredThe native document containing the value to validate.
nodebuiltin.i64ValueRequiredThe node ID within instance to validate. Zero selects its root.
allocatormem.AllocatorValuecontext.allocatorAllocator for copied return values and diagnostics. The caller releases them with the same allocator.

Errors

StatusWhen
.Invalid_Argumentthe bundle, schemaPath or node is invalid or an input exceeds its limit
.Invalid_Definitiona schema cannot compile or a reference cannot resolve
.Schema_Violationthe selected instance does not match the schema
.Evaluation_Failedthe validation work limit is exceeded
.Out_Of_Memorynative memory is exhausted

Examples

Validate a document against an authored schema

odin231 B
bundle := stardust.parse(bundle_text, .DUST) or_return
defer stardust.close(&bundle)
instance := stardust.parse(instance_text, .DUST) or_return
defer stardust.close(&instance)
stardust.validate_schema(bundle, instance, 0) or_return

Validate a document against an authored schema

odin231 B
bundle := stardust.parse(bundle_text, .JSON) or_return
defer stardust.close(&bundle)
instance := stardust.parse(instance_text, .JSON) or_return
defer stardust.close(&instance)
stardust.validate_schema(bundle, instance, 0) or_return

Validate a document against an authored schema

odin229 B
bundle := stardust.parse(bundle_text, .EDN) or_return
defer stardust.close(&bundle)
instance := stardust.parse(instance_text, .EDN) or_return
defer stardust.close(&instance)
stardust.validate_schema(bundle, instance, 0) or_return

Validate a document against an authored schema

odin231 B
bundle := stardust.parse(bundle_text, .TRON) or_return
defer stardust.close(&bundle)
instance := stardust.parse(instance_text, .TRON) or_return
defer stardust.close(&instance)
stardust.validate_schema(bundle, instance, 0) or_return

C exports: stardust_schema_validate, stardust_result_error, stardust_result_release

parse

odin114 B
parse :: proc(
	text: string,
	format: Source_Format,
	allocator := context.allocator,
) -> (
	Document,
	Error,
)

Parameters

ParameterTypeOwnershipDefaultMeaning
textstringValueRequiredEncoded input bytes in the selected format. TRON is binary.
formatSource_FormatValueRequiredThe codec to use for this operation.
allocatormem.AllocatorValuecontext.allocatorAllocator for copied return values and diagnostics. The caller releases them with the same allocator.

Errors

StatusWhen
.Invalid_JSONJSON text is malformed
.Invalid_Argumentthe text is malformed, the format is unknown or a number is outside i64 or finite f64
.Out_Of_Memorynative memory is exhausted

Examples

Parse the shared DUST expression fixture

expression_text holds these bytes:

text278 B
// One expression built once and evaluated against several inputs. The
// evaluate examples load this file, build `expression`, evaluate it with each
// of `inputs` in order and compare each result with `expected`.
expression [+ ?x 1]
inputs     [{x 1} {x 41}]
expected   [2 42]
odin88 B
result := stardust.parse(expression_text, .DUST) or_return
defer stardust.close(&result)
Result / 293 B
{
  // One expression built once and evaluated against several inputs. The
  // evaluate examples load this file, build `expression`, evaluate it with each
  // of `inputs` in order and compare each result with `expected`.
  expression [+ ?x 1]
  inputs     [{x 1} {x 41}]
  expected   [2 42]}

Report a malformed byte at offset zero

malformed holds these bytes:

text1 B
]
odin110 B
failed, result := stardust.parse(malformed, .DUST)
defer stardust.close(&failed)
defer stardust.close(&result)
Result / 175 B
{violations null
 code       invalid_dust
 message    Invalid_Syntax at offset 0
 path       null
 field      null
 entity     null
 offset     0
 status     invalid_argument}

Report malformed DUST

malformed holds these bytes:

text1 B
[
odin110 B
failed, result := stardust.parse(malformed, .DUST)
defer stardust.close(&failed)
defer stardust.close(&result)
Result / 175 B
{violations null
 code       invalid_dust
 message    Unexpected_End at offset 2
 path       null
 field      null
 entity     null
 offset     2
 status     invalid_argument}

C exports: stardust_parse, stardust_document_error

root

odin56 B
root :: proc(document: Document) -> (builtin.i64, Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.

Errors

StatusWhen
.Closedthe document is closed

Examples

document/root

input_text holds this document:

dust5 B
x 41
odin131 B
source := stardust.parse(input_text, .DUST) or_return
defer stardust.close(&source)

result := i64(stardust.root(source) or_return)
Result / 1 B
0

C exports: stardust_document_root

count

odin57 B
count :: proc(document: Document) -> (builtin.i64, Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.

Errors

StatusWhen
.Closedthe document is closed

Examples

document/count

input_text holds this document:

dust5 B
x 41
odin132 B
source := stardust.parse(input_text, .DUST) or_return
defer stardust.close(&source)

result := i64(stardust.count(source) or_return)
Result / 1 B
2

C exports: stardust_document_count

read

odin71 B
read :: proc(document: Document, id: builtin.i64) -> (Node_View, Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idbuiltin.i64ValueRequiredA node ID within the receiver document.

Errors

StatusWhen
.Invalid_Argumentid is not a node of the document
.Closedthe document is closed

Notes

Text contains string values. Read numbers with i64 or f64.

Examples

document/read

input_text holds this document:

dust5 B
x 41
odin172 B
source := stardust.parse(input_text, .DUST) or_return
defer stardust.close(&source)

root := stardust.root(source) or_return
result := stardust.read(source, root) or_return
Result / 145 B
{kind         object
 boolean      false
 parent       null
 position     0
 first_child  1
 next_sibling null
 key          ""
 text         ""}

C exports: stardust_document_read_many

read_many

odin102 B
read_many :: proc(document: Document, start: builtin.i64, output: []Node_View) -> (builtin.i64, Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
startbuiltin.i64ValueRequiredThe first node ID of the consecutive read.
output[]Node_ViewValueRequiredThe maximum number of node views to read. For output slices, the call uses the caller's slice length.

Errors

StatusWhen
.Invalid_Argumentstart is past the last node or capacity is negative
.Closedthe document is closed

Examples

document/read_many

input_text holds this document:

dust5 B
x 41
odin229 B
source := stardust.parse(input_text, .DUST) or_return
defer stardust.close(&source)

root := stardust.root(source) or_return
views: [1]stardust.Node_View
_ = stardust.read_many(source, root, views[:]) or_return
result := views[0]
Result / 145 B
{kind         object
 boolean      false
 parent       null
 position     0
 first_child  1
 next_sibling null
 key          ""
 text         ""}

C exports: stardust_document_read_many

member

odin88 B
member :: proc(document: Document, id: builtin.i64, key: string) -> (builtin.i64, Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idbuiltin.i64ValueRequiredA node ID within the receiver document.
keystringValueRequiredThe object member name to find.

Errors

StatusWhen
.Not_Foundthe object has no member with the key
.Invalid_Argumentid is not an object node of the document
.Closedthe document is closed

Examples

document/member

input_text holds this document:

dust5 B
x 41
odin197 B
source := stardust.parse(input_text, .DUST) or_return
defer stardust.close(&source)

root := stardust.root(source) or_return
node := stardust.member(source, root, "x") or_return
result := i64(node)
Result / 1 B
1

C exports: stardust_document_member

pointer

odin162 B
pointer :: proc(
	document: Document,
	id: builtin.i64,
	path: []Token,
	allocator := context.allocator,
) -> (
	node: builtin.i64,
	found: bool,
	error: Error,
)
odin116 B
pointer :: proc(document: Document, id: builtin.i64, text: string) -> (node: builtin.i64, found: bool, error: Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idbuiltin.i64ValueRequiredA node ID within the receiver document.
path[]TokenValueRequiredThe stored definition path.
allocatormem.AllocatorValuecontext.allocatorAllocator for copied return values and diagnostics. The caller releases them with the same allocator.
textstringValueRequired—

Errors

StatusWhen
.Invalid_Pointerthe text is not a JSON Pointer or the array is not a canonical path array
.Invalid_Argumentid is not a node of the document
.Closedthe document is closed
.Out_Of_Memorynative memory is exhausted

Notes

A miss is not a failure: a missing key, an index past the end, a string token such as - on an array, or a step into a scalar. An integer token on an object selects the member named by its decimal text. Duplicate keys select the first member, as member does. A malformed path fails even after a step that misses.

Examples

document/pointer

expression_text holds these bytes:

text278 B
// One expression built once and evaluated against several inputs. The
// evaluate examples load this file, build `expression`, evaluate it with each
// of `inputs` in order and compare each result with `expected`.
expression [+ ?x 1]
inputs     [{x 1} {x 41}]
expected   [2 42]
odin216 B
source := stardust.parse(expression_text, .DUST) or_return
defer stardust.close(&source)

root := stardust.root(source) or_return
node, _ := stardust.pointer(source, root, "/inputs/1/x") or_return
result := i64(node)
Result / 1 B
9

C exports: stardust_document_pointer, stardust_document_pointer_text, stardust_document_builder_create, stardust_document_builder_add_many, stardust_document_builder_seal, stardust_document_builder_release, stardust_document_release

pointer_parse

odin100 B
pointer_parse :: proc(text: string, allocator := context.allocator) -> (path: []Token, error: Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
textstringValueRequiredEncoded input bytes in the selected format. TRON is binary.
allocatormem.AllocatorValuecontext.allocatorAllocator for copied return values and diagnostics. The caller releases them with the same allocator.

Errors

StatusWhen
.Invalid_Pointerthe text is not empty and does not start with /, or holds ~ not followed by 0 or 1
.Out_Of_Memorynative memory is exhausted

Notes

The empty text is the root path []. ~1 decodes to / and ~0 to ~. A canonical array index token becomes an integer, including a key such as 2024; every other token stays a string, such as 01 or -. No target document is needed.

Examples

pointer_parse

odin81 B
result := stardust.pointer_parse("/inputs/1/x", context.temp_allocator) or_return
Result / 12 B
[inputs 1 x]

C exports: stardust_pointer_parse, stardust_document_error, stardust_document_read_many, stardust_document_i64, stardust_document_release

pointer_format

odin103 B
pointer_format :: proc(path: []Token, allocator := context.allocator) -> (output: string, error: Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
path[]TokenValueRequiredThe database file path.
allocatormem.AllocatorValuecontext.allocatorAllocator for copied return values and diagnostics. The caller releases them with the same allocator.

Errors

StatusWhen
.Invalid_Pointera token is a negative or non-integral number, an index-shaped string such as "0", or neither a number nor a string
.Out_Of_Memorynative memory is exhausted

Notes

The root path [] formats as empty text. / encodes as ~1 and ~ as ~0. pointer.parse of the result returns the same path.

Examples

pointer_format

odin87 B
result := stardust.pointer_format({"inputs", 1, "x"}, context.temp_allocator) or_return
Result / 18 B
{text /inputs/1/x}

C exports: stardust_pointer_format, stardust_document_builder_create, stardust_document_builder_add_many, stardust_document_builder_seal, stardust_document_builder_release, stardust_document_release, stardust_text_data, stardust_text_error, stardust_text_release

child_iterator

odin72 B
child_iterator :: proc(doc: Document, id: builtin.i64) -> Child_Iterator

Parameters

ParameterTypeOwnershipDefaultMeaning
docDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idbuiltin.i64ValueRequiredA node ID within the receiver document.

Errors

StatusWhen
.Invalid_Argumentid is not a node of the document
.Closedthe document is closed

Notes

Keep the document open during iteration. A scalar has no children. A read failure is reported once and ends iteration.

Examples

Iterate the children of a node

seed_text holds this document:

dust232 B
#_ada {name Ada
       role engineer
       age  36}
#_bob {name   Bob
       role   engineer
       age    41
       mentor {#link #_ada}}
#_cy  {name Cy
       role designer
       age  29}
#_tx  {author smoke
       reason seed}
odin291 B
seed := stardust.parse(seed_text, .DUST) or_return
defer stardust.close(&seed)

root := stardust.root(seed) or_return
it := stardust.child_iterator(seed, root)
result: i64 = 0
for view in stardust.iterate_children(&it) {
	if len(view.key) > 0 {result += 1}
}
if it.err != nil {return it.err}
Result / 1 B
4

C exports: stardust_document_read_many

members

child_iterator views carry each member key. Odin has no separate members iterator.

C exports: stardust_document_read_many

i64

odin72 B
i64 :: proc(document: Document, id: builtin.i64) -> (builtin.i64, Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idbuiltin.i64ValueRequiredA node ID within the receiver document.

Errors

StatusWhen
.Invalid_Argumentthe node is not an integer number
.Closedthe document is closed

Examples

document/i64

input_text holds this document:

dust5 B
x 41
odin224 B
source := stardust.parse(input_text, .DUST) or_return
defer stardust.close(&source)

root := stardust.root(source) or_return
node := stardust.member(source, root, "x") or_return
result := stardust.i64(source, node) or_return
Result / 2 B
41

C exports: stardust_document_i64

f64

odin72 B
f64 :: proc(document: Document, id: builtin.i64) -> (builtin.f64, Error)

Parameters

ParameterTypeOwnershipDefaultMeaning
documentDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idbuiltin.i64ValueRequiredA node ID within the receiver document.

Errors

StatusWhen
.Invalid_Argumentthe node is not a number or its value overflows f64
.Closedthe document is closed

Notes

Integer conversion can round values outside the exact f64 integer range.

Examples

document/f64

float_text holds this document:

dust4 B
1.0
odin158 B
source := stardust.parse(float_text, .DUST) or_return
defer stardust.close(&source)

result := stardust.f64(source, stardust.root(source) or_return) or_return
Result / 3 B
1.0

C exports: stardust_document_f64

close

odin34 B
close :: proc(document: ^Document)

Consumes the receiver handle.

Parameters

ParameterTypeOwnershipDefaultMeaning
document^DocumentConsumed ownerRequiredThe document owner to release. Do not use it after this call.

Examples

document/close

expression_text holds this document:

dust9 B
[+ ?x 1]
odin189 B
source := stardust.parse(expression_text, .DUST) or_return
defer stardust.close(&source)

result := stardust.serialize(source, .DUST) or_return
defer delete(result)

stardust.close(&source)
Result / 33 B
{
  text '''
    [+ ?x 1]
  '''
}

C exports: stardust_document_release

node_view

This record contains the result of read and read_many. The key and text fields borrow the document until it closes.

FieldTypeUnitAbsentMeaning
kindValue_KindNone——
booleanboolNone—True only for true.
parentbuiltin.i64NoneNO_NODE—
positionbuiltin.i64Count—Index within the parent container.
first_childbuiltin.i64NoneNO_NODE—
next_siblingbuiltin.i64NoneNO_NODE—
keystringNone—Member key of an object member, else empty.
textstringNone—Value of a string, else empty. It can contain NUL.

node_input

This record supplies a node for add and add_many. The number field holds a typed i64 or f64 value. Go uses I64/F64, Rust and Odin use Number, Clojure uses long or double, and Python uses int or float. This value sets the ABI number kind and the integer or real field.

FieldTypeAbsentMeaning
parentbuiltin.i64NO_NODE—
kindValue_Kind——
booleanbool—Value of a boolean node.
numberNumbernilValue of a number node.
keystring—Member key when the parent is an object.
textstring—Value of a string node.