silk.schema.shape.

One description of a Schema type's wire form, for everything that needs to know it.

When to use#

Call silk.schema.schema.Schemas.shape<T>() to get the Shapes of T: what kind of value it is on the wire, its fields with their wire names, doc comments and constraints, and its variants. silk.schema.json_schema.JsonSchemas renders it as JSON Schema; command line help and AI tool definitions read the same description.

Details#

A shape is derived from the type, its Spec and its doc comments, never written by hand. Its parts are nodes of one table, and a part refers to another by ShapeRef, so a recursive type such as a tree whose children are trees is one named node that refers to itself. Every struct and union is one node named by its type, so a type used twice is described once.

The text in a shape (names, docs, expected text) is static: building one only links nodes.

Import as Shapes with import silk.schema.shape { Shapes }.

Public declarations: 10.

Bound#

silk
pub union Bound

One end of a numeric range.

None#

silk
None

No limit.

Signed#

silk
Signed

A signed integer limit.

Field value#

silk
pub value: i64

The limit.

Unsigned#

silk
Unsigned

An unsigned integer limit.

Field value#

silk
pub value: u64

The limit.

Float#

silk
Float

A floating-point limit.

Field value#

silk
pub value: f64

The limit.

Implementation Copy for Bound#

silk
impl Copy for Bound

Constraint#

silk
pub union Constraint

A rule a value must follow beyond its type, declared in a Spec or given by a codec.

Details#

Every limit is inclusive. Lengths count characters, as JSON Schema's minLength and maxLength do, and usize.MAX means no upper limit.

Range#

silk
Range

A number between min and max.

Field min#

silk
pub min: Bound

The smallest value.

Field max#

silk
pub max: Bound

The largest value.

Length#

silk
Length

Text of min to max characters.

Field min#

silk
pub min: usize

The fewest characters.

Field max#

silk
pub max: usize

The most characters.

Items#

silk
Items

A list of min to max elements.

Field min#

silk
pub min: usize

The fewest elements.

Field max#

silk
pub max: usize

The most elements.

Pattern#

silk
Pattern

Text that a regular expression in silk.text.regex syntax matches.

Field source#

silk
pub source: string<'static>

The expression, which JSON Schema shares, since both use JavaScript syntax.

Expected#

silk
Expected

What a person reads when the value fails, instead of the generated text.

Field text#

silk
pub text: string<'static>

The text, such as "at most an hour".

Implementation Copy for Constraint#

silk
impl Copy for Constraint

Literal#

silk
pub union Literal

A value a static Spec can hold, such as a field's default.

Boolean#

silk
Boolean

true or false.

Field value#

silk
pub value: bool

The value.

Integer#

silk
Integer

An integer.

Field value#

silk
pub value: i64

The value.

Number#

silk
Number

A number.

Field value#

silk
pub value: f64

The value.

Text#

silk
Text

Text, or the name of a unit variant.

Field value#

silk
pub value: string<'static>

The value.

Implementation Copy for Literal#

silk
impl Copy for Literal

ShapeRef#

silk
pub struct ShapeRef

A link to one node of a Shapes table.

Field index#

silk
pub index: usize

The node's position in the table.

Implementation Copy for ShapeRef#

silk
impl Copy for ShapeRef

ShapeOf#

silk
pub struct ShapeOf<T>

The node of a type T in a Shapes table, as silk.schema.schema.Schema.shape returns it.

Details#

The type parameter says whose shape it is, so a call of Schema.shape names the type it wants by the result it expects. at is the node itself.

Field at#

silk
pub at: ShapeRef

The node.

Implementation ShapeOf<T>#

silk
impl<T> ShapeOf<T>

Associated function make#

silk
pub fn make(at: ShapeRef) -> ShapeOf<T>

Returns at as the node of T.

TypeInfo#

silk
pub struct TypeInfo

The name and doc comment of a named type.

Field name#

silk
pub name: string<'static>

The type's name with its type arguments, such as Server or Page<String>.

Field doc#

silk
pub doc: string<'static>

The type's /// comment, without slashes, or empty text.

Implementation Copy for TypeInfo#

silk
impl Copy for TypeInfo

FieldShape#

silk
pub struct FieldShape

One field of a struct or of a union variant.

Field name#

silk
pub name: string<'static>

The field's name in Silk.

Field wire#

silk
pub wire: string<'static>

The field's name in the data format.

Field doc#

silk
pub doc: string<'static>

The field's /// comment, without slashes, or empty text.

Field value#

silk
pub value: ShapeRef

The shape of the field's value.

Field required#

silk
pub required: bool

Whether the field must be present: false for an Option field and a field with a default.

Field default#

silk
pub default: Option<Literal>

The value of an absent field, from Spec.defaultValue.

Field constraints#

silk
pub constraints: List<Constraint>

The rules the field's Spec declares.

VariantShape#

silk
pub struct VariantShape

One variant of a union.

Field name#

silk
pub name: string<'static>

The variant's name in Silk.

Field wire#

silk
pub wire: string<'static>

The variant's name in the data format.

Field doc#

silk
pub doc: string<'static>

The variant's /// comment, without slashes, or empty text.

Field fields#

silk
pub fields: List<FieldShape>

The variant's fields; empty for a unit variant.

Shape#

silk
pub union Shape

The wire form of one type.

Boolean#

silk
Boolean

true or false.

Integer#

silk
Integer

An integer between min and max, the limits of its type.

Field min#

silk
pub min: Bound

The smallest value the type holds.

Field max#

silk
pub max: Bound

The largest value the type holds.

Float#

silk
Float

A number.

Text#

silk
Text

Text.

Optional#

silk
Optional

A value that may be absent or null, as Option is.

Field value#

silk
pub value: ShapeRef

The value when present.

List#

silk
List

A list.

Field element#

silk
pub element: ShapeRef

Each element.

Object#

silk
Object

A struct: an object with one member per field.

Field info#

silk
pub info: TypeInfo

The struct's name and doc comment.

Field fields#

silk
pub fields: List<FieldShape>

The fields, in declaration order.

Field strict#

silk
pub strict: bool

Whether keys that no field names are rejected, from Spec.strict.

Choice#

silk
Choice

A union whose variants have no fields: the name of one variant.

Field info#

silk
pub info: TypeInfo

The union's name and doc comment.

Field variants#

silk
pub variants: List<VariantShape>

The variants, in declaration order.

Tagged#

silk
Tagged

A union with fields: an object whose tag member names the variant and whose other members are that variant's fields.

Field info#

silk
pub info: TypeInfo

The union's name and doc comment.

Field tag#

silk
pub tag: string<'static>

The member that names the variant, type unless Spec.tag picks another.

Field variants#

silk
pub variants: List<VariantShape>

The variants, in declaration order.

Custom#

silk
Custom

A type with its own read, such as a port written as text: its wire form wire, plus the rules its codec reports.

Field wire#

silk
pub wire: ShapeRef

The shape of the wire form.

Field constraints#

silk
pub constraints: List<Constraint>

The rules about the wire value itself.

Field notes#

silk
pub notes: List<string<'static>>

What else the codec expects, as text for a person, such as "a port from 1 to 65535".

Pending#

silk
Pending

A node whose type is still being described; only seen while a recursive type is built.

Shapes#

silk
pub struct Shapes

The shape of one type, and of every type it uses, as a table of nodes.

When to use#

Get one from Schemas.shape<T>() and walk it from Shapes.root with Shapes.get.

Implementation Shapes#

silk
impl Shapes

Associated function make#

silk
pub fn make() -> Shapes

Returns an empty table. Schemas.shape fills it.

Method root#

silk
pub fn root(self: &Self) -> ShapeRef

Returns the shape of the type the table was made for.

Method setRoot#

silk
pub fn setRoot(self: &mut Self, root: ShapeRef) -> ()

Sets the node of the type the table describes.

Method get#

silk
pub fn get(self: &Self, at: ShapeRef) -> &Shape

Returns the node at refers to.

Method length#

silk
pub fn length(self: &Self) -> usize

Returns the number of nodes.

Method add#

silk
pub effect fn add(self: &mut Self, shape: Shape) -> ShapeRef ! OutOfMemoryError ? &mut Allocator

Adds an unnamed node and returns its link.

Errors#

  • OutOfMemoryError if the Allocator cannot grow the table.

Method named#

silk
pub fn named(self: &Self, name: string<'static>) -> Option<ShapeRef>

Returns the node of the named type name, if the table has one, finished or pending.

Method reserve#

silk
pub effect fn reserve(self: &mut Self, name: string<'static>) -> ShapeRef ! OutOfMemoryError ? &mut Allocator

Adds a pending node for the named type name and returns its link. Fill it with Shapes.set once its parts are described; a part that refers back to name meanwhile finds this node with Shapes.named.

Errors#

  • OutOfMemoryError if the Allocator cannot grow the table.

Method set#

silk
pub fn set(self: &mut Self, at: ShapeRef, shape: Shape) -> ()

Replaces the node at with shape.

Method nameOf#

silk
pub fn nameOf(self: &Self, at: ShapeRef) -> string<'static>

Returns the name of the node at: its type's name for a struct or union, else empty text.

Category: Data formats.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page