sdkloom 0.0.0-stage → 0.1.0

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 (383) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +239 -2
  3. package/kits/allowed-sources.json +24 -0
  4. package/kits/csharp/kit.json +6 -0
  5. package/kits/csharp/src/Runtime/Client.cs +295 -0
  6. package/kits/csharp/src/Runtime/Context.cs +216 -0
  7. package/kits/csharp/src/Runtime/Errors.cs +121 -0
  8. package/kits/csharp/src/Runtime/Http.cs +218 -0
  9. package/kits/csharp/src/Runtime/Json.cs +650 -0
  10. package/kits/csharp/src/Runtime/Lang.cs +212 -0
  11. package/kits/csharp/src/Runtime/Models.cs +206 -0
  12. package/kits/csharp/src/Runtime/Multipart.cs +108 -0
  13. package/kits/csharp/src/Runtime/Pagination.cs +122 -0
  14. package/kits/csharp/src/Runtime/Sse.cs +197 -0
  15. package/kits/csharp/src/Runtime/Testing.cs +375 -0
  16. package/kits/csharp/src/Runtime/Transport.cs +212 -0
  17. package/kits/csharp/src/Runtime/Wait.cs +57 -0
  18. package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
  19. package/kits/csharp/tests/ClientTests.cs +128 -0
  20. package/kits/csharp/tests/HttpTests.cs +44 -0
  21. package/kits/csharp/tests/JsonTests.cs +132 -0
  22. package/kits/csharp/tests/LangTests.cs +57 -0
  23. package/kits/csharp/tests/MultipartTests.cs +31 -0
  24. package/kits/csharp/tests/PaginationTests.cs +93 -0
  25. package/kits/csharp/tests/SseTests.cs +122 -0
  26. package/kits/csharp/tests/TestRunner.cs +93 -0
  27. package/kits/csharp/tests/TestingTests.cs +80 -0
  28. package/kits/csharp/tests/TransportTests.cs +145 -0
  29. package/kits/csharp/tests/WaitTests.cs +39 -0
  30. package/kits/csharp/tests/WebhooksTests.cs +46 -0
  31. package/kits/csharp/versions.json +12 -0
  32. package/kits/go/kit.json +6 -0
  33. package/kits/go/src/sdkruntime/client.go +322 -0
  34. package/kits/go/src/sdkruntime/context.go +207 -0
  35. package/kits/go/src/sdkruntime/errors.go +74 -0
  36. package/kits/go/src/sdkruntime/http.go +261 -0
  37. package/kits/go/src/sdkruntime/json.go +527 -0
  38. package/kits/go/src/sdkruntime/lang.go +462 -0
  39. package/kits/go/src/sdkruntime/multipart.go +75 -0
  40. package/kits/go/src/sdkruntime/pagination.go +150 -0
  41. package/kits/go/src/sdkruntime/sse.go +267 -0
  42. package/kits/go/src/sdkruntime/testing.go +343 -0
  43. package/kits/go/src/sdkruntime/transport.go +168 -0
  44. package/kits/go/src/sdkruntime/wait.go +49 -0
  45. package/kits/go/src/sdkruntime/webhooks.go +129 -0
  46. package/kits/go/tests/client_test.go +185 -0
  47. package/kits/go/tests/http_test.go +78 -0
  48. package/kits/go/tests/json_test.go +127 -0
  49. package/kits/go/tests/lang_test.go +98 -0
  50. package/kits/go/tests/multipart_test.go +29 -0
  51. package/kits/go/tests/pagination_test.go +69 -0
  52. package/kits/go/tests/sse_test.go +129 -0
  53. package/kits/go/tests/testing_test.go +121 -0
  54. package/kits/go/tests/transport_test.go +133 -0
  55. package/kits/go/tests/wait_test.go +46 -0
  56. package/kits/go/tests/webhooks_test.go +62 -0
  57. package/kits/go/versions.json +12 -0
  58. package/kits/java/kit.json +6 -0
  59. package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
  60. package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
  61. package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
  62. package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
  63. package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
  64. package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
  65. package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
  66. package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
  67. package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
  68. package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
  69. package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
  70. package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
  71. package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
  72. package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
  73. package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
  74. package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
  75. package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
  76. package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
  77. package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
  78. package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
  79. package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
  80. package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
  81. package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
  82. package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
  83. package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
  84. package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
  85. package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
  86. package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
  87. package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
  88. package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
  89. package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
  90. package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
  91. package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
  92. package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
  93. package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
  94. package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
  95. package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
  96. package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
  97. package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
  98. package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
  99. package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
  100. package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
  101. package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
  102. package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
  103. package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
  104. package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
  105. package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
  106. package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
  107. package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
  108. package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
  109. package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
  110. package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
  111. package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
  112. package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
  113. package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
  114. package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
  115. package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
  116. package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
  117. package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
  118. package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
  119. package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
  120. package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
  121. package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
  122. package/kits/java/versions.json +41 -0
  123. package/kits/python/kit.json +6 -0
  124. package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
  125. package/kits/python/src/sdkloom_runtime/client.py +232 -0
  126. package/kits/python/src/sdkloom_runtime/compat.py +195 -0
  127. package/kits/python/src/sdkloom_runtime/context.py +134 -0
  128. package/kits/python/src/sdkloom_runtime/errors.py +65 -0
  129. package/kits/python/src/sdkloom_runtime/http.py +192 -0
  130. package/kits/python/src/sdkloom_runtime/lang.py +207 -0
  131. package/kits/python/src/sdkloom_runtime/models.py +221 -0
  132. package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
  133. package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
  134. package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
  135. package/kits/python/src/sdkloom_runtime/sse.py +121 -0
  136. package/kits/python/src/sdkloom_runtime/testing.py +224 -0
  137. package/kits/python/src/sdkloom_runtime/transport.py +120 -0
  138. package/kits/python/src/sdkloom_runtime/wait.py +44 -0
  139. package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
  140. package/kits/python/tests/support.py +28 -0
  141. package/kits/python/tests/test_client.py +275 -0
  142. package/kits/python/tests/test_compat.py +87 -0
  143. package/kits/python/tests/test_http.py +94 -0
  144. package/kits/python/tests/test_lang.py +104 -0
  145. package/kits/python/tests/test_models.py +110 -0
  146. package/kits/python/tests/test_multipart.py +37 -0
  147. package/kits/python/tests/test_pagination.py +70 -0
  148. package/kits/python/tests/test_sse.py +61 -0
  149. package/kits/python/tests/test_testing.py +130 -0
  150. package/kits/python/tests/test_transport.py +124 -0
  151. package/kits/python/tests/test_wait.py +46 -0
  152. package/kits/python/tests/test_webhooks.py +57 -0
  153. package/kits/python/versions.json +33 -0
  154. package/kits/rust/kit.json +6 -0
  155. package/kits/rust/src/runtime/client.rs +212 -0
  156. package/kits/rust/src/runtime/context.rs +182 -0
  157. package/kits/rust/src/runtime/crypto.rs +84 -0
  158. package/kits/rust/src/runtime/errors.rs +147 -0
  159. package/kits/rust/src/runtime/http.rs +248 -0
  160. package/kits/rust/src/runtime/json.rs +424 -0
  161. package/kits/rust/src/runtime/lang.rs +296 -0
  162. package/kits/rust/src/runtime/mod.rs +30 -0
  163. package/kits/rust/src/runtime/models.rs +266 -0
  164. package/kits/rust/src/runtime/multipart.rs +81 -0
  165. package/kits/rust/src/runtime/pagination.rs +117 -0
  166. package/kits/rust/src/runtime/sse.rs +195 -0
  167. package/kits/rust/src/runtime/testing.rs +308 -0
  168. package/kits/rust/src/runtime/transport.rs +324 -0
  169. package/kits/rust/src/runtime/wait.rs +29 -0
  170. package/kits/rust/src/runtime/webhooks.rs +142 -0
  171. package/kits/rust/tests/client.rs +152 -0
  172. package/kits/rust/tests/http.rs +44 -0
  173. package/kits/rust/tests/json.rs +82 -0
  174. package/kits/rust/tests/lang.rs +74 -0
  175. package/kits/rust/tests/multipart.rs +21 -0
  176. package/kits/rust/tests/pagination.rs +61 -0
  177. package/kits/rust/tests/sse.rs +77 -0
  178. package/kits/rust/tests/testing.rs +90 -0
  179. package/kits/rust/tests/transport.rs +218 -0
  180. package/kits/rust/tests/wait.rs +32 -0
  181. package/kits/rust/tests/webhooks.rs +73 -0
  182. package/kits/rust/versions.json +6 -0
  183. package/kits/typescript/kit.json +6 -0
  184. package/kits/typescript/src/client.ts +223 -0
  185. package/kits/typescript/src/context.ts +108 -0
  186. package/kits/typescript/src/errors.ts +63 -0
  187. package/kits/typescript/src/helpers.ts +49 -0
  188. package/kits/typescript/src/http.ts +192 -0
  189. package/kits/typescript/src/models.ts +139 -0
  190. package/kits/typescript/src/multipart.ts +66 -0
  191. package/kits/typescript/src/pagination.ts +54 -0
  192. package/kits/typescript/src/pipeline.ts +25 -0
  193. package/kits/typescript/src/sse.ts +102 -0
  194. package/kits/typescript/src/testing.ts +217 -0
  195. package/kits/typescript/src/transport.ts +101 -0
  196. package/kits/typescript/src/wait.ts +36 -0
  197. package/kits/typescript/src/webhooks.ts +168 -0
  198. package/kits/typescript/tests/client.test.ts +269 -0
  199. package/kits/typescript/tests/helpers.test.ts +27 -0
  200. package/kits/typescript/tests/http.test.ts +80 -0
  201. package/kits/typescript/tests/models.test.ts +71 -0
  202. package/kits/typescript/tests/multipart.test.ts +28 -0
  203. package/kits/typescript/tests/pagination.test.ts +76 -0
  204. package/kits/typescript/tests/sse.test.ts +82 -0
  205. package/kits/typescript/tests/testing.test.ts +103 -0
  206. package/kits/typescript/tests/transport.test.ts +127 -0
  207. package/kits/typescript/tests/wait.test.ts +31 -0
  208. package/kits/typescript/tests/webhooks.test.ts +53 -0
  209. package/kits/typescript/versions.json +15 -0
  210. package/package.json +37 -4
  211. package/packages/cli/recipes/apiKeyHeader.ts +16 -0
  212. package/packages/cli/recipes/apiVersionHeader.ts +16 -0
  213. package/packages/cli/recipes/basic.ts +13 -0
  214. package/packages/cli/recipes/bearer.ts +10 -0
  215. package/packages/cli/recipes/hmacSign.ts +23 -0
  216. package/packages/cli/recipes/idempotencyKey.ts +19 -0
  217. package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
  218. package/packages/cli/recipes/retry.ts +42 -0
  219. package/packages/cli/recipes/timing.ts +12 -0
  220. package/packages/cli/recipes/userAgent.ts +8 -0
  221. package/packages/cli/src/build.js +93 -0
  222. package/packages/cli/src/commands/build.js +31 -0
  223. package/packages/cli/src/commands/conformance-check.js +50 -0
  224. package/packages/cli/src/commands/contract.js +181 -0
  225. package/packages/cli/src/commands/diff.js +113 -0
  226. package/packages/cli/src/commands/doctor.js +43 -0
  227. package/packages/cli/src/commands/import-openapi.js +42 -0
  228. package/packages/cli/src/commands/init.js +27 -0
  229. package/packages/cli/src/commands/kit-check.js +145 -0
  230. package/packages/cli/src/commands/publish.js +81 -0
  231. package/packages/cli/src/commands/recipe.js +49 -0
  232. package/packages/cli/src/commands/record.js +181 -0
  233. package/packages/cli/src/commands/serve.js +156 -0
  234. package/packages/cli/src/commands/spec-check.js +57 -0
  235. package/packages/cli/src/commands/test.js +177 -0
  236. package/packages/cli/src/commands/variants.js +32 -0
  237. package/packages/cli/src/commands/verify.js +57 -0
  238. package/packages/cli/src/commands/version.js +63 -0
  239. package/packages/cli/src/containers.js +82 -0
  240. package/packages/cli/src/coverage.js +72 -0
  241. package/packages/cli/src/git.js +25 -0
  242. package/packages/cli/src/io.js +50 -0
  243. package/packages/cli/src/java/ScenarioHarness.java +297 -0
  244. package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
  245. package/packages/cli/src/java.js +70 -0
  246. package/packages/cli/src/js/harness.mjs +120 -0
  247. package/packages/cli/src/kits.js +103 -0
  248. package/packages/cli/src/languages/csharp.js +274 -0
  249. package/packages/cli/src/languages/go.js +185 -0
  250. package/packages/cli/src/languages/index.js +18 -0
  251. package/packages/cli/src/languages/java.js +187 -0
  252. package/packages/cli/src/languages/python.js +120 -0
  253. package/packages/cli/src/languages/rust.js +189 -0
  254. package/packages/cli/src/languages/types.js +93 -0
  255. package/packages/cli/src/languages/typescript.js +112 -0
  256. package/packages/cli/src/main.js +13 -0
  257. package/packages/cli/src/packaging.js +39 -0
  258. package/packages/cli/src/portability.js +45 -0
  259. package/packages/cli/src/profiles.js +55 -0
  260. package/packages/cli/src/project.js +56 -0
  261. package/packages/cli/src/python/scenario_harness.py +162 -0
  262. package/packages/cli/src/python/schema_roundtrip.py +42 -0
  263. package/packages/cli/src/registries.js +214 -0
  264. package/packages/cli/src/resolve-core.js +39 -0
  265. package/packages/cli/src/run.js +133 -0
  266. package/packages/cli/src/runtimes.js +61 -0
  267. package/packages/cli/src/scenarios-java.js +82 -0
  268. package/packages/cli/src/scenarios-javascript.js +54 -0
  269. package/packages/cli/src/scenarios-python.js +90 -0
  270. package/packages/cli/src/scenarios.js +183 -0
  271. package/packages/cli/src/schema.js +84 -0
  272. package/packages/cli/src/templates.js +85 -0
  273. package/packages/cli/src/toolchains.js +68 -0
  274. package/packages/cli/src/transpiled-tests.js +92 -0
  275. package/packages/codegen/src/canonical-json.js +31 -0
  276. package/packages/codegen/src/capabilities.js +50 -0
  277. package/packages/codegen/src/code-writer.js +54 -0
  278. package/packages/codegen/src/file-tree.js +25 -0
  279. package/packages/codegen/src/hash.js +6 -0
  280. package/packages/codegen/src/index.js +16 -0
  281. package/packages/codegen/src/lock.js +106 -0
  282. package/packages/conformance/src/case.js +143 -0
  283. package/packages/conformance/src/checks.js +40 -0
  284. package/packages/conformance/src/index.js +3 -0
  285. package/packages/conformance/src/matrix.js +48 -0
  286. package/packages/conformance/src/suite.js +48 -0
  287. package/packages/differ/src/diff.js +500 -0
  288. package/packages/differ/src/index.js +3 -0
  289. package/packages/differ/src/notes.js +112 -0
  290. package/packages/differ/src/support.js +34 -0
  291. package/packages/gen-csharp/src/emit-client.js +397 -0
  292. package/packages/gen-csharp/src/emit-harness.js +333 -0
  293. package/packages/gen-csharp/src/emit-mock.js +111 -0
  294. package/packages/gen-csharp/src/emit-models.js +119 -0
  295. package/packages/gen-csharp/src/emit-webhooks.js +57 -0
  296. package/packages/gen-csharp/src/generate.js +245 -0
  297. package/packages/gen-csharp/src/index.js +5 -0
  298. package/packages/gen-csharp/src/names.js +284 -0
  299. package/packages/gen-go/src/emit-client.js +405 -0
  300. package/packages/gen-go/src/emit-harness.js +294 -0
  301. package/packages/gen-go/src/emit-mock.js +112 -0
  302. package/packages/gen-go/src/emit-models.js +146 -0
  303. package/packages/gen-go/src/emit-webhooks.js +58 -0
  304. package/packages/gen-go/src/generate.js +205 -0
  305. package/packages/gen-go/src/index.js +5 -0
  306. package/packages/gen-go/src/names.js +197 -0
  307. package/packages/gen-java/src/emit-client.js +441 -0
  308. package/packages/gen-java/src/emit-models.js +342 -0
  309. package/packages/gen-java/src/emit-testing.js +123 -0
  310. package/packages/gen-java/src/emit-webhooks.js +70 -0
  311. package/packages/gen-java/src/generate.js +244 -0
  312. package/packages/gen-java/src/index.js +4 -0
  313. package/packages/gen-java/src/names.js +197 -0
  314. package/packages/gen-python/src/emit-client.js +272 -0
  315. package/packages/gen-python/src/emit-models.js +139 -0
  316. package/packages/gen-python/src/emit-testing.js +93 -0
  317. package/packages/gen-python/src/emit-webhooks.js +48 -0
  318. package/packages/gen-python/src/generate.js +237 -0
  319. package/packages/gen-python/src/index.js +2 -0
  320. package/packages/gen-python/src/names.js +161 -0
  321. package/packages/gen-python/src/style.js +67 -0
  322. package/packages/gen-rust/src/emit-client.js +379 -0
  323. package/packages/gen-rust/src/emit-harness.js +278 -0
  324. package/packages/gen-rust/src/emit-mock.js +121 -0
  325. package/packages/gen-rust/src/emit-models.js +117 -0
  326. package/packages/gen-rust/src/emit-webhooks.js +58 -0
  327. package/packages/gen-rust/src/generate.js +243 -0
  328. package/packages/gen-rust/src/index.js +5 -0
  329. package/packages/gen-rust/src/names.js +223 -0
  330. package/packages/gen-typescript/src/emit-client.js +434 -0
  331. package/packages/gen-typescript/src/emit-models.js +93 -0
  332. package/packages/gen-typescript/src/emit-package.js +90 -0
  333. package/packages/gen-typescript/src/emit-testing.js +138 -0
  334. package/packages/gen-typescript/src/emit-webhooks.js +52 -0
  335. package/packages/gen-typescript/src/generate.js +70 -0
  336. package/packages/gen-typescript/src/index.js +3 -0
  337. package/packages/gen-typescript/src/naming.js +64 -0
  338. package/packages/gen-typescript/src/runtime.js +81 -0
  339. package/packages/gen-typescript/src/types.js +136 -0
  340. package/packages/ir/src/api-surface.js +22 -0
  341. package/packages/ir/src/checks.js +52 -0
  342. package/packages/ir/src/index.js +32 -0
  343. package/packages/ir/src/ir.js +214 -0
  344. package/packages/ir/src/pages.js +18 -0
  345. package/packages/ir/src/validate-ir.js +462 -0
  346. package/packages/ir/src/validate-types.js +140 -0
  347. package/packages/kit/src/block-parser.js +196 -0
  348. package/packages/kit/src/index.js +15 -0
  349. package/packages/kit/src/kit-check.js +203 -0
  350. package/packages/kit/src/sources.js +53 -0
  351. package/packages/kit/src/version-policy.js +133 -0
  352. package/packages/kit/src/version-range.js +132 -0
  353. package/packages/openapi/src/import.js +292 -0
  354. package/packages/openapi/src/index.js +1 -0
  355. package/packages/openapi/src/names.js +50 -0
  356. package/packages/openapi/src/schemas.js +264 -0
  357. package/packages/packager/src/archive.js +106 -0
  358. package/packages/packager/src/crate.js +39 -0
  359. package/packages/packager/src/gomodule.js +11 -0
  360. package/packages/packager/src/index.js +9 -0
  361. package/packages/packager/src/maven.js +85 -0
  362. package/packages/packager/src/metadata.js +34 -0
  363. package/packages/packager/src/npm.js +71 -0
  364. package/packages/packager/src/nuget.js +78 -0
  365. package/packages/packager/src/sbom.js +56 -0
  366. package/packages/packager/src/wheel.js +62 -0
  367. package/packages/spec/src/index.js +11 -0
  368. package/packages/spec/src/requirements.js +139 -0
  369. package/packages/transpile/src/ast.js +51 -0
  370. package/packages/transpile/src/check.js +669 -0
  371. package/packages/transpile/src/emit-csharp.js +469 -0
  372. package/packages/transpile/src/emit-go.js +529 -0
  373. package/packages/transpile/src/emit-java.js +360 -0
  374. package/packages/transpile/src/emit-python.js +320 -0
  375. package/packages/transpile/src/emit-rust.js +499 -0
  376. package/packages/transpile/src/errors.js +15 -0
  377. package/packages/transpile/src/index.js +13 -0
  378. package/packages/transpile/src/lexer.js +161 -0
  379. package/packages/transpile/src/parser.js +543 -0
  380. package/packages/transpile/src/tests/ast.js +46 -0
  381. package/packages/transpile/src/tests/parse.js +376 -0
  382. package/packages/transpile/src/tests/translate.js +849 -0
  383. package/packages/transpile/src/transpile.js +57 -0
