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:
#_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}
async with (await stardust.parse(people_text, "dust")) as people:
ada = (await people.member((await people.root()), "#_ada"))
result = (await people.i64((await people.member(ada, "age"))))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
| Operation | Returns | Summary |
|---|---|---|
| validate_schema | none | validate_schema(bundle, instance, node) validates a native subtree with JSON Schema Draft 7. |
| parse | document | parse(text, format) returns an owned document. |
| Document.root | node | document.root() returns the root node ID. |
| Document.count | count | document.count() returns the number of nodes. |
| Document.read | node_view | document.read(id) returns a node view: kind, boolean, parent, position, first_child, next_sibling, key and text. |
| Document.read_many | node_views | document.read_many(start, capacity) reads consecutive node views. |
| Document.member | node | document.member(id, key) returns the first matching object member ID. |
| Document.pointer | found_node | document.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.parse | path | pointer.parse(text) returns the path array of JSON Pointer text. |
| pointer.format | name | pointer.format(path) returns the JSON Pointer text of a path array. |
| Document.children | node_views | document.children(id) iterates the node views of a node's children in order. |
| Document.members | node_views | document.members(id) iterates the members of an object node in order, each with its key. |
| Document.i64 | integer | Read an exact signed integer. |
| Document.f64 | real | Read a finite floating-point number. |
| Document.close | none | Release the document handle. |
validate_schema
def validate_schema(bundle: Document, instance: Document, node: int) -> NoneParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
bundle | Document | Borrowed for the call | Required | The document containing the root schema name, schema resources and optional schemaPath. |
instance | Document | Borrowed for the call | Required | The native document containing the value to validate. |
node | int | Value | Required | The node ID within instance to validate. Zero selects its root. |
Errors
| Status | When |
|---|---|
"invalid_argument" | the bundle, schemaPath or node is invalid or an input exceeds its limit |
"invalid_definition" | a schema cannot compile or a reference cannot resolve |
"schema_violation" | the selected instance does not match the schema |
"evaluation_failed" | the validation work limit is exceeded |
"out_of_memory" | native memory is exhausted |
Examples
Validate a document against an authored schema
async with (await stardust.parse(bundle_text, "dust")) as bundle, (await stardust.parse(instance_text, "dust")) as instance:
await stardust.validate_schema(bundle, instance, 0)Validate a document against an authored schema
async with (await stardust.parse(bundle_text, "json")) as bundle, (await stardust.parse(instance_text, "json")) as instance:
await stardust.validate_schema(bundle, instance, 0)Validate a document against an authored schema
async with (await stardust.parse(bundle_text, "edn")) as bundle, (await stardust.parse(instance_text, "edn")) as instance:
await stardust.validate_schema(bundle, instance, 0)Validate a document against an authored schema
async with (await stardust.parse(bundle_text, "tron")) as bundle, (await stardust.parse(instance_text, "tron")) as instance:
await stardust.validate_schema(bundle, instance, 0)C exports: stardust_schema_validate, stardust_result_error, stardust_result_release
parse
def parse(text: str | bytes, format: str, *, library: str | None = None) -> DocumentParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
text | str | bytes | Value | Required | Encoded input bytes in the selected format. TRON is binary. |
format | str | Value | Required | The codec to use for this operation. |
library | str | None | Value | None | Loaded native library used by this call. |
Errors
| Status | When |
|---|---|
"invalid_json" | JSON text is malformed |
"invalid_argument" | the text is malformed, the format is unknown or a number is outside i64 or finite f64 |
"out_of_memory" | native memory is exhausted |
Examples
Parse the shared DUST expression fixture
expression_text holds these bytes:
// 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]result = (await stardust.parse(expression_text, "dust")){
// 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_text holds these bytes:
]
try:
async with (await stardust.parse(malformed_text, "dust")):
pass
except stardust.StardustError as error:
result = error{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_text holds these bytes:
[
try:
async with (await stardust.parse(malformed_text, "dust")):
pass
except stardust.StardustError as error:
result = error{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
Document.root
def root(self) -> intParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
Errors
| Status | When |
|---|---|
"closed" | the document is closed |
Examples
document/root
input_text holds this document:
x 41
async with (await stardust.parse(input_text, "dust")) as input:
result = (await input.root())0
C exports: stardust_document_root
Document.count
def count(self) -> intParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
Errors
| Status | When |
|---|---|
"closed" | the document is closed |
Examples
document/count
input_text holds this document:
x 41
async with (await stardust.parse(input_text, "dust")) as input:
result = (await input.count())2
C exports: stardust_document_count
Document.read
def read(self, id: int) -> NodeViewParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | int | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
"invalid_argument" | id is not a node of the document |
"closed" | the document is closed |
Notes
Text contains string values. Read numbers with i64 or f64.
Examples
document/read
input_text holds this document:
x 41
async with (await stardust.parse(input_text, "dust")) as input:
result = (await input.read((await input.root()))){kind object
boolean false
parent null
position 0
first_child 1
next_sibling null
key ""
text ""}C exports: stardust_document_read_many
Document.read_many
def read_many(self, start: int, capacity: int) -> list[NodeView]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
start | int | Value | Required | The first node ID of the consecutive read. |
capacity | int | Value | Required | The maximum number of node views to read. For output slices, the call uses the caller's slice length. |
Errors
| Status | When |
|---|---|
"invalid_argument" | start is past the last node or capacity is negative |
"closed" | the document is closed |
Examples
document/read_many
input_text holds this document:
x 41
async with (await stardust.parse(input_text, "dust")) as input:
views = (await input.read_many((await input.root()), 1))
result = views[0]{kind object
boolean false
parent null
position 0
first_child 1
next_sibling null
key ""
text ""}C exports: stardust_document_read_many
Document.member
def member(self, id: int, key: str) -> intParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | int | Value | Required | A node ID within the receiver document. |
key | str | Value | Required | The object member name to find. |
Errors
| Status | When |
|---|---|
"not_found" | the object has no member with the key |
"invalid_argument" | id is not an object node of the document |
"closed" | the document is closed |
Examples
document/member
input_text holds this document:
x 41
async with (await stardust.parse(input_text, "dust")) as input:
result = (await input.member((await input.root()), "x"))1
C exports: stardust_document_member
Document.pointer
def pointer(self, id: int, path: str | list[str | int] | tuple[str | int, ...]) -> int | NoneParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | int | Value | Required | A node ID within the receiver document. |
path | str | list[str | int] | tuple[str | int, ...] | Value | Required | The stored definition path. |
Errors
| Status | When |
|---|---|
"invalid_pointer" | the text is not a JSON Pointer or the array is not a canonical path array |
"invalid_argument" | id is not a node of the document |
"closed" | the document is closed |
"out_of_memory" | native memory is exhausted |
"invalid_argument" | the path is not a str, list or tuple, or a token is neither a str nor an int |
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:
// 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]async with (await stardust.parse(expression_text, "dust")) as source:
result = (await source.pointer((await source.root()), "/inputs/1/x"))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
def parse(text: str, *, library: str | None = None) -> list[str | int]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
text | str | Value | Required | Encoded input bytes in the selected format. TRON is binary. |
library | str | None | Value | None | Loaded native library used by this call. |
Errors
| Status | When |
|---|---|
"invalid_pointer" | the text is not empty and does not start with /, or holds ~ not followed by 0 or 1 |
"out_of_memory" | native 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
result = (await stardust.pointer.parse("/inputs/1/x"))[inputs 1 x]
C exports: stardust_pointer_parse, stardust_document_error, stardust_document_read_many, stardust_document_i64, stardust_document_release
pointer.format
def format(path: list[str | int] | tuple[str | int, ...], *, library: str | None = None) -> strParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
path | list[str | int] | tuple[str | int, ...] | Value | Required | The database file path. |
library | str | None | Value | None | Loaded native library used by this call. |
Errors
| Status | When |
|---|---|
"invalid_pointer" | a token is a negative or non-integral number, an index-shaped string such as "0", or neither a number nor a string |
"out_of_memory" | native memory is exhausted |
"invalid_argument" | the path is not a list or tuple, or a token is neither a str nor an int |
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
result = (await stardust.pointer.format(["inputs", 1, "x"])){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
Document.children
def children(self, id: int) -> Iterator[NodeView]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | int | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
"invalid_argument" | id is not a node of the document |
"closed" | the 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
document/children
seed_text holds this document:
#_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}
async with (await stardust.parse(seed_text, "dust")) as source:
result = sum([1 async for view in source.children((await source.root()))])4
C exports: stardust_document_read_many
Document.members
def members(self, id: int) -> Iterator[tuple[str, NodeView]]Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | int | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
"invalid_argument" | id is not an object node of the document |
"closed" | the document is closed |
Notes
Duplicate keys appear in document order. Keep the document open during iteration. A read failure is reported once and ends iteration.
Examples
document/members
seed_text holds this document:
#_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}
async with (await stardust.parse(seed_text, "dust")) as source:
result, view = (await anext(source.members((await source.root())))){text #_ada}C exports: stardust_document_read_many
Document.i64
def i64(self, id: int) -> intParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | int | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
"invalid_argument" | the node is not an integer number |
"closed" | the document is closed |
Examples
document/i64
input_text holds this document:
x 41
async with (await stardust.parse(input_text, "dust")) as input:
x = (await input.member((await input.root()), "x"))
result = (await input.i64(x))41
C exports: stardust_document_i64
Document.f64
def f64(self, id: int) -> floatParameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Borrowed for the call | Required | The document owner for this operation. Keep it alive during the call. |
id | int | Value | Required | A node ID within the receiver document. |
Errors
| Status | When |
|---|---|
"invalid_argument" | the node is not a number or its value overflows f64 |
"closed" | the document is closed |
Notes
Integer conversion can round values outside the exact f64 integer range.
Examples
document/f64
number_text holds this document:
1.0
async with (await stardust.parse(number_text, "dust")) as number:
result = (await number.f64((await number.root())))1.0
C exports: stardust_document_f64
Document.close
def close(self) -> NoneConsumes the receiver handle.
Parameters
| Parameter | Type | Ownership | Default | Meaning |
|---|---|---|---|---|
self | Document | Consumed owner | Required | The document owner to release. Do not use it after this call. |
Examples
document/close
expression_text holds this document:
[+ ?x 1]
async with (await stardust.parse(expression_text, "dust")) as expression:
result = (await expression.serialize("dust")){
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 are owned strings and remain valid after the document closes.
| Field | Type | Unit | Absent | Meaning |
|---|---|---|---|---|
kind | str | None | — | — |
boolean | bool | None | — | True only for true. |
parent | int | None | None | None | — |
position | int | Count | — | Index within the parent container. |
first_child | int | None | None | None | — |
next_sibling | int | None | None | None | — |
key | str | None | — | Member key of an object member, else empty. |
text | str | None | — | 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.
| Field | Type | Absent | Meaning |
|---|---|---|---|
parent | int | None | None | — |
kind | str | — | — |
boolean | bool | — | Value of a boolean node. |
number | int | float | None | None | Value of a number node. |
key | str | — | Member key when the parent is an object. |
text | str | — | Value of a string node. |