/
struct-field.ts
196 lines (184 loc) · 5.33 KB
/
struct-field.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import { toHex, toUtf8 } from "@ndn/util";
import type { Constructor } from "type-fest";
import { type Decodable, Decoder } from "./decoder";
import { type Encodable, Encoder } from "./encoder";
import { NNI } from "./nni";
/**
* StructBuilder field type.
* @typeParam T - Value type.
*/
export interface StructFieldType<T> {
/**
* Create a new value of type T.
*
* @remarks
* Invoked by the TLV class constructor on each non-repeatable required field.
*/
newValue: (this: void) => T;
/**
* Encode a value to sub-TLV element.
* @returns TLV-VALUE, or `Encoder.OmitEmpty` to omit the field.
*
* @remarks
* Invoked by TLV class `.encodeTo` method.
* If the field is optional and unset, this is not invoked.
* If the field is repeatable, this is invoked once per element.
*/
encode: (this: void, value: T) => Encodable | typeof Encoder.OmitEmpty;
/**
* Decode a value from sub-TLV element.
*
* @remarks
* Invoked by TLV class `.decodeFrom` method.
* If the field is repeatable, this is invoked once per sub-TLV element.
*/
decode: (this: void, tlv: Decoder.Tlv) => T;
/**
* Print a value as string representation.
* @defaultValue
* ```ts
* `${value}`
* ```
*
* @remarks
* Invoked by TLV class `.toString` method.
* If the field is optional and unset, this is not invoked.
* If the field is repeatable, this is invoked once per element.
*/
asString?: (this: void, value: T) => string;
}
export namespace StructFieldType {
/**
* Turn a TLV class into a field type, where the TLV directly appears as the field.
*
* @example
* Given this structure:
* ```abnf
* Outer = OUTER-TYPE TLV-LENGTH Inner
* Inner = INNER-TYPE TLV-LENGTH INNER-VALUE
* ```
*
* You can define the `Outer` builder:
* ```ts
* const buildOuter = new StructBuilder("Outer", TT.Outer)
* .add(TT.Inner, "inner", StructFieldType.wrap(Inner));
* ```
*
* `Inner` type must encode itself as a TLV, and its TLV-TYPE must equal field TLV-TYPE.
*/
export function wrap<T extends NonNullable<Encodable>>(
F: Constructor<T, []> & Decodable<T>,
overrides: Partial<StructFieldType<T>> = {},
): StructFieldType<T> {
return {
newValue: () => new F(),
encode: (value) => {
const d = new Decoder(Encoder.encode(value));
return d.read().value;
},
decode: ({ decoder }) => decoder.decode(F),
asString: (value) => value.toString(),
...overrides,
};
}
/**
* Turn a TLV class into a field type, where the TLV is nested inside the field.
*
* @example
* Given this structure:
* ```abnf
* Outer = OUTER-TYPE TLV-LENGTH Middle
* Middle = MIDDLE-TYPE TLV-LENGTH Inner
* Inner = INNER-TYPE TLV-LENGTH INNER-VALUE
* ```
*
* You can define the `Outer` builder:
* ```ts
* const buildOuter = new StructBuilder("Outer", TT.Outer)
* .add(TT.Middle, "inner", StructFieldType.nest(Inner));
* ```
*
* `Inner` type does not have to encode itself as a TLV. Its encoding result appears as
* the TLV-VALUE of the "middle" field TLV.
*/
export function nest<T extends NonNullable<Encodable>>(
F: Constructor<T, []> & Decodable<T>,
overrides: Partial<StructFieldType<T>> = {},
): StructFieldType<T> {
return {
newValue: () => new F(),
encode: (value) => value,
decode: ({ vd }) => vd.decode(F),
asString: (value) => value.toString(),
...overrides,
};
}
}
export const StructFieldBool: StructFieldType<boolean> = {
newValue: () => false,
encode: (value) => value ? new Uint8Array(0) : Encoder.OmitEmpty,
decode: () => true,
};
/**
* StructBuilder field type of non-negative integer.
*
* @remarks
* The field is defined as number.
* If the field is required, it is initialized as zero.
*/
export const StructFieldNNI: StructFieldType<number> = {
newValue: () => 0,
encode: NNI,
decode: ({ nni }) => nni,
};
/**
* StructBuilder field type of non-negative integer.
*
* @remarks
* The field is defined as bigint.
* If the field is required, it is initialized as zero.
*/
export const StructFieldNNIBig: StructFieldType<bigint> = {
newValue: () => 0n,
encode: NNI,
decode: ({ nniBig }) => nniBig,
};
/**
* Declare a StructBuilder field type of non-negative integer from an enum.
* @param Enum - A flat (not OR'ed flags) enum type.
*
* @remarks
* The field is defined as a flat enum type.
* If the field is required, it is initialized as zero.
*/
export function StructFieldEnum<E extends number>(Enum: Record<number, string>): StructFieldType<E> {
return {
...(StructFieldNNI as any),
asString: (value) => `${value}(${Enum[value] ?? "unknown"})`,
};
}
/**
* StructBuilder field type of UTF-8 text.
*
* @remarks
* The field is defined as string.
* If the field is required, it is initialized as an empty string.
*/
export const StructFieldText: StructFieldType<string> = {
newValue: () => "",
encode: toUtf8,
decode: ({ text }) => text,
};
/**
* StructBuilder field type of raw bytes.
*
* @remarks
* The field is defined as Uint8Array.
* If the field is required, it is initialized as an empty Uint8Array.
*/
export const StructFieldBytes: StructFieldType<Uint8Array> = {
newValue: () => new Uint8Array(),
encode: (value) => value,
decode: ({ value }) => value,
asString: (value) => toHex(value),
};