@@ -0,0 +1,248 @@
1
+ // sdkloom Rust kit: HTTP request and response values, URL building.
2
+
3
+ use super::errors::Error;
4
+ use super::json::Json;
5
+ use super::lang::js_str;
6
+ use std::fmt;
7
+ use std::io::Read;
8
+ use std::sync::{Arc, Mutex};
9
+
10
+ // @block http.request
11
+ // @summary An HTTP request value whose header names are matched case-insensitively; its with_ methods return changed copies.
12
+ // @versions >=1.70
13
+ // @source [1] https://www.rfc-editor.org/rfc/rfc9110#section-5.1
14
+ // @quote [1] "Field names are case-insensitive"
15
+ // @retrieved 2026-12-05
16
+ // @tests tests/http.rs
17
+ // @requirement SDKL-HTTP-001
18
+
19
+ fn find_header(headers: &[(String, String)], name: &str) -> Option<String> {
20
+ headers.iter().find(|(key, _)| key.eq_ignore_ascii_case(name)).map(|(_, value)| value.clone())
21
+ }
22
+
23
+ fn without_header(headers: &[(String, String)], name: &str) -> Vec<(String, String)> {
24
+ headers.iter().filter(|(key, _)| !key.eq_ignore_ascii_case(name)).cloned().collect()
25
+ }
26
+
27
+ /// An HTTP request. Headers keep their order; with_header replaces any value the header had.
28
+ #[derive(Clone, Debug, PartialEq)]
29
+ pub struct Request {
30
+ pub method: String,
31
+ pub url: String,
32
+ pub headers: Vec<(String, String)>,
33
+ pub body: Option<Vec<u8>>,
34
+ }
35
+
36
+ impl Request {
37
+ pub fn new(method: &str, url: &str) -> Request {
38
+ Request { method: method.to_string(), url: url.to_string(), headers: Vec::new(), body: None }
39
+ }
40
+
41
+ pub fn header(&self, name: &str) -> Option<String> {
42
+ find_header(&self.headers, name)
43
+ }
44
+
45
+ pub fn with_header(&self, name: &str, value: &str) -> Request {
46
+ let mut headers = without_header(&self.headers, name);
47
+ headers.push((name.to_string(), value.to_string()));
48
+ Request { headers, ..self.clone() }
49
+ }
50
+
51
+ pub fn without_header(&self, name: &str) -> Request {
52
+ Request { headers: without_header(&self.headers, name), ..self.clone() }
53
+ }
54
+
55
+ pub fn with_url(&self, url: &str) -> Request {
56
+ Request { url: url.to_string(), ..self.clone() }
57
+ }
58
+
59
+ pub fn with_body(&self, body: Option<Vec<u8>>) -> Request {
60
+ Request { body, ..self.clone() }
61
+ }
62
+
63
+ /// The body as UTF-8 text (invalid bytes become U+FFFD), "" without one.
64
+ pub fn text(&self) -> String {
65
+ self.body.as_deref().map(|body| String::from_utf8_lossy(body).into_owned()).unwrap_or_default()
66
+ }
67
+ }
68
+
69
+ // @end
70
+
71
+ // @block http.response
72
+ // @summary A received HTTP response whose body can be read as bytes, text or JSON any number of times, or as a stream as it arrives when the transport streamed it.
73
+ // @versions >=1.70
74
+ // @source [1] https://doc.rust-lang.org/std/string/struct.String.html#method.from_utf8_lossy
75
+ // @quote [1] "Converts a slice of bytes to a string, including invalid characters."
76
+ // @retrieved 2026-12-05
77
+ // @tests tests/http.rs
78
+ // @requirement SDKL-HTTP-003
79
+
80
+ /// A body read as it arrives, shared by the copies of its response: whoever reads first gets the bytes.
81
+ #[derive(Clone)]
82
+ pub struct BodyStream(Arc<Mutex<Box<dyn Read + Send>>>);
83
+
84
+ impl BodyStream {
85
+ pub fn new(reader: impl Read + Send + 'static) -> BodyStream {
86
+ BodyStream(Arc::new(Mutex::new(Box::new(reader))))
87
+ }
88
+ }
89
+
90
+ impl Read for BodyStream {
91
+ fn read(&mut self, buffer: &mut [u8]) -> std::io::Result<usize> {
92
+ self.0.lock().unwrap_or_else(|poisoned| poisoned.into_inner()).read(buffer)
93
+ }
94
+ }
95
+
96
+ impl fmt::Debug for BodyStream {
97
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98
+ f.write_str("BodyStream")
99
+ }
100
+ }
101
+
102
+ impl PartialEq for BodyStream {
103
+ fn eq(&self, other: &BodyStream) -> bool {
104
+ Arc::ptr_eq(&self.0, &other.0)
105
+ }
106
+ }
107
+
108
+ /// A received HTTP response; body holds all of it, except for a streamed response (an event stream), whose body is
109
+ /// empty and whose bytes are read from stream as they arrive.
110
+ #[derive(Clone, Debug, PartialEq)]
111
+ pub struct Response {
112
+ pub status: u16,
113
+ pub headers: Vec<(String, String)>,
114
+ pub body: Vec<u8>,
115
+ pub stream: Option<BodyStream>,
116
+ }
117
+
118
+ impl Response {
119
+ pub fn new(status: u16, headers: Vec<(String, String)>, body: Vec<u8>) -> Response {
120
+ Response { status, headers, body, stream: None }
121
+ }
122
+
123
+ /// The body as it arrives: the stream of a streamed response, else the buffered body.
124
+ pub fn body_reader(&self) -> BodyStream {
125
+ match &self.stream {
126
+ Some(stream) => stream.clone(),
127
+ None => BodyStream::new(std::io::Cursor::new(self.body.clone())),
128
+ }
129
+ }
130
+
131
+ pub fn header(&self, name: &str) -> Option<String> {
132
+ find_header(&self.headers, name)
133
+ }
134
+
135
+ pub fn text(&self) -> String {
136
+ String::from_utf8_lossy(&self.body).into_owned()
137
+ }
138
+
139
+ /// The body as JSON; an empty body is null.
140
+ pub fn json(&self) -> Result<Json, Error> {
141
+ let text = self.text();
142
+ if text.trim().is_empty() {
143
+ return Ok(Json::Null);
144
+ }
145
+ Json::parse(&text).map_err(Error::thrown)
146
+ }
147
+ }
148
+
149
+ // @end
150
+
151
+ // @block http.url
152
+ // @summary Builds a request URL: percent-encodes path parameters like encodeURIComponent and appends query parameters in order as browsers encode forms, like the TypeScript kit. A path parameter of "." or ".." is refused with an ArgumentError: URL parsers read it as a step up the path, even percent-encoded. A {+name} placeholder (RFC 6570 reserved expansion, limited to "/") takes a value that spans segments, each encoded on its own.
153
+ // @versions >=1.70
154
+ // @source [1] https://www.rfc-editor.org/rfc/rfc3986#section-2.1
155
+ // @quote [1] "A percent-encoding mechanism is used to represent a data octet in a component when that octet's corresponding character is outside the allowed set or is being used as a delimiter of, or within, the component."
156
+ // @source [2] https://url.spec.whatwg.org/#application-x-www-form-urlencoded-percent-encode-set
157
+ // @quote [2] "The application/x-www-form-urlencoded percent-encode set contains all code points, except the ASCII alphanumeric, U+002A (*), U+002D (-), U+002E (.), and U+005F (_)."
158
+ // @source [3] https://www.rfc-editor.org/rfc/rfc3986#section-3.3
159
+ // @quote [3] "also known as dot-segments, are defined for relative reference within the path name hierarchy."
160
+ // @source [4] https://www.rfc-editor.org/rfc/rfc6570#section-3.2.3
161
+ // @quote [4] "is identical to simple string expansion except that the substituted values may also contain pct-encoded triplets and characters in the reserved set."
162
+ // @retrieved 2026-12-05
163
+ // @tests tests/http.rs
164
+ // @requirement SDKL-HTTP-002
165
+
166
+ fn percent(text: &str, keep: impl Fn(u8) -> bool, space_as_plus: bool) -> String {
167
+ let mut out = String::new();
168
+ for byte in text.bytes() {
169
+ if byte == b' ' && space_as_plus {
170
+ out.push('+');
171
+ } else if byte.is_ascii_alphanumeric() || keep(byte) {
172
+ out.push(byte as char);
173
+ } else {
174
+ out.push_str(&format!("%{:02X}", byte));
175
+ }
176
+ }
177
+ out
178
+ }
179
+
180
+ /// encodeURIComponent: letters, digits and - _ . ! ~ * ' ( ) stay.
181
+ pub fn path_segment(text: &str) -> String {
182
+ percent(text, |b| b"-_.!~*'()".contains(&b), false)
183
+ }
184
+
185
+ /// A path parameter as one encoded segment. "." and ".." are refused: URL parsers treat them, even percent-encoded, as
186
+ /// steps up the path, so the request would reach another resource.
187
+ pub fn path_parameter(name: &str, text: &str) -> Result<String, Error> {
188
+ if text == "." || text == ".." {
189
+ return Err(Error::Argument(format!("path parameter {} cannot be \"{}\": a URL would read it as a step up the path", name, text)));
190
+ }
191
+ Ok(path_segment(text))
192
+ }
193
+
194
+ /// application/x-www-form-urlencoded: space is "+", "*" - . _ stay, everything else is encoded.
195
+ pub fn form(text: &str) -> String {
196
+ percent(text, |b| b"*-._".contains(&b), true)
197
+ }
198
+
199
+ /// A parameter value as text, like the other kits: true/false, 2 (not 2.0), 1e-7.
200
+ pub fn scalar(value: &Json) -> String {
201
+ js_str(value)
202
+ }
203
+
204
+ /// Joins base and template, fills {name} and {+name} from path (absent names fail) and appends the query; Null values are left out
205
+ /// and arrays repeat the name.
206
+ pub fn build_url(base: &str, template: &str, path: &[(&str, Json)], query: &[(&str, Json)]) -> Result<String, Error> {
207
+ let mut filled = String::new();
208
+ let mut rest = template;
209
+ while let Some(start) = rest.find('{') {
210
+ let end = rest[start..].find('}').map(|offset| start + offset).ok_or_else(|| Error::thrown(format!("invalid path template {}", template)))?;
211
+ filled.push_str(&rest[..start]);
212
+ // {+name} (RFC 6570 reserved expansion, limited to "/"): the value may span segments, each encoded on its own.
213
+ let placeholder = &rest[start + 1..end];
214
+ let (reserved, name) = match placeholder.strip_prefix('+') {
215
+ Some(name) => (true, name),
216
+ None => (false, placeholder),
217
+ };
218
+ let value = path.iter().find(|(key, _)| *key == name).map(|(_, value)| value).ok_or_else(|| Error::Argument(format!("missing path parameter {}", name)))?;
219
+ let text = scalar(value);
220
+ if reserved {
221
+ let segments: Result<Vec<String>, Error> = text.split('/').map(|segment| path_parameter(name, segment)).collect();
222
+ filled.push_str(&segments?.join("/"));
223
+ } else {
224
+ filled.push_str(&path_parameter(name, &text)?);
225
+ }
226
+ rest = &rest[end + 1..];
227
+ }
228
+ filled.push_str(rest);
229
+ let mut pairs = Vec::new();
230
+ for (name, value) in query {
231
+ let items = match value {
232
+ Json::Null => Vec::new(),
233
+ Json::Array(items) => items.iter().collect(),
234
+ other => vec![other],
235
+ };
236
+ for item in items {
237
+ pairs.push(format!("{}={}", form(name), form(&scalar(item))));
238
+ }
239
+ }
240
+ let mut url = format!("{}{}", base.trim_end_matches('/'), filled);
241
+ if !pairs.is_empty() {
242
+ url.push('?');
243
+ url.push_str(&pairs.join("&"));
244
+ }
245
+ Ok(url)
246
+ }
247
+
248
+ // @end
@@ -0,0 +1,424 @@
1
+ // sdkloom Rust kit: JSON values, parsing and writing. Rust's standard library has no JSON, so the kit has its own.
2
+
3
+ use std::fmt::Write as _;
4
+
5
+ // @block json.value
6
+ // @summary A JSON value that keeps numbers exactly as written and object fields in order, with a strict parser and a compact writer that prints numbers like JavaScript.
7
+ // @versions >=1.70
8
+ // @source [1] https://www.rfc-editor.org/rfc/rfc8259#section-4
9
+ // @quote [1] "An object structure is represented as a pair of curly brackets surrounding zero or more name/value pairs (or members)."
10
+ // @source [2] https://doc.rust-lang.org/std/fmt/trait.LowerExp.html
11
+ // @quote [2] "e formatting."
12
+ // @retrieved 2026-12-05
13
+ // @tests tests/json.rs
14
+ // @requirement SDKL-JSON-001
15
+ // @requirement SDKL-JSON-004
16
+
17
+ /// A JSON value. Numbers keep their text so 64-bit integers and decimals stay exact; objects keep field order.
18
+ #[derive(Clone, Debug, PartialEq)]
19
+ pub enum Json {
20
+ Null,
21
+ Bool(bool),
22
+ Number(String),
23
+ String(String),
24
+ Array(Vec<Json>),
25
+ Object(Vec<(String, Json)>),
26
+ }
27
+
28
+ impl Json {
29
+ /// An integer value.
30
+ pub fn int(value: i64) -> Json {
31
+ Json::Number(value.to_string())
32
+ }
33
+
34
+ /// A number written like JavaScript's String(number); NaN and infinities become null.
35
+ pub fn float(value: f64) -> Json {
36
+ if value.is_finite() {
37
+ Json::Number(number_string(value))
38
+ } else {
39
+ Json::Null
40
+ }
41
+ }
42
+
43
+ pub fn str(value: &str) -> Json {
44
+ Json::String(value.to_string())
45
+ }
46
+
47
+ /// An object from (name, value) pairs, in order.
48
+ pub fn object(fields: Vec<(&str, Json)>) -> Json {
49
+ Json::Object(fields.into_iter().map(|(name, value)| (name.to_string(), value)).collect())
50
+ }
51
+
52
+ /// The value of an object field; None for a missing field or a non-object.
53
+ pub fn get(&self, name: &str) -> Option<&Json> {
54
+ match self {
55
+ Json::Object(fields) => fields.iter().rev().find(|(key, _)| key == name).map(|(_, value)| value),
56
+ _ => None,
57
+ }
58
+ }
59
+
60
+ /// Sets an object field, replacing an existing one.
61
+ pub fn set(&mut self, name: &str, value: Json) {
62
+ if let Json::Object(fields) = self {
63
+ if let Some(entry) = fields.iter_mut().find(|(key, _)| key == name) {
64
+ entry.1 = value;
65
+ } else {
66
+ fields.push((name.to_string(), value));
67
+ }
68
+ }
69
+ }
70
+
71
+ pub fn is_null(&self) -> bool {
72
+ matches!(self, Json::Null)
73
+ }
74
+
75
+ pub fn as_str(&self) -> Option<&str> {
76
+ match self {
77
+ Json::String(text) => Some(text),
78
+ _ => None,
79
+ }
80
+ }
81
+
82
+ pub fn as_f64(&self) -> Option<f64> {
83
+ match self {
84
+ Json::Number(text) => text.parse::<f64>().ok(),
85
+ _ => None,
86
+ }
87
+ }
88
+
89
+ /// The value as a 64-bit integer: exact digits, or a float without a fraction (2.0).
90
+ pub fn as_i64(&self) -> Option<i64> {
91
+ match self {
92
+ Json::Number(text) => text.parse::<i64>().ok().or_else(|| {
93
+ let value = text.parse::<f64>().ok()?;
94
+ if value.fract() == 0.0 && value.abs() < 9.223_372_036_854_776e18 {
95
+ Some(value as i64)
96
+ } else {
97
+ None
98
+ }
99
+ }),
100
+ _ => None,
101
+ }
102
+ }
103
+
104
+ pub fn as_bool(&self) -> Option<bool> {
105
+ match self {
106
+ Json::Bool(flag) => Some(*flag),
107
+ _ => None,
108
+ }
109
+ }
110
+
111
+ /// Compact JSON text: no spaces, characters outside ASCII as they are, numbers as they were read.
112
+ pub fn to_json_string(&self) -> String {
113
+ let mut out = String::new();
114
+ write_value(&mut out, self);
115
+ out
116
+ }
117
+
118
+ /// Parses JSON text; the whole input must be one value.
119
+ pub fn parse(text: &str) -> Result<Json, String> {
120
+ let mut parser = Parser { bytes: text.as_bytes(), at: 0 };
121
+ parser.space();
122
+ let value = parser.value(0)?;
123
+ parser.space();
124
+ if parser.at != parser.bytes.len() {
125
+ return Err(format!("invalid JSON: unexpected text at {}", parser.at));
126
+ }
127
+ Ok(value)
128
+ }
129
+ }
130
+
131
+ /// String(number) as JavaScript writes it: shortest digits, plain between 1e-7 and 1e21, exponent form otherwise.
132
+ pub fn number_string(value: f64) -> String {
133
+ if value.is_nan() {
134
+ return "NaN".to_string();
135
+ }
136
+ if value.is_infinite() {
137
+ return if value > 0.0 { "Infinity" } else { "-Infinity" }.to_string();
138
+ }
139
+ if value == 0.0 {
140
+ return "0".to_string();
141
+ }
142
+ let sign = if value < 0.0 { "-" } else { "" };
143
+ let magnitude = value.abs();
144
+ // {:e} gives the shortest digits that read back as the same value, e.g. "1.2345e-7".
145
+ let scientific = format!("{:e}", magnitude);
146
+ let (mantissa, exponent) = scientific.split_once('e').unwrap_or((&scientific, "0"));
147
+ let digits: String = mantissa.chars().filter(|c| *c != '.').collect();
148
+ let exponent: i32 = exponent.parse().unwrap_or(0);
149
+ if magnitude >= 1e21 || magnitude < 1e-6 {
150
+ let mut text = String::from(&digits[..1]);
151
+ if digits.len() > 1 {
152
+ text.push('.');
153
+ text.push_str(&digits[1..]);
154
+ }
155
+ return format!("{}{}e{}{}", sign, text, if exponent >= 0 { "+" } else { "-" }, exponent.abs());
156
+ }
157
+ let point = exponent + 1;
158
+ if point <= 0 {
159
+ return format!("{}0.{}{}", sign, "0".repeat((-point) as usize), digits);
160
+ }
161
+ let point = point as usize;
162
+ if point >= digits.len() {
163
+ return format!("{}{}{}", sign, digits, "0".repeat(point - digits.len()));
164
+ }
165
+ format!("{}{}.{}", sign, &digits[..point], &digits[point..])
166
+ }
167
+
168
+ fn write_string(out: &mut String, text: &str) {
169
+ out.push('"');
170
+ for c in text.chars() {
171
+ match c {
172
+ '"' => out.push_str("\\\""),
173
+ '\\' => out.push_str("\\\\"),
174
+ '\n' => out.push_str("\\n"),
175
+ '\r' => out.push_str("\\r"),
176
+ '\t' => out.push_str("\\t"),
177
+ '\u{8}' => out.push_str("\\b"),
178
+ '\u{c}' => out.push_str("\\f"),
179
+ c if (c as u32) < 0x20 => {
180
+ let _ = write!(out, "\\u{:04x}", c as u32);
181
+ }
182
+ c => out.push(c),
183
+ }
184
+ }
185
+ out.push('"');
186
+ }
187
+
188
+ fn write_value(out: &mut String, value: &Json) {
189
+ match value {
190
+ Json::Null => out.push_str("null"),
191
+ Json::Bool(flag) => out.push_str(if *flag { "true" } else { "false" }),
192
+ // JSON.stringify writes NaN and the infinities (kept as text for String()) as null.
193
+ Json::Number(text) if matches!(text.as_str(), "NaN" | "Infinity" | "-Infinity") => out.push_str("null"),
194
+ Json::Number(text) => out.push_str(text),
195
+ Json::String(text) => write_string(out, text),
196
+ Json::Array(items) => {
197
+ out.push('[');
198
+ for (i, item) in items.iter().enumerate() {
199
+ if i > 0 {
200
+ out.push(',');
201
+ }
202
+ write_value(out, item);
203
+ }
204
+ out.push(']');
205
+ }
206
+ Json::Object(fields) => {
207
+ out.push('{');
208
+ for (i, (key, item)) in fields.iter().enumerate() {
209
+ if i > 0 {
210
+ out.push(',');
211
+ }
212
+ write_string(out, key);
213
+ out.push(':');
214
+ write_value(out, item);
215
+ }
216
+ out.push('}');
217
+ }
218
+ }
219
+ }
220
+
221
+ const MAX_DEPTH: usize = 512;
222
+
223
+ struct Parser<'a> {
224
+ bytes: &'a [u8],
225
+ at: usize,
226
+ }
227
+
228
+ impl<'a> Parser<'a> {
229
+ fn space(&mut self) {
230
+ while self.at < self.bytes.len() && matches!(self.bytes[self.at], b' ' | b'\t' | b'\n' | b'\r') {
231
+ self.at += 1;
232
+ }
233
+ }
234
+
235
+ fn fail<T>(&self, what: &str) -> Result<T, String> {
236
+ Err(format!("invalid JSON: {} at {}", what, self.at))
237
+ }
238
+
239
+ fn literal(&mut self, word: &str, value: Json) -> Result<Json, String> {
240
+ if self.bytes[self.at..].starts_with(word.as_bytes()) {
241
+ self.at += word.len();
242
+ Ok(value)
243
+ } else {
244
+ self.fail("unexpected character")
245
+ }
246
+ }
247
+
248
+ fn value(&mut self, depth: usize) -> Result<Json, String> {
249
+ if depth > MAX_DEPTH {
250
+ return self.fail("too deeply nested");
251
+ }
252
+ match self.bytes.get(self.at) {
253
+ None => self.fail("unexpected end"),
254
+ Some(b'n') => self.literal("null", Json::Null),
255
+ Some(b't') => self.literal("true", Json::Bool(true)),
256
+ Some(b'f') => self.literal("false", Json::Bool(false)),
257
+ Some(b'"') => Ok(Json::String(self.string()?)),
258
+ Some(b'[') => self.array(depth),
259
+ Some(b'{') => self.object(depth),
260
+ Some(_) => self.number(),
261
+ }
262
+ }
263
+
264
+ fn array(&mut self, depth: usize) -> Result<Json, String> {
265
+ self.at += 1;
266
+ let mut items = Vec::new();
267
+ self.space();
268
+ if self.bytes.get(self.at) == Some(&b']') {
269
+ self.at += 1;
270
+ return Ok(Json::Array(items));
271
+ }
272
+ loop {
273
+ self.space();
274
+ items.push(self.value(depth + 1)?);
275
+ self.space();
276
+ match self.bytes.get(self.at) {
277
+ Some(b',') => self.at += 1,
278
+ Some(b']') => {
279
+ self.at += 1;
280
+ return Ok(Json::Array(items));
281
+ }
282
+ _ => return self.fail("expected , or ]"),
283
+ }
284
+ }
285
+ }
286
+
287
+ fn object(&mut self, depth: usize) -> Result<Json, String> {
288
+ self.at += 1;
289
+ let mut fields: Vec<Option<(String, Json)>> = Vec::new();
290
+ let mut seen: std::collections::HashMap<String, usize> = std::collections::HashMap::new();
291
+ self.space();
292
+ if self.bytes.get(self.at) == Some(&b'}') {
293
+ self.at += 1;
294
+ return Ok(Json::Object(Vec::new()));
295
+ }
296
+ loop {
297
+ self.space();
298
+ if self.bytes.get(self.at) != Some(&b'"') {
299
+ return self.fail("expected a field name");
300
+ }
301
+ let key = self.string()?;
302
+ self.space();
303
+ if self.bytes.get(self.at) != Some(&b':') {
304
+ return self.fail("expected :");
305
+ }
306
+ self.at += 1;
307
+ self.space();
308
+ let value = self.value(depth + 1)?;
309
+ // A later duplicate replaces the earlier one and moves to the end; the index keeps this linear.
310
+ if let Some(index) = seen.insert(key.clone(), fields.len()) {
311
+ fields[index] = None;
312
+ }
313
+ fields.push(Some((key, value)));
314
+ self.space();
315
+ match self.bytes.get(self.at) {
316
+ Some(b',') => self.at += 1,
317
+ Some(b'}') => {
318
+ self.at += 1;
319
+ return Ok(Json::Object(fields.into_iter().flatten().collect()));
320
+ }
321
+ _ => return self.fail("expected , or }"),
322
+ }
323
+ }
324
+ }
325
+
326
+ fn number(&mut self) -> Result<Json, String> {
327
+ let start = self.at;
328
+ if self.bytes.get(self.at) == Some(&b'-') {
329
+ self.at += 1;
330
+ }
331
+ let digits = |parser: &mut Parser| {
332
+ let begin = parser.at;
333
+ while parser.at < parser.bytes.len() && parser.bytes[parser.at].is_ascii_digit() {
334
+ parser.at += 1;
335
+ }
336
+ parser.at - begin
337
+ };
338
+ let whole_start = self.at;
339
+ let whole = digits(self);
340
+ if whole == 0 || (whole > 1 && self.bytes[whole_start] == b'0') {
341
+ return self.fail("invalid number");
342
+ }
343
+ if self.bytes.get(self.at) == Some(&b'.') {
344
+ self.at += 1;
345
+ if digits(self) == 0 {
346
+ return self.fail("invalid number");
347
+ }
348
+ }
349
+ if matches!(self.bytes.get(self.at), Some(b'e') | Some(b'E')) {
350
+ self.at += 1;
351
+ if matches!(self.bytes.get(self.at), Some(b'+') | Some(b'-')) {
352
+ self.at += 1;
353
+ }
354
+ if digits(self) == 0 {
355
+ return self.fail("invalid number");
356
+ }
357
+ }
358
+ Ok(Json::Number(String::from_utf8_lossy(&self.bytes[start..self.at]).into_owned()))
359
+ }
360
+
361
+ fn hex(&mut self) -> Result<u32, String> {
362
+ let text = self.bytes.get(self.at..self.at + 4).ok_or_else(|| "invalid JSON: short \\u escape".to_string())?;
363
+ if !text.iter().all(u8::is_ascii_hexdigit) {
364
+ return Err("invalid JSON: bad \\u escape".to_string());
365
+ }
366
+ let value = u32::from_str_radix(&String::from_utf8_lossy(text), 16).map_err(|_| "invalid JSON: bad \\u escape".to_string())?;
367
+ self.at += 4;
368
+ Ok(value)
369
+ }
370
+
371
+ fn string(&mut self) -> Result<String, String> {
372
+ self.at += 1;
373
+ let mut out = String::new();
374
+ loop {
375
+ let start = self.at;
376
+ while self.at < self.bytes.len() && self.bytes[self.at] != b'"' && self.bytes[self.at] != b'\\' && self.bytes[self.at] >= 0x20 {
377
+ self.at += 1;
378
+ }
379
+ out.push_str(std::str::from_utf8(&self.bytes[start..self.at]).map_err(|_| "invalid JSON: not UTF-8".to_string())?);
380
+ match self.bytes.get(self.at) {
381
+ Some(b'"') => {
382
+ self.at += 1;
383
+ return Ok(out);
384
+ }
385
+ Some(b'\\') => {
386
+ self.at += 1;
387
+ let escape = *self.bytes.get(self.at).ok_or_else(|| "invalid JSON: unexpected end".to_string())?;
388
+ self.at += 1;
389
+ match escape {
390
+ b'"' => out.push('"'),
391
+ b'\\' => out.push('\\'),
392
+ b'/' => out.push('/'),
393
+ b'b' => out.push('\u{8}'),
394
+ b'f' => out.push('\u{c}'),
395
+ b'n' => out.push('\n'),
396
+ b'r' => out.push('\r'),
397
+ b't' => out.push('\t'),
398
+ b'u' => {
399
+ let high = self.hex()?;
400
+ let mut code = high;
401
+ // A high surrogate combines only with a following low one; otherwise it is U+FFFD and
402
+ // the next escape is read on its own.
403
+ if (0xD800..0xDC00).contains(&high) && self.bytes[self.at..].starts_with(b"\\u") {
404
+ let saved = self.at;
405
+ self.at += 2;
406
+ let low = self.hex()?;
407
+ if (0xDC00..0xE000).contains(&low) {
408
+ code = 0x10000 + ((high - 0xD800) << 10) + (low - 0xDC00);
409
+ } else {
410
+ self.at = saved;
411
+ }
412
+ }
413
+ out.push(char::from_u32(code).unwrap_or('\u{FFFD}'));
414
+ }
415
+ _ => return self.fail("bad escape"),
416
+ }
417
+ }
418
+ _ => return self.fail("unterminated string"),
419
+ }
420
+ }
421
+ }
422
+ }
423
+
424
+ // @end