Scoop API

scoop:scoop.core:0.1.0

src/decoding.scoop

Selected production source · src/decoding.scoop

1/**
2 * A format-independent decoding strategy for T, usable explicitly or through a type companion.
3 */
4public interface ValueDecoder<T> {
5 /**
6 * Reads one value from the supplied format backend, reporting incompatible input as a decoding
7 * failure.
8 */
9 public fun decode(decoder: Decoder): T
12/**
13 * A format backend positioned at one value. Select a keyed record, unkeyed sequence, or single
14 * scalar container according to the value's representation.
15 */
16public interface Decoder {
17 /**
18 * The backend-defined diagnostic path of the current value.
19 */
20 public val path: String
21 /**
22 * Selects the named-field record container at this value.
23 */
24 public fun keyed(): KeyedDecodingContainer
25 /**
26 * Selects the ordered sequence container at this value.
27 */
28 public fun unkeyed(): UnkeyedDecodingContainer
29 /**
30 * Selects the scalar or null container at this value.
31 */
32 public fun singleValue(): SingleValueDecodingContainer
35/**
36 * A named-field record container. Finish the record with end after processing its fields.
37 */
38public interface KeyedDecodingContainer {
39 /**
40 * The field names available in this record.
41 */
42 public val keys: List<String>
43 /**
44 * Returns the named child decoder, failing when the field is absent.
45 */
46 public fun required(name: String): Decoder
47 /**
48 * Returns None when the field is absent; explicit null is still a present child decoder.
49 */
50 public fun optional(name: String): Option<Decoder>
51 /**
52 * Completes the container and permits the backend to check its final structure.
53 */
54 public fun end(): Unit
57/**
58 * An ordered sequence container. Finish the sequence with end after processing its elements.
59 */
60public interface UnkeyedDecodingContainer {
61 /**
62 * Whether another sequence element remains.
63 */
64 public val hasNext: Boolean
65 /**
66 * Returns the decoder for the next element and advances the sequence position.
67 */
68 public fun element(): Decoder
69 /**
70 * Completes the container and permits the backend to check its final structure.
71 */
72 public fun end(): Unit
75/**
76 * The format backend's scalar and explicit-null operations. Supported representations and
77 * conversion errors belong to the backend.
78 */
79public interface SingleValueDecodingContainer {
80 /**
81 * Tests whether the current scalar represents explicit null.
82 */
83 public fun isNull(): Boolean
84 /**
85 * Reads one Boolean scalar using the backend's representation rules.
86 */
87 public fun readBoolean(): Boolean
88 /**
89 * Reads one Long scalar using the backend's representation rules.
90 */
91 public fun readLong(): Long
92 /**
93 * Reads one ULong scalar using the backend's representation rules.
94 */
95 public fun readULong(): ULong
96 /**
97 * Reads one Float scalar using the backend's representation rules.
98 */
99 public fun readFloat(): Float
100 /**
101 * Reads one Double scalar using the backend's representation rules.
102 */
103 public fun readDouble(): Double
104 /**
105 * Reads one String scalar using the backend's representation rules.
106 */
107 public fun readString(): String
108 /**
109 * Reads an explicit null.
110 */
111 public fun readNull(): Unit
114/**
115 * Reports a decoding failure with the backend's path to the affected value.
116 */
117public class DecodingException public constructor(
118 /**
119 * The backend-defined path of the value that failed.
120 */
121 public val path: String, message: String)
122 : Exception(Some(message))
124/**
125 * Adapts an ordinary function to the value decoding protocol.
126 */
127public class ValueDecoderFunction<T> public constructor(private val body: (Decoder) -> T)
128 : ValueDecoder<T> {
129 /**
130 * Invokes the wrapped decoding function.
131 */
132 public override fun decode(decoder: Decoder): T = body(decoder)
135/**
136 * Represents the zero-sized Unit value as an explicit null scalar.
137 */
138public object UnitDecoder : ValueDecoder<Unit> {
139 /**
140 * Uses the backend's explicit null operation for Unit.
141 */
142 public override fun decode(decoder: Decoder): Unit = decoder.singleValue().readNull()
145internal fun decodeSigned(decoder: Decoder, minimum: Long, maximum: Long): Long {
146 val result = decoder.singleValue().readLong()
147 if (result < minimum || result > maximum) {
148 throw DecodingException(decoder.path, "integer is out of range")
149 }
150 return result
153internal fun decodeUnsigned(decoder: Decoder, maximum: ULong): ULong {
154 val result = decoder.singleValue().readULong()
155 if (result > maximum) {
156 throw DecodingException(decoder.path, "integer is out of range")
157 }
158 return result