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}
python196 B
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"))))
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.
Document.rootnodedocument.root() returns the root node ID.
Document.countcountdocument.count() returns the number of nodes.
Document.readnode_viewdocument.read(id) returns a node view: kind, boolean, parent, position, first_child, next_sibling, key and text.
Document.read_manynode_viewsdocument.read_many(start, capacity) reads consecutive node views.
Document.membernodedocument.member(id, key) returns the first matching object member ID.
Document.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.
Document.childrennode_viewsdocument.children(id) iterates the node views of a node's children in order.
Document.membersnode_viewsdocument.members(id) iterates the members of an object node in order, each with its key.
Document.i64integerRead an exact signed integer.
Document.f64realRead a finite floating-point number.
Document.closenoneRelease the document handle.

validate_schema

python76 B
def validate_schema(bundle: Document, instance: Document, node: int) -> None

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.
nodeintValueRequiredThe node ID within instance to validate. Zero selects its root.

Errors

StatusWhen
"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

python180 B
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

python180 B
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

python178 B
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

python180 B
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

python84 B
def parse(text: str | bytes, format: str, *, library: str | None = None) -> Document

Parameters

ParameterTypeOwnershipDefaultMeaning
textstr | bytesValueRequiredEncoded input bytes in the selected format. TRON is binary.
formatstrValueRequiredThe codec to use for this operation.
librarystr | NoneValueNoneLoaded native library used by this call.

Errors

StatusWhen
"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:

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]
python56 B
result = (await stardust.parse(expression_text, "dust"))
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_text holds these bytes:

text1 B
]
python139 B
try:
    async with (await stardust.parse(malformed_text, "dust")):
        pass
except stardust.StardustError as error:
    result = error
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_text holds these bytes:

text1 B
[
python139 B
try:
    async with (await stardust.parse(malformed_text, "dust")):
        pass
except stardust.StardustError as error:
    result = error
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

Document.root

python21 B
def root(self) -> int

Parameters

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

Errors

StatusWhen
"closed"the document is closed

Examples

document/root

input_text holds this document:

dust5 B
x 41
python97 B
async with (await stardust.parse(input_text, "dust")) as input:
    result = (await input.root())
Result / 1 B
0

C exports: stardust_document_root

Document.count

python22 B
def count(self) -> int

Parameters

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

Errors

StatusWhen
"closed"the document is closed

Examples

document/count

input_text holds this document:

dust5 B
x 41
python98 B
async with (await stardust.parse(input_text, "dust")) as input:
    result = (await input.count())
Result / 1 B
2

C exports: stardust_document_count

Document.read

python35 B
def read(self, id: int) -> NodeView

Parameters

ParameterTypeOwnershipDefaultMeaning
selfDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idintValueRequiredA node ID within the receiver document.

Errors

StatusWhen
"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:

dust5 B
x 41
python117 B
async with (await stardust.parse(input_text, "dust")) as input:
    result = (await input.read((await input.root())))
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

Document.read_many

python64 B
def read_many(self, start: int, capacity: int) -> list[NodeView]

Parameters

ParameterTypeOwnershipDefaultMeaning
selfDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
startintValueRequiredThe first node ID of the consecutive read.
capacityintValueRequiredThe maximum number of node views to read. For output slices, the call uses the caller's slice length.

Errors

StatusWhen
"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:

dust5 B
x 41
python142 B
async with (await stardust.parse(input_text, "dust")) as input:
    views = (await input.read_many((await input.root()), 1))
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

Document.member

python42 B
def member(self, id: int, key: str) -> int

Parameters

ParameterTypeOwnershipDefaultMeaning
selfDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idintValueRequiredA node ID within the receiver document.
keystrValueRequiredThe object member name to find.

Errors

StatusWhen
"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:

dust5 B
x 41
python124 B
async with (await stardust.parse(input_text, "dust")) as input:
    result = (await input.member((await input.root()), "x"))
Result / 1 B
1

C exports: stardust_document_member

Document.pointer

python93 B
def pointer(self, id: int, path: str | list[str | int] | tuple[str | int, ...]) -> int | None

Parameters

ParameterTypeOwnershipDefaultMeaning
selfDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idintValueRequiredA node ID within the receiver document.
pathstr | list[str | int] | tuple[str | int, ...]ValueRequiredThe stored definition path.

Errors

StatusWhen
"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:

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]
python143 B
async with (await stardust.parse(expression_text, "dust")) as source:
    result = (await source.pointer((await source.root()), "/inputs/1/x"))
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

python70 B
def parse(text: str, *, library: str | None = None) -> list[str | int]

Parameters

ParameterTypeOwnershipDefaultMeaning
textstrValueRequiredEncoded input bytes in the selected format. TRON is binary.
librarystr | NoneValueNoneLoaded native library used by this call.

Errors

StatusWhen
"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

python54 B
result = (await stardust.pointer.parse("/inputs/1/x"))
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

python95 B
def format(path: list[str | int] | tuple[str | int, ...], *, library: str | None = None) -> str

Parameters

ParameterTypeOwnershipDefaultMeaning
pathlist[str | int] | tuple[str | int, ...]ValueRequiredThe database file path.
librarystr | NoneValueNoneLoaded native library used by this call.

Errors

StatusWhen
"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

python60 B
result = (await stardust.pointer.format(["inputs", 1, "x"]))
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

Document.children

python49 B
def children(self, id: int) -> Iterator[NodeView]

Parameters

ParameterTypeOwnershipDefaultMeaning
selfDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idintValueRequiredA node ID within the receiver document.

Errors

StatusWhen
"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:

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}
python142 B
async with (await stardust.parse(seed_text, "dust")) as source:
    result = sum([1 async for view in source.children((await source.root()))])
Result / 1 B
4

C exports: stardust_document_read_many

Document.members

python60 B
def members(self, id: int) -> Iterator[tuple[str, NodeView]]

Parameters

ParameterTypeOwnershipDefaultMeaning
selfDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idintValueRequiredA node ID within the receiver document.

Errors

StatusWhen
"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:

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}
python135 B
async with (await stardust.parse(seed_text, "dust")) as source:
    result, view = (await anext(source.members((await source.root()))))
Result / 12 B
{text #_ada}

C exports: stardust_document_read_many

Document.i64

python29 B
def i64(self, id: int) -> int

Parameters

ParameterTypeOwnershipDefaultMeaning
selfDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idintValueRequiredA node ID within the receiver document.

Errors

StatusWhen
"invalid_argument"the node is not an integer number
"closed"the document is closed

Examples

document/i64

input_text holds this document:

dust5 B
x 41
python153 B
async with (await stardust.parse(input_text, "dust")) as input:
    x = (await input.member((await input.root()), "x"))
    result = (await input.i64(x))
Result / 2 B
41

C exports: stardust_document_i64

Document.f64

python31 B
def f64(self, id: int) -> float

Parameters

ParameterTypeOwnershipDefaultMeaning
selfDocumentBorrowed for the callRequiredThe document owner for this operation. Keep it alive during the call.
idintValueRequiredA node ID within the receiver document.

Errors

StatusWhen
"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:

dust4 B
1.0
python120 B
async with (await stardust.parse(number_text, "dust")) as number:
    result = (await number.f64((await number.root())))
Result / 3 B
1.0

C exports: stardust_document_f64

Document.close

python23 B
def close(self) -> None

Consumes the receiver handle.

Parameters

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

Examples

document/close

expression_text holds this document:

dust9 B
[+ ?x 1]
python123 B
async with (await stardust.parse(expression_text, "dust")) as expression:
    result = (await expression.serialize("dust"))
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 are owned strings and remain valid after the document closes.

FieldTypeUnitAbsentMeaning
kindstrNone——
booleanboolNone—True only for true.
parentint | NoneNoneNone—
positionintCount—Index within the parent container.
first_childint | NoneNoneNone—
next_siblingint | NoneNoneNone—
keystrNone—Member key of an object member, else empty.
textstrNone—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
parentint | NoneNone—
kindstr——
booleanbool—Value of a boolean node.
numberint | float | NoneNoneValue of a number node.
keystr—Member key when the parent is an object.
textstr—Value of a string node.