@kontor/kontor-sdk 1.0.0-alpha.39 → 1.0.0-alpha.40

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (270) hide show
  1. package/README.md +1 -1
  2. package/dist/cjs/sdk/actions/kontor/public/call-view.js.map +1 -1
  3. package/dist/cjs/sdk/chains/definitions/signet.js +2 -2
  4. package/dist/cjs/sdk/test/e2e.test-deprecated.js +2 -0
  5. package/dist/cjs/sdk/test/e2e.test-deprecated.js.map +1 -0
  6. package/dist/cjs/sdk/utils/wit/codecs/bool.js +9 -21
  7. package/dist/cjs/sdk/utils/wit/codecs/bool.js.map +1 -1
  8. package/dist/cjs/sdk/utils/wit/codecs/contract-address.js +92 -0
  9. package/dist/cjs/sdk/utils/wit/codecs/contract-address.js.map +1 -0
  10. package/dist/cjs/sdk/utils/wit/codecs/index.js +29 -0
  11. package/dist/cjs/sdk/utils/wit/codecs/index.js.map +1 -0
  12. package/dist/cjs/sdk/utils/wit/codecs/list.js +21 -95
  13. package/dist/cjs/sdk/utils/wit/codecs/list.js.map +1 -1
  14. package/dist/cjs/sdk/utils/wit/codecs/numerics.js +228 -0
  15. package/dist/cjs/sdk/utils/wit/codecs/numerics.js.map +1 -0
  16. package/dist/cjs/sdk/utils/wit/codecs/option.js +21 -72
  17. package/dist/cjs/sdk/utils/wit/codecs/option.js.map +1 -1
  18. package/dist/cjs/sdk/utils/wit/codecs/result.js +32 -0
  19. package/dist/cjs/sdk/utils/wit/codecs/result.js.map +1 -0
  20. package/dist/cjs/sdk/utils/wit/codecs/string.js +16 -21
  21. package/dist/cjs/sdk/utils/wit/codecs/string.js.map +1 -1
  22. package/dist/cjs/sdk/utils/wit/codecs/unit.js +11 -19
  23. package/dist/cjs/sdk/utils/wit/codecs/unit.js.map +1 -1
  24. package/dist/cjs/sdk/utils/wit/codecs/util.js +185 -0
  25. package/dist/cjs/sdk/utils/wit/codecs/util.js.map +1 -0
  26. package/dist/cjs/sdk/utils/wit/decode-wit-parameter.js +297 -163
  27. package/dist/cjs/sdk/utils/wit/decode-wit-parameter.js.map +1 -1
  28. package/dist/cjs/sdk/utils/wit/encode-wit-parameters.js +214 -221
  29. package/dist/cjs/sdk/utils/wit/encode-wit-parameters.js.map +1 -1
  30. package/dist/cjs/tsconfig.build.tsbuildinfo +1 -1
  31. package/dist/cjs/wit/built-ins.js +19 -0
  32. package/dist/cjs/wit/built-ins.js.map +1 -0
  33. package/dist/cjs/wit/regex.js +1 -1
  34. package/dist/cjs/wit/regex.js.map +1 -1
  35. package/dist/cjs/wit/wit-parser/core/errors/signature.js +17 -2
  36. package/dist/cjs/wit/wit-parser/core/errors/signature.js.map +1 -1
  37. package/dist/cjs/wit/wit-parser/core/signatures.js +18 -0
  38. package/dist/cjs/wit/wit-parser/core/signatures.js.map +1 -1
  39. package/dist/cjs/wit/wit-parser/core/types/{records.js → user-defined.js} +1 -1
  40. package/dist/cjs/wit/wit-parser/core/types/user-defined.js.map +1 -0
  41. package/dist/cjs/wit/wit-parser/core/user-defined.js +271 -0
  42. package/dist/cjs/wit/wit-parser/core/user-defined.js.map +1 -0
  43. package/dist/cjs/wit/wit-parser/core/utils.js +29 -7
  44. package/dist/cjs/wit/wit-parser/core/utils.js.map +1 -1
  45. package/dist/cjs/wit/wit-parser/parse-wit-parameter.js +9 -3
  46. package/dist/cjs/wit/wit-parser/parse-wit-parameter.js.map +1 -1
  47. package/dist/cjs/wit/wit-parser/parse-wit.js +7 -3
  48. package/dist/cjs/wit/wit-parser/parse-wit.js.map +1 -1
  49. package/dist/esm/sdk/actions/kontor/public/call-view.js.map +1 -1
  50. package/dist/esm/sdk/chains/definitions/signet.js +2 -2
  51. package/dist/esm/sdk/test/e2e.test-deprecated.js +197 -0
  52. package/dist/esm/sdk/test/e2e.test-deprecated.js.map +1 -0
  53. package/dist/esm/sdk/utils/wit/codecs/bool.js +9 -21
  54. package/dist/esm/sdk/utils/wit/codecs/bool.js.map +1 -1
  55. package/dist/esm/sdk/utils/wit/codecs/contract-address.js +92 -0
  56. package/dist/esm/sdk/utils/wit/codecs/contract-address.js.map +1 -0
  57. package/dist/esm/sdk/utils/wit/codecs/index.js +9 -0
  58. package/dist/esm/sdk/utils/wit/codecs/index.js.map +1 -0
  59. package/dist/esm/sdk/utils/wit/codecs/list.js +20 -114
  60. package/dist/esm/sdk/utils/wit/codecs/list.js.map +1 -1
  61. package/dist/esm/sdk/utils/wit/codecs/numerics.js +234 -0
  62. package/dist/esm/sdk/utils/wit/codecs/numerics.js.map +1 -0
  63. package/dist/esm/sdk/utils/wit/codecs/option.js +21 -88
  64. package/dist/esm/sdk/utils/wit/codecs/option.js.map +1 -1
  65. package/dist/esm/sdk/utils/wit/codecs/result.js +29 -0
  66. package/dist/esm/sdk/utils/wit/codecs/result.js.map +1 -0
  67. package/dist/esm/sdk/utils/wit/codecs/string.js +22 -22
  68. package/dist/esm/sdk/utils/wit/codecs/string.js.map +1 -1
  69. package/dist/esm/sdk/utils/wit/codecs/unit.js +12 -21
  70. package/dist/esm/sdk/utils/wit/codecs/unit.js.map +1 -1
  71. package/dist/esm/sdk/utils/wit/codecs/util.js +181 -0
  72. package/dist/esm/sdk/utils/wit/codecs/util.js.map +1 -0
  73. package/dist/esm/sdk/utils/wit/decode-wit-parameter.js +322 -188
  74. package/dist/esm/sdk/utils/wit/decode-wit-parameter.js.map +1 -1
  75. package/dist/esm/sdk/utils/wit/encode-wit-parameters.js +230 -233
  76. package/dist/esm/sdk/utils/wit/encode-wit-parameters.js.map +1 -1
  77. package/dist/esm/tsconfig.build.tsbuildinfo +1 -1
  78. package/dist/esm/wit/built-ins.js +16 -0
  79. package/dist/esm/wit/built-ins.js.map +1 -0
  80. package/dist/esm/wit/regex.js +1 -1
  81. package/dist/esm/wit/regex.js.map +1 -1
  82. package/dist/esm/wit/wit-parser/core/errors/signature.js +15 -1
  83. package/dist/esm/wit/wit-parser/core/errors/signature.js.map +1 -1
  84. package/dist/esm/wit/wit-parser/core/signatures.js +14 -0
  85. package/dist/esm/wit/wit-parser/core/signatures.js.map +1 -1
  86. package/dist/esm/wit/wit-parser/core/types/signatures.js.map +1 -1
  87. package/dist/esm/wit/wit-parser/core/types/user-defined.js +2 -0
  88. package/dist/esm/wit/wit-parser/core/types/user-defined.js.map +1 -0
  89. package/dist/esm/wit/wit-parser/core/types/utils.js +25 -0
  90. package/dist/esm/wit/wit-parser/core/types/utils.js.map +1 -1
  91. package/dist/esm/wit/wit-parser/core/user-defined.js +280 -0
  92. package/dist/esm/wit/wit-parser/core/user-defined.js.map +1 -0
  93. package/dist/esm/wit/wit-parser/core/utils.js +33 -36
  94. package/dist/esm/wit/wit-parser/core/utils.js.map +1 -1
  95. package/dist/esm/wit/wit-parser/parse-wit-parameter.js +10 -4
  96. package/dist/esm/wit/wit-parser/parse-wit-parameter.js.map +1 -1
  97. package/dist/esm/wit/wit-parser/parse-wit.js +8 -4
  98. package/dist/esm/wit/wit-parser/parse-wit.js.map +1 -1
  99. package/dist/types/sdk/actions/kontor/public/call-view.d.ts +2 -1
  100. package/dist/types/sdk/actions/kontor/public/call-view.d.ts.map +1 -1
  101. package/dist/types/sdk/chains/definitions/signet.d.ts +2 -2
  102. package/dist/types/sdk/test/e2e.test-deprecated.d.ts +2 -0
  103. package/dist/types/sdk/test/e2e.test-deprecated.d.ts.map +1 -0
  104. package/dist/types/sdk/types/contract.d.ts +5 -5
  105. package/dist/types/sdk/types/contract.d.ts.map +1 -1
  106. package/dist/types/sdk/utils/wit/codecs/bool.d.ts +2 -2
  107. package/dist/types/sdk/utils/wit/codecs/bool.d.ts.map +1 -1
  108. package/dist/types/sdk/utils/wit/codecs/contract-address.d.ts +5 -0
  109. package/dist/types/sdk/utils/wit/codecs/contract-address.d.ts.map +1 -0
  110. package/dist/types/sdk/utils/wit/codecs/index.d.ts +9 -0
  111. package/dist/types/sdk/utils/wit/codecs/index.d.ts.map +1 -0
  112. package/dist/types/sdk/utils/wit/codecs/list.d.ts +2 -16
  113. package/dist/types/sdk/utils/wit/codecs/list.d.ts.map +1 -1
  114. package/dist/types/sdk/utils/wit/codecs/numerics.d.ts +14 -0
  115. package/dist/types/sdk/utils/wit/codecs/numerics.d.ts.map +1 -0
  116. package/dist/types/sdk/utils/wit/codecs/option.d.ts +3 -23
  117. package/dist/types/sdk/utils/wit/codecs/option.d.ts.map +1 -1
  118. package/dist/types/sdk/utils/wit/codecs/result.d.ts +4 -0
  119. package/dist/types/sdk/utils/wit/codecs/result.d.ts.map +1 -0
  120. package/dist/types/sdk/utils/wit/codecs/string.d.ts +6 -2
  121. package/dist/types/sdk/utils/wit/codecs/string.d.ts.map +1 -1
  122. package/dist/types/sdk/utils/wit/codecs/types.d.ts +4 -0
  123. package/dist/types/sdk/utils/wit/codecs/types.d.ts.map +1 -1
  124. package/dist/types/sdk/utils/wit/codecs/unit.d.ts +2 -11
  125. package/dist/types/sdk/utils/wit/codecs/unit.d.ts.map +1 -1
  126. package/dist/types/sdk/utils/wit/codecs/util.d.ts +12 -0
  127. package/dist/types/sdk/utils/wit/codecs/util.d.ts.map +1 -0
  128. package/dist/types/sdk/utils/wit/decode-wit-parameter.d.ts +9 -31
  129. package/dist/types/sdk/utils/wit/decode-wit-parameter.d.ts.map +1 -1
  130. package/dist/types/sdk/utils/wit/encode-wit-parameters.d.ts +9 -1
  131. package/dist/types/sdk/utils/wit/encode-wit-parameters.d.ts.map +1 -1
  132. package/dist/types/wit/built-ins.d.ts +6 -0
  133. package/dist/types/wit/built-ins.d.ts.map +1 -0
  134. package/dist/types/wit/regex.d.ts.map +1 -1
  135. package/dist/types/wit/utils.d.ts +53 -33
  136. package/dist/types/wit/utils.d.ts.map +1 -1
  137. package/dist/types/wit/wit-parser/core/errors/signature.d.ts +7 -1
  138. package/dist/types/wit/wit-parser/core/errors/signature.d.ts.map +1 -1
  139. package/dist/types/wit/wit-parser/core/signatures.d.ts +10 -0
  140. package/dist/types/wit/wit-parser/core/signatures.d.ts.map +1 -1
  141. package/dist/types/wit/wit-parser/core/types/signatures.d.ts +6 -2
  142. package/dist/types/wit/wit-parser/core/types/signatures.d.ts.map +1 -1
  143. package/dist/types/wit/wit-parser/core/types/user-defined.d.ts +134 -0
  144. package/dist/types/wit/wit-parser/core/types/user-defined.d.ts.map +1 -0
  145. package/dist/types/wit/wit-parser/core/types/utils.d.ts +86 -11
  146. package/dist/types/wit/wit-parser/core/types/utils.d.ts.map +1 -1
  147. package/dist/types/wit/wit-parser/core/user-defined.d.ts +19 -0
  148. package/dist/types/wit/wit-parser/core/user-defined.d.ts.map +1 -0
  149. package/dist/types/wit/wit-parser/core/utils.d.ts +8 -4
  150. package/dist/types/wit/wit-parser/core/utils.d.ts.map +1 -1
  151. package/dist/types/wit/wit-parser/parse-wit-parameter.d.ts +11 -5
  152. package/dist/types/wit/wit-parser/parse-wit-parameter.d.ts.map +1 -1
  153. package/dist/types/wit/wit-parser/parse-wit.d.ts +7 -3
  154. package/dist/types/wit/wit-parser/parse-wit.d.ts.map +1 -1
  155. package/dist/types/wit/wit.d.ts +20 -12
  156. package/dist/types/wit/wit.d.ts.map +1 -1
  157. package/package.json +1 -1
  158. package/src/sdk/actions/get-contract.test-d.ts +430 -50
  159. package/src/sdk/actions/get-contract.test.ts +324 -24
  160. package/src/sdk/actions/kontor/public/call-view.ts +2 -1
  161. package/src/sdk/actions/kontor/public/proc-contract.test-d.ts +107 -0
  162. package/src/sdk/actions/kontor/public/proc-contract.test.ts +234 -21
  163. package/src/sdk/actions/kontor/public/view-contract.test-d.ts +240 -0
  164. package/src/sdk/actions/kontor/public/view-contract.test.ts +613 -18
  165. package/src/sdk/chains/definitions/signet.ts +2 -2
  166. package/src/sdk/clients/base/create-rpc-client.test.ts +2 -2
  167. package/src/sdk/clients/create-public-client.test.ts +2 -2
  168. package/src/sdk/clients/kontor/create-public-client.test.ts +2 -2
  169. package/src/sdk/clients/transports/http-rpc.test.ts +0 -24
  170. package/src/sdk/test/e2e.test-deprecated.ts +195 -0
  171. package/src/sdk/types/contract.test-d.ts +489 -55
  172. package/src/sdk/types/contract.ts +6 -7
  173. package/src/sdk/utils/wit/codecs/bool.test.ts +29 -0
  174. package/src/sdk/utils/wit/codecs/bool.ts +10 -25
  175. package/src/sdk/utils/wit/codecs/contract-address.test.ts +37 -0
  176. package/src/sdk/utils/wit/codecs/contract-address.ts +114 -0
  177. package/src/sdk/utils/wit/codecs/index.ts +20 -0
  178. package/src/sdk/utils/wit/codecs/list.test-d.ts +13 -0
  179. package/src/sdk/utils/wit/codecs/list.test.ts +55 -0
  180. package/src/sdk/utils/wit/codecs/list.ts +21 -150
  181. package/src/sdk/utils/wit/codecs/numerics.test.ts +399 -0
  182. package/src/sdk/utils/wit/codecs/numerics.ts +304 -0
  183. package/src/sdk/utils/wit/codecs/option.test.ts +71 -0
  184. package/src/sdk/utils/wit/codecs/option.ts +21 -120
  185. package/src/sdk/utils/wit/codecs/result.test.ts +82 -0
  186. package/src/sdk/utils/wit/codecs/result.ts +37 -0
  187. package/src/sdk/utils/wit/codecs/string.test.ts +62 -0
  188. package/src/sdk/utils/wit/codecs/string.ts +23 -25
  189. package/src/sdk/utils/wit/codecs/types.ts +5 -0
  190. package/src/sdk/utils/wit/codecs/unit.test.ts +27 -0
  191. package/src/sdk/utils/wit/codecs/unit.ts +14 -38
  192. package/src/sdk/utils/wit/codecs/util.test.ts +221 -0
  193. package/src/sdk/utils/wit/codecs/util.ts +193 -0
  194. package/src/sdk/utils/wit/decode-wit-parameter.test.ts +1264 -32
  195. package/src/sdk/utils/wit/decode-wit-parameter.ts +508 -211
  196. package/src/sdk/utils/wit/encode-wit-parameters.test.ts +955 -91
  197. package/src/sdk/utils/wit/encode-wit-parameters.ts +277 -274
  198. package/src/sdk/utils/wit/get-wit-item.test.ts +1 -1
  199. package/src/wit/built-ins.ts +23 -0
  200. package/src/wit/regex.ts +1 -1
  201. package/src/wit/utils.ts +120 -74
  202. package/src/wit/wit-parser/core/errors/signature.ts +13 -2
  203. package/src/wit/wit-parser/core/signatures.ts +28 -0
  204. package/src/wit/wit-parser/core/types/signatures.test-d.ts +21 -0
  205. package/src/wit/wit-parser/core/types/signatures.ts +19 -2
  206. package/src/wit/wit-parser/core/types/user-defined.test-d.ts +1308 -0
  207. package/src/wit/wit-parser/core/types/user-defined.ts +412 -0
  208. package/src/wit/wit-parser/core/types/utils.test-d.ts +43 -2
  209. package/src/wit/wit-parser/core/types/utils.ts +143 -14
  210. package/src/wit/wit-parser/core/user-defined.test.ts +609 -0
  211. package/src/wit/wit-parser/core/user-defined.ts +392 -0
  212. package/src/wit/wit-parser/core/utils.test.ts +334 -115
  213. package/src/wit/wit-parser/core/utils.ts +54 -38
  214. package/src/wit/wit-parser/parse-wit-parameter.test.ts +282 -4
  215. package/src/wit/wit-parser/parse-wit-parameter.ts +37 -11
  216. package/src/wit/wit-parser/parse-wit.test.ts +738 -12
  217. package/src/wit/wit-parser/parse-wit.ts +25 -10
  218. package/src/wit/wit.ts +37 -16
  219. package/vitest.config.ts +5 -0
  220. package/dist/cjs/sdk/utils/wit/codecs/decimal.js +0 -148
  221. package/dist/cjs/sdk/utils/wit/codecs/decimal.js.map +0 -1
  222. package/dist/cjs/sdk/utils/wit/codecs/enum.js +0 -94
  223. package/dist/cjs/sdk/utils/wit/codecs/enum.js.map +0 -1
  224. package/dist/cjs/sdk/utils/wit/codecs/integer.js +0 -125
  225. package/dist/cjs/sdk/utils/wit/codecs/integer.js.map +0 -1
  226. package/dist/cjs/sdk/utils/wit/codecs/s64.js +0 -57
  227. package/dist/cjs/sdk/utils/wit/codecs/s64.js.map +0 -1
  228. package/dist/cjs/sdk/utils/wit/codecs/u64.js +0 -56
  229. package/dist/cjs/sdk/utils/wit/codecs/u64.js.map +0 -1
  230. package/dist/cjs/wit/wit-parser/core/records.js +0 -70
  231. package/dist/cjs/wit/wit-parser/core/records.js.map +0 -1
  232. package/dist/cjs/wit/wit-parser/core/types/records.js.map +0 -1
  233. package/dist/esm/sdk/utils/wit/codecs/decimal.js +0 -165
  234. package/dist/esm/sdk/utils/wit/codecs/decimal.js.map +0 -1
  235. package/dist/esm/sdk/utils/wit/codecs/enum.js +0 -104
  236. package/dist/esm/sdk/utils/wit/codecs/enum.js.map +0 -1
  237. package/dist/esm/sdk/utils/wit/codecs/integer.js +0 -167
  238. package/dist/esm/sdk/utils/wit/codecs/integer.js.map +0 -1
  239. package/dist/esm/sdk/utils/wit/codecs/s64.js +0 -65
  240. package/dist/esm/sdk/utils/wit/codecs/s64.js.map +0 -1
  241. package/dist/esm/sdk/utils/wit/codecs/u64.js +0 -63
  242. package/dist/esm/sdk/utils/wit/codecs/u64.js.map +0 -1
  243. package/dist/esm/wit/wit-parser/core/records.js +0 -73
  244. package/dist/esm/wit/wit-parser/core/records.js.map +0 -1
  245. package/dist/esm/wit/wit-parser/core/types/records.js +0 -2
  246. package/dist/esm/wit/wit-parser/core/types/records.js.map +0 -1
  247. package/dist/types/sdk/utils/wit/codecs/decimal.d.ts +0 -42
  248. package/dist/types/sdk/utils/wit/codecs/decimal.d.ts.map +0 -1
  249. package/dist/types/sdk/utils/wit/codecs/enum.d.ts +0 -49
  250. package/dist/types/sdk/utils/wit/codecs/enum.d.ts.map +0 -1
  251. package/dist/types/sdk/utils/wit/codecs/integer.d.ts +0 -36
  252. package/dist/types/sdk/utils/wit/codecs/integer.d.ts.map +0 -1
  253. package/dist/types/sdk/utils/wit/codecs/s64.d.ts +0 -23
  254. package/dist/types/sdk/utils/wit/codecs/s64.d.ts.map +0 -1
  255. package/dist/types/sdk/utils/wit/codecs/u64.d.ts +0 -23
  256. package/dist/types/sdk/utils/wit/codecs/u64.d.ts.map +0 -1
  257. package/dist/types/wit/wit-parser/core/records.d.ts +0 -4
  258. package/dist/types/wit/wit-parser/core/records.d.ts.map +0 -1
  259. package/dist/types/wit/wit-parser/core/types/records.d.ts +0 -51
  260. package/dist/types/wit/wit-parser/core/types/records.d.ts.map +0 -1
  261. package/src/sdk/test/e2e.test.ts +0 -194
  262. package/src/sdk/utils/wit/codecs/decimal.ts +0 -215
  263. package/src/sdk/utils/wit/codecs/enum.ts +0 -114
  264. package/src/sdk/utils/wit/codecs/integer.ts +0 -193
  265. package/src/sdk/utils/wit/codecs/s64.ts +0 -76
  266. package/src/sdk/utils/wit/codecs/u64.ts +0 -73
  267. package/src/wit/wit-parser/core/records.test.ts +0 -69
  268. package/src/wit/wit-parser/core/records.ts +0 -101
  269. package/src/wit/wit-parser/core/types/records.test-d.ts +0 -331
  270. package/src/wit/wit-parser/core/types/records.ts +0 -91
@@ -0,0 +1,71 @@
1
+ import { describe, it, expect } from "vitest";
2
+
3
+ import { OptionCodec } from "./option.js";
4
+ import { IntegerCodec } from "./numerics.js"; // adjust if different path
5
+
6
+ import type { Option } from "../../../../wit/utils.js";
7
+
8
+ describe("optionCodec", () => {
9
+ const OptionOfInteger = OptionCodec(IntegerCodec);
10
+ it("decodes and encodes none", () => {
11
+ expect(OptionOfInteger.decodeWave("none")).toEqual(["none"]);
12
+ expect(OptionOfInteger.encodeWave(["none"])).toBe("none");
13
+ });
14
+
15
+ it("decodes some(<wave>) by delegating to the inner codec", () => {
16
+ const wave = "some({r0: 7, r1: 0, r2: 0, r3: 0, sign: plus})";
17
+ const decoded = OptionOfInteger.decodeWave(wave);
18
+
19
+ expect(decoded).toEqual(["some", 7n] satisfies Option<bigint>);
20
+ });
21
+
22
+ it("encodes some(value) using the inner codec's canonical encoding", () => {
23
+ const js: Option<bigint> = ["some", 7n];
24
+ expect(OptionOfInteger.encodeWave(js)).toBe(
25
+ "some({r0: 7, r1: 0, r2: 0, r3: 0, sign: plus})",
26
+ );
27
+ });
28
+
29
+ it("round-trips: decode -> encode -> decode", () => {
30
+ const wave = "some({r0: 123, r1: 0, r2: 0, r3: 0, sign: plus})";
31
+ const js1 = OptionOfInteger.decodeWave(wave);
32
+ const wave2 = OptionOfInteger.encodeWave(js1);
33
+ const js2 = OptionOfInteger.decodeWave(wave2);
34
+ expect(js2).toEqual(js1);
35
+ });
36
+
37
+ it("throws on invalid option constructor", () => {
38
+ expect(() => OptionOfInteger.decodeWave("ok(1)")).toThrow(
39
+ "invalid option: ok(1)",
40
+ );
41
+ expect(() => OptionOfInteger.decodeWave("err(1)")).toThrow(
42
+ "invalid option: err(1)",
43
+ );
44
+ });
45
+
46
+ it("throws when some() has an empty argument", () => {
47
+ expect(() => OptionOfInteger.decodeWave("some()")).toThrow(
48
+ "invalid option: some()",
49
+ );
50
+ expect(() => OptionOfInteger.decodeWave("some( )")).toThrow(
51
+ "invalid option: some( )",
52
+ );
53
+ });
54
+
55
+ it("throws when trailing junk exists after some(...)", () => {
56
+ // parseConstructorCall will throw, and we want to preserve that behavior
57
+ expect(() => OptionOfInteger.decodeWave("some(1))")).toThrow(
58
+ 'unexpected trailing characters after some(...): ")"',
59
+ );
60
+ expect(() => OptionOfInteger.decodeWave("some(1) nope")).toThrow(
61
+ 'unexpected trailing characters after some(...): "nope"',
62
+ );
63
+ });
64
+
65
+ it("allows whitespace around the whole expression", () => {
66
+ const wave = " some({r0: 7, r1: 0, r2: 0, r3: 0, sign: plus}) ";
67
+ expect(OptionOfInteger.decodeWave(wave)).toEqual(["some", 7n]);
68
+ });
69
+
70
+ // TODO: add nested
71
+ });
@@ -1,128 +1,29 @@
1
- import type {
2
- WaveExpr,
3
- CodecConstructor,
4
- CodecInstance,
5
- CodecJs,
6
- CodecInst,
7
- CodecWave,
8
- } from "./types.js";
1
+ import { type Codec } from "./types.js";
2
+ import { parseConstructorCall } from "./util.js";
9
3
 
10
- import { Enum } from "./enum.js";
4
+ import type { Option } from "../../../../wit/utils.js";
11
5
 
12
- export const KontorOption = Enum.Option;
6
+ export function OptionCodec<T>(inner: Codec<T>): Codec<Option<T>> {
7
+ return {
8
+ decodeWave(expr: string): Option<T> {
9
+ const s = expr.trim();
13
10
 
14
- // The JS-side shape we expose for option<T>
15
- export type OptionJs<T> = { kind: "none" } | { kind: "some"; value: T };
11
+ if (s === "none") return ["none"];
16
12
 
17
- /**
18
- * Instance that represents a decoded option<Inner> value.
19
- *
20
- * Generic parameterization is on the *inner codec constructor* so that
21
- * the JS type of this option instance is always OptionJs<CodecJs<InnerC>>.
22
- */
23
- export class OptionCodecFactory<
24
- InnerC extends CodecConstructor<unknown, CodecInstance<unknown>, unknown>,
25
- > implements CodecInstance<OptionJs<CodecJs<InnerC>>>
26
- {
27
- constructor(
28
- private readonly innerCodec: InnerC,
29
- public readonly data: Enum.Option.T, // parsed wave data for option
30
- ) {}
13
+ const { name, arg } = parseConstructorCall(s); // shared wave util
14
+ if (name !== "some" || arg == null)
15
+ throw new Error(`invalid option: ${expr}`);
31
16
 
32
- toWave(): WaveExpr {
33
- if (this.data.kind === "none") {
34
- return "none" as WaveExpr;
35
- }
36
- // assume data.value is a WaveExpr string for the inner
37
- return `some(${this.data.value ?? ""})` as WaveExpr;
38
- }
17
+ return ["some", inner.decodeWave(arg)];
18
+ },
39
19
 
40
- toJs(): OptionJs<CodecJs<InnerC>> {
41
- if (this.data.kind === "none") {
42
- return { kind: "none" };
43
- }
44
- if (this.data.value == null) {
45
- throw new Error("Option some() has no inner value");
46
- }
47
-
48
- // 1) parse inner WaveExpr → inner WaveData
49
- const innerWaveData = this.innerCodec.parseWave(
50
- this.data.value,
51
- ) as CodecWave<InnerC>;
52
-
53
- // 2) WaveData → inner instance
54
- const innerInst = this.innerCodec.fromKontor(
55
- innerWaveData,
56
- ) as CodecInst<InnerC>;
57
-
58
- // 3) instance → JS
59
- return { kind: "some", value: innerInst.toJs() as CodecJs<InnerC> };
60
- }
61
-
62
- // ----- Static factory to build a typed option<T> codec constructor -----
63
-
64
- static create<
65
- InnerC extends CodecConstructor<unknown, CodecInstance<unknown>, unknown>,
66
- >(
67
- innerCodec: InnerC,
68
- ): CodecConstructor<
69
- // JS type this codec produces:
70
- OptionJs<CodecJs<InnerC>>,
71
- // instance type:
72
- OptionCodecFactory<InnerC>,
73
- // wave data type (parsed form of "none" | "some(<inner>)"):
74
- Enum.Option.T
75
- > {
76
- // Class expression implements the static methods required by CodecConstructor
77
- class OptionCodecClass implements CodecInstance<OptionJs<CodecJs<InnerC>>> {
78
- // Internally forward all instance behavior to OptionCodecInstance
79
- private readonly _impl: OptionCodecFactory<InnerC>;
80
-
81
- constructor(data: Enum.Option.T) {
82
- this._impl = new OptionCodecFactory(innerCodec, data);
83
- }
84
-
85
- // ----- CodecConstructor statics -----
86
-
87
- static fromJs(
88
- value: OptionJs<CodecJs<InnerC>>,
89
- ): OptionCodecFactory<InnerC> {
90
- if (value.kind === "none") {
91
- return new OptionCodecFactory(innerCodec, { kind: "none" });
92
- }
93
- // encode inner via innerCodec
94
- const innerInst = innerCodec.fromJs(value.value as CodecJs<InnerC>);
95
- const innerWave = innerInst.toWave();
96
- return new OptionCodecFactory(innerCodec, {
97
- kind: "some",
98
- value: innerWave,
99
- });
20
+ encodeWave(value: Option<T>): string {
21
+ switch (value[0]) {
22
+ case "none":
23
+ return "none";
24
+ case "some":
25
+ return `some(${inner.encodeWave(value[1])})`;
100
26
  }
101
-
102
- static parseWave(expr: string): Enum.Option.T {
103
- // Expect raw "none" or "some(<inner...>)"
104
- return Enum.Option.schema.parse(expr);
105
- }
106
-
107
- static fromKontor(data: Enum.Option.T): OptionCodecFactory<InnerC> {
108
- return new OptionCodecFactory(innerCodec, data);
109
- }
110
-
111
- // ----- Instance side -----
112
-
113
- toWave(): WaveExpr {
114
- return this._impl.toWave();
115
- }
116
- toJs(): OptionJs<CodecJs<InnerC>> {
117
- return this._impl.toJs();
118
- }
119
- }
120
-
121
- // Satisfy the CodecConstructor contract with precise generics
122
- return OptionCodecClass as unknown as CodecConstructor<
123
- OptionJs<CodecJs<InnerC>>,
124
- OptionCodecFactory<InnerC>,
125
- Enum.Option.T
126
- >;
127
- }
27
+ },
28
+ };
128
29
  }
@@ -0,0 +1,82 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { ResultCodec } from "./result.js";
3
+ import { IntegerCodec, DecimalCodec } from "./numerics.js";
4
+ import type { Result } from "../../../../wit/utils.js";
5
+
6
+ describe("ResultCodec", () => {
7
+ const ResultOfIntegerDecimal = ResultCodec(IntegerCodec, DecimalCodec);
8
+
9
+ it("decodes ok(<wave>) by delegating to the ok codec", () => {
10
+ const wave = "ok({r0: 7, r1: 0, r2: 0, r3: 0, sign: plus})";
11
+ const decoded = ResultOfIntegerDecimal.decodeWave(wave);
12
+
13
+ expect(decoded).toEqual(["ok", 7n] satisfies Result<
14
+ bigint,
15
+ [bigint, number]
16
+ >);
17
+ });
18
+
19
+ it("decodes err(<wave>) by delegating to the err codec", () => {
20
+ const wave = "err({r0: 9, r1: 0, r2: 0, r3: 0, sign: minus})";
21
+ const decoded = ResultOfIntegerDecimal.decodeWave(wave);
22
+
23
+ // DecimalCodec decodes to [coef, 18]
24
+ expect(decoded).toEqual(["err", [-9n, 18]] satisfies Result<
25
+ bigint,
26
+ [bigint, number]
27
+ >);
28
+ });
29
+
30
+ it("encodes ok and err with canonical inner encodings", () => {
31
+ expect(ResultOfIntegerDecimal.encodeWave(["ok", 7n])).toBe(
32
+ "ok({r0: 7, r1: 0, r2: 0, r3: 0, sign: plus})",
33
+ );
34
+
35
+ expect(ResultOfIntegerDecimal.encodeWave(["err", [-9n, 18]])).toBe(
36
+ "err({r0: 9, r1: 0, r2: 0, r3: 0, sign: minus})",
37
+ );
38
+ });
39
+
40
+ it("round-trips: decode -> encode -> decode", () => {
41
+ const wave1 = "ok({r0: 123, r1: 0, r2: 0, r3: 0, sign: plus})";
42
+ const js1 = ResultOfIntegerDecimal.decodeWave(wave1);
43
+ const wave2 = ResultOfIntegerDecimal.encodeWave(js1);
44
+ const js2 = ResultOfIntegerDecimal.decodeWave(wave2);
45
+
46
+ expect(js2).toEqual(js1);
47
+ expect(wave2).toBe(wave1);
48
+ });
49
+
50
+ it("throws on invalid constructor names", () => {
51
+ expect(() => ResultOfIntegerDecimal.decodeWave("some(1)")).toThrow(
52
+ "invalid result: some(1)",
53
+ );
54
+ expect(() => ResultOfIntegerDecimal.decodeWave("none")).toThrow(
55
+ "invalid result: none",
56
+ );
57
+ });
58
+
59
+ it("throws when ok()/err() has an empty argument", () => {
60
+ expect(() => ResultOfIntegerDecimal.decodeWave("ok()")).toThrow(
61
+ "invalid result: ok()",
62
+ );
63
+ expect(() => ResultOfIntegerDecimal.decodeWave("err( )")).toThrow(
64
+ "invalid result: err( )",
65
+ );
66
+ });
67
+
68
+ it("throws when trailing junk exists after ok(...)/err(...)", () => {
69
+ // parseConstructorCall is responsible for this error text
70
+ expect(() => ResultOfIntegerDecimal.decodeWave("ok(1))")).toThrow(
71
+ 'unexpected trailing characters after ok(...): ")"',
72
+ );
73
+ expect(() => ResultOfIntegerDecimal.decodeWave("err(1) nope")).toThrow(
74
+ 'unexpected trailing characters after err(...): "nope"',
75
+ );
76
+ });
77
+
78
+ it("allows whitespace around the whole expression", () => {
79
+ const wave = " ok({r0: 7, r1: 0, r2: 0, r3: 0, sign: plus}) ";
80
+ expect(ResultOfIntegerDecimal.decodeWave(wave)).toEqual(["ok", 7n]);
81
+ });
82
+ });
@@ -0,0 +1,37 @@
1
+ // result.ts
2
+ import type { Result } from "../../../../wit/utils.js";
3
+ import type { Codec } from "./types.js";
4
+ import { parseConstructorCall } from "./util.js";
5
+
6
+ export function ResultCodec<T, E>(
7
+ ok: Codec<T>,
8
+ err: Codec<E>,
9
+ ): Codec<Result<T, E>> {
10
+ return {
11
+ decodeWave(expr: string): Result<T, E> {
12
+ const s = expr.trim();
13
+ const { name, arg } = parseConstructorCall(s);
14
+
15
+ if (name === "ok") {
16
+ if (arg == null) throw new Error(`invalid result: ${expr}`);
17
+ return ["ok", ok.decodeWave(arg)];
18
+ }
19
+
20
+ if (name === "err") {
21
+ if (arg == null) throw new Error(`invalid result: ${expr}`);
22
+ return ["err", err.decodeWave(arg)];
23
+ }
24
+
25
+ throw new Error(`invalid result: ${expr}`);
26
+ },
27
+
28
+ encodeWave(value: Result<T, E>): string {
29
+ switch (value[0]) {
30
+ case "ok":
31
+ return `ok(${ok.encodeWave(value[1])})`;
32
+ case "err":
33
+ return `err(${err.encodeWave(value[1])})`;
34
+ }
35
+ },
36
+ };
37
+ }
@@ -0,0 +1,62 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { StringCodec } from "./string.js";
3
+
4
+ describe("StringCodec", () => {
5
+ it("decodes quoted strings", () => {
6
+ expect(StringCodec.decodeWave(`"hello"`)).toBe("hello");
7
+ expect(StringCodec.decodeWave(`" spaced "`)).toBe(" spaced ");
8
+ });
9
+
10
+ it("encodes using JSON escaping rules", () => {
11
+ expect(StringCodec.encodeWave("hello")).toBe(`"hello"`);
12
+ expect(StringCodec.encodeWave(`he said "yo"`)).toBe(`"he said \\"yo\\""`);
13
+ expect(StringCodec.encodeWave("a\\b")).toBe(`"a\\\\b"`);
14
+ expect(StringCodec.encodeWave("line1\nline2")).toBe(`"line1\\nline2"`);
15
+ });
16
+
17
+ it("round-trips", () => {
18
+ const values = [
19
+ "",
20
+ "ascii",
21
+ `quote: "`,
22
+ "backslash: \\",
23
+ "newline:\n",
24
+ "tab:\t",
25
+ "emoji: 😀",
26
+ "json-ish: {a:1} [x] (y), commas, colons: :",
27
+ ];
28
+
29
+ for (const v of values) {
30
+ const wave = StringCodec.encodeWave(v);
31
+ const decoded = StringCodec.decodeWave(wave);
32
+ expect(decoded).toBe(v);
33
+ }
34
+ });
35
+
36
+ it("throws on non-strings / invalid wave", () => {
37
+ expect(() => StringCodec.decodeWave("hello")).toThrow(
38
+ "invalid string: hello",
39
+ );
40
+ expect(() => StringCodec.decodeWave("123")).toThrow("invalid string: 123");
41
+ expect(() => StringCodec.decodeWave("true")).toThrow(
42
+ "invalid string: true",
43
+ );
44
+ expect(() => StringCodec.decodeWave("null")).toThrow(
45
+ "invalid string: null",
46
+ );
47
+
48
+ // bad JSON string
49
+ expect(() => StringCodec.decodeWave(`"unterminated`)).toThrow(
50
+ 'invalid string: "unterminated',
51
+ );
52
+
53
+ // JSON.parse can return non-string if not quoted (we guard earlier), but keep a sanity check
54
+ expect(() => StringCodec.decodeWave(`{"a":1}`)).toThrow(
55
+ 'invalid string: {"a":1}',
56
+ );
57
+ });
58
+
59
+ it("allows whitespace around the whole expression", () => {
60
+ expect(StringCodec.decodeWave(` "hi" `)).toBe("hi");
61
+ });
62
+ });
@@ -1,30 +1,28 @@
1
- import type { CodecConstructor, CodecInstance, WaveExpr } from "./types.js";
1
+ import type { Codec } from "./types.js";
2
2
 
3
- export const StringCodec = {
4
- fromJs(value: String) {
5
- return {
6
- toWave(): WaveExpr {
7
- // Wave syntax for string args: we assume quoted
8
- return `"${value}"` as WaveExpr;
9
- },
10
- toJs(): String {
11
- return value;
12
- },
13
- };
14
- },
3
+ /**
4
+ * Wave string literals follow JSON escaping rules (double-quoted).
5
+ * We encode using JSON.stringify to ensure valid escaping.
6
+ */
7
+ export const StringCodec: Codec<string> = {
8
+ decodeWave(expr: string): string {
9
+ const s = expr.trim();
10
+ // Require quoted string (Wave/JSON style)
11
+ if (s.length < 2 || s[0] !== `"` || s[s.length - 1] !== `"`) {
12
+ throw new Error(`invalid string: ${expr}`);
13
+ }
15
14
 
16
- fromKontor(data: string) {
17
- return {
18
- toWave(): WaveExpr {
19
- return `"${data}"` as WaveExpr;
20
- },
21
- toJs(): String {
22
- return data as String;
23
- },
24
- };
15
+ try {
16
+ const v = JSON.parse(s);
17
+ if (typeof v !== "string") throw new Error("not a string");
18
+ return v;
19
+ } catch {
20
+ throw new Error(`invalid string: ${expr}`);
21
+ }
25
22
  },
26
23
 
27
- parseWave(expr: string): string {
28
- return JSON.parse(expr);
24
+ encodeWave(value: string): string {
25
+ // JSON.stringify returns a valid double-quoted JSON string literal
26
+ return JSON.stringify(value);
29
27
  },
30
- } as unknown as CodecConstructor<String, CodecInstance<String>, string>;
28
+ };
@@ -106,3 +106,8 @@ export function makeDecoder<
106
106
  >(codec: C) {
107
107
  return new Decoder<C>(codec);
108
108
  }
109
+
110
+ export type Codec<T> = {
111
+ decodeWave(expr: string): T;
112
+ encodeWave(value: T): string; // or WaveExpr if you keep that brand
113
+ };
@@ -0,0 +1,27 @@
1
+ import { describe, it, expect } from "vitest";
2
+ import { UnitCodec } from "./unit.js";
3
+ import { unit } from "./types.js";
4
+
5
+ describe("UnitCodec", () => {
6
+ it("decodes underscore to unit", () => {
7
+ expect(UnitCodec.decodeWave("_")).toBe(unit);
8
+ expect(UnitCodec.decodeWave(" _ \n")).toBe(unit);
9
+ });
10
+
11
+ it("encodes unit to underscore", () => {
12
+ expect(UnitCodec.encodeWave(unit)).toBe("_");
13
+ });
14
+
15
+ it("round-trips", () => {
16
+ const wave = UnitCodec.encodeWave(unit);
17
+ expect(wave).toBe("_");
18
+ expect(UnitCodec.decodeWave(wave)).toBe(unit);
19
+ });
20
+
21
+ it("throws on invalid input", () => {
22
+ expect(() => UnitCodec.decodeWave("")).toThrow("invalid unit:");
23
+ expect(() => UnitCodec.decodeWave("()")).toThrow("invalid unit: ()");
24
+ expect(() => UnitCodec.decodeWave("none")).toThrow("invalid unit: none");
25
+ expect(() => UnitCodec.decodeWave("__")).toThrow("invalid unit: __");
26
+ });
27
+ });
@@ -1,38 +1,14 @@
1
- import {
2
- type CodecConstructor,
3
- type CodecInstance,
4
- type Unit,
5
- unit,
6
- type WaveExpr,
7
- } from "./types.js";
8
-
9
- class _UnitCodec implements CodecInstance<Unit> {
10
- constructor() {}
11
-
12
- static fromJs(_value: null): _UnitCodec {
13
- return new _UnitCodec();
14
- }
15
-
16
- static fromKontor(_data: unknown): _UnitCodec {
17
- return new _UnitCodec();
18
- }
19
-
20
- static parseWave(_expr: string): Unit {
21
- // ok branch payload is conceptually "unit"
22
- return unit;
23
- }
24
-
25
- toWave(): WaveExpr {
26
- return "" as WaveExpr;
27
- }
28
-
29
- toJs(): Unit {
30
- return unit;
31
- }
32
- }
33
-
34
- export const UnitCodec = _UnitCodec as unknown as CodecConstructor<
35
- Unit,
36
- _UnitCodec,
37
- unknown
38
- >;
1
+ import type { Codec, Unit } from "./types.js";
2
+ import { unit } from "./types.js";
3
+
4
+ export const UnitCodec: Codec<Unit> = {
5
+ decodeWave(expr: string): Unit {
6
+ const s = expr.trim();
7
+ if (s === "_") return unit;
8
+ throw new Error(`invalid unit: ${expr}`);
9
+ },
10
+
11
+ encodeWave(_value: Unit): string {
12
+ return "_";
13
+ },
14
+ };