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
*/4
public interface ValueDecoder<T> {5
/**6
* Reads one value from the supplied format backend, reporting incompatible input as a decoding7
* failure.8
*/9
public fun decode(decoder: Decoder): T10
}12
/**13
* A format backend positioned at one value. Select a keyed record, unkeyed sequence, or single14
* scalar container according to the value's representation.15
*/16
public interface Decoder {17
/**18
* The backend-defined diagnostic path of the current value.19
*/20
public val path: String21
/**22
* Selects the named-field record container at this value.23
*/24
public fun keyed(): KeyedDecodingContainer25
/**26
* Selects the ordered sequence container at this value.27
*/28
public fun unkeyed(): UnkeyedDecodingContainer29
/**30
* Selects the scalar or null container at this value.31
*/32
public fun singleValue(): SingleValueDecodingContainer33
}35
/**36
* A named-field record container. Finish the record with end after processing its fields.37
*/38
public 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): Decoder47
/**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(): Unit55
}57
/**58
* An ordered sequence container. Finish the sequence with end after processing its elements.59
*/60
public interface UnkeyedDecodingContainer {61
/**62
* Whether another sequence element remains.63
*/64
public val hasNext: Boolean65
/**66
* Returns the decoder for the next element and advances the sequence position.67
*/68
public fun element(): Decoder69
/**70
* Completes the container and permits the backend to check its final structure.71
*/72
public fun end(): Unit73
}75
/**76
* The format backend's scalar and explicit-null operations. Supported representations and77
* conversion errors belong to the backend.78
*/79
public interface SingleValueDecodingContainer {80
/**81
* Tests whether the current scalar represents explicit null.82
*/83
public fun isNull(): Boolean84
/**85
* Reads one Boolean scalar using the backend's representation rules.86
*/87
public fun readBoolean(): Boolean88
/**89
* Reads one Long scalar using the backend's representation rules.90
*/91
public fun readLong(): Long92
/**93
* Reads one ULong scalar using the backend's representation rules.94
*/95
public fun readULong(): ULong96
/**97
* Reads one Float scalar using the backend's representation rules.98
*/99
public fun readFloat(): Float100
/**101
* Reads one Double scalar using the backend's representation rules.102
*/103
public fun readDouble(): Double104
/**105
* Reads one String scalar using the backend's representation rules.106
*/107
public fun readString(): String108
/**109
* Reads an explicit null.110
*/111
public fun readNull(): Unit112
}114
/**115
* Reports a decoding failure with the backend's path to the affected value.116
*/117
public 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
*/127
public 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)133
}135
/**136
* Represents the zero-sized Unit value as an explicit null scalar.137
*/138
public 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()143
}145
internal 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 result151
}153
internal 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 result159
}