Scoop API

scoop:scoop.core:0.1.0

src/encoding.scoop

Selected production source · src/encoding.scoop

1/**
2 * A format-independent encoding strategy for T, usable explicitly or through a type companion.
3 */
4public interface ValueEncoder<T> {
5 /**
6 * Writes one value using the supplied format backend.
7 */
8 public fun encode(value: T, encoder: Encoder): Unit
9}
11/**
12 * A format backend positioned at one value. Select a keyed record, unkeyed sequence, or single
13 * scalar container according to the value's representation.
14 */
15public interface Encoder {
16 /**
17 * The backend-defined diagnostic path of the current value.
18 */
19 public val path: String
20 /**
21 * Selects the named-field record container at this value.
22 */
23 public fun keyed(): KeyedEncodingContainer
24 /**
25 * Selects the ordered sequence container at this value.
26 */
27 public fun unkeyed(): UnkeyedEncodingContainer
28 /**
29 * Selects the scalar or null container at this value.
30 */
31 public fun singleValue(): SingleValueEncodingContainer
34/**
35 * A named-field record container. Finish the record with end after processing its fields.
36 */
37public interface KeyedEncodingContainer {
38 /**
39 * Opens the child encoder for the named field.
40 */
41 public fun field(name: String): Encoder
42 /**
43 * Completes the container and permits the backend to check its final structure.
44 */
45 public fun end(): Unit
48/**
49 * An ordered sequence container. Finish the sequence with end after processing its elements.
50 */
51public interface UnkeyedEncodingContainer {
52 /**
53 * Opens the encoder for the next sequence element.
54 */
55 public fun element(): Encoder
56 /**
57 * Completes the container and permits the backend to check its final structure.
58 */
59 public fun end(): Unit
62/**
63 * The format backend's scalar and explicit-null operations. Supported representations and
64 * conversion errors belong to the backend.
65 */
66public interface SingleValueEncodingContainer {
67 /**
68 * Writes one Boolean scalar using the backend's representation rules.
69 */
70 public fun writeBoolean(value: Boolean): Unit
71 /**
72 * Writes one Long scalar using the backend's representation rules.
73 */
74 public fun writeLong(value: Long): Unit
75 /**
76 * Writes one ULong scalar using the backend's representation rules.
77 */
78 public fun writeULong(value: ULong): Unit
79 /**
80 * Writes one Float scalar using the backend's representation rules.
81 */
82 public fun writeFloat(value: Float): Unit
83 /**
84 * Writes one Double scalar using the backend's representation rules.
85 */
86 public fun writeDouble(value: Double): Unit
87 /**
88 * Writes one String scalar using the backend's representation rules.
89 */
90 public fun writeString(value: String): Unit
91 /**
92 * Writes an explicit null.
93 */
94 public fun writeNull(): Unit
97/**
98 * Reports a encoding failure with the backend's path to the affected value.
99 */
100public class EncodingException public constructor(
101 /**
102 * The backend-defined path of the value that failed.
103 */
104 public val path: String, message: String)
105 : Exception(Some(message))
107/**
108 * Adapts an ordinary function to the value encoding protocol.
109 */
110public class ValueEncoderFunction<T> public constructor(private val body: (T, Encoder) -> Unit)
111 : ValueEncoder<T> {
112 /**
113 * Invokes the wrapped encoding function.
114 */
115 public override fun encode(value: T, encoder: Encoder): Unit = body(value, encoder)
118/**
119 * Represents the zero-sized Unit value as an explicit null scalar.
120 */
121public object UnitEncoder : ValueEncoder<Unit> {
122 /**
123 * Uses the backend's explicit null operation for Unit.
124 */
125 public override fun encode(value: Unit, encoder: Encoder): Unit =
126 encoder.singleValue().writeNull()