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
*/4
public interface ValueEncoder<T> {5
/**6
* Writes one value using the supplied format backend.7
*/8
public fun encode(value: T, encoder: Encoder): Unit9
}11
/**12
* A format backend positioned at one value. Select a keyed record, unkeyed sequence, or single13
* scalar container according to the value's representation.14
*/15
public interface Encoder {16
/**17
* The backend-defined diagnostic path of the current value.18
*/19
public val path: String20
/**21
* Selects the named-field record container at this value.22
*/23
public fun keyed(): KeyedEncodingContainer24
/**25
* Selects the ordered sequence container at this value.26
*/27
public fun unkeyed(): UnkeyedEncodingContainer28
/**29
* Selects the scalar or null container at this value.30
*/31
public fun singleValue(): SingleValueEncodingContainer32
}34
/**35
* A named-field record container. Finish the record with end after processing its fields.36
*/37
public interface KeyedEncodingContainer {38
/**39
* Opens the child encoder for the named field.40
*/41
public fun field(name: String): Encoder42
/**43
* Completes the container and permits the backend to check its final structure.44
*/45
public fun end(): Unit46
}48
/**49
* An ordered sequence container. Finish the sequence with end after processing its elements.50
*/51
public interface UnkeyedEncodingContainer {52
/**53
* Opens the encoder for the next sequence element.54
*/55
public fun element(): Encoder56
/**57
* Completes the container and permits the backend to check its final structure.58
*/59
public fun end(): Unit60
}62
/**63
* The format backend's scalar and explicit-null operations. Supported representations and64
* conversion errors belong to the backend.65
*/66
public interface SingleValueEncodingContainer {67
/**68
* Writes one Boolean scalar using the backend's representation rules.69
*/70
public fun writeBoolean(value: Boolean): Unit71
/**72
* Writes one Long scalar using the backend's representation rules.73
*/74
public fun writeLong(value: Long): Unit75
/**76
* Writes one ULong scalar using the backend's representation rules.77
*/78
public fun writeULong(value: ULong): Unit79
/**80
* Writes one Float scalar using the backend's representation rules.81
*/82
public fun writeFloat(value: Float): Unit83
/**84
* Writes one Double scalar using the backend's representation rules.85
*/86
public fun writeDouble(value: Double): Unit87
/**88
* Writes one String scalar using the backend's representation rules.89
*/90
public fun writeString(value: String): Unit91
/**92
* Writes an explicit null.93
*/94
public fun writeNull(): Unit95
}97
/**98
* Reports a encoding failure with the backend's path to the affected value.99
*/100
public 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
*/110
public 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)116
}118
/**119
* Represents the zero-sized Unit value as an explicit null scalar.120
*/121
public 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()127
}