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.
- package/LICENSE +202 -0
- package/README.md +239 -2
- package/kits/allowed-sources.json +24 -0
- package/kits/csharp/kit.json +6 -0
- package/kits/csharp/src/Runtime/Client.cs +295 -0
- package/kits/csharp/src/Runtime/Context.cs +216 -0
- package/kits/csharp/src/Runtime/Errors.cs +121 -0
- package/kits/csharp/src/Runtime/Http.cs +218 -0
- package/kits/csharp/src/Runtime/Json.cs +650 -0
- package/kits/csharp/src/Runtime/Lang.cs +212 -0
- package/kits/csharp/src/Runtime/Models.cs +206 -0
- package/kits/csharp/src/Runtime/Multipart.cs +108 -0
- package/kits/csharp/src/Runtime/Pagination.cs +122 -0
- package/kits/csharp/src/Runtime/Sse.cs +197 -0
- package/kits/csharp/src/Runtime/Testing.cs +375 -0
- package/kits/csharp/src/Runtime/Transport.cs +212 -0
- package/kits/csharp/src/Runtime/Wait.cs +57 -0
- package/kits/csharp/src/Runtime/Webhooks.cs +148 -0
- package/kits/csharp/tests/ClientTests.cs +128 -0
- package/kits/csharp/tests/HttpTests.cs +44 -0
- package/kits/csharp/tests/JsonTests.cs +132 -0
- package/kits/csharp/tests/LangTests.cs +57 -0
- package/kits/csharp/tests/MultipartTests.cs +31 -0
- package/kits/csharp/tests/PaginationTests.cs +93 -0
- package/kits/csharp/tests/SseTests.cs +122 -0
- package/kits/csharp/tests/TestRunner.cs +93 -0
- package/kits/csharp/tests/TestingTests.cs +80 -0
- package/kits/csharp/tests/TransportTests.cs +145 -0
- package/kits/csharp/tests/WaitTests.cs +39 -0
- package/kits/csharp/tests/WebhooksTests.cs +46 -0
- package/kits/csharp/versions.json +12 -0
- package/kits/go/kit.json +6 -0
- package/kits/go/src/sdkruntime/client.go +322 -0
- package/kits/go/src/sdkruntime/context.go +207 -0
- package/kits/go/src/sdkruntime/errors.go +74 -0
- package/kits/go/src/sdkruntime/http.go +261 -0
- package/kits/go/src/sdkruntime/json.go +527 -0
- package/kits/go/src/sdkruntime/lang.go +462 -0
- package/kits/go/src/sdkruntime/multipart.go +75 -0
- package/kits/go/src/sdkruntime/pagination.go +150 -0
- package/kits/go/src/sdkruntime/sse.go +267 -0
- package/kits/go/src/sdkruntime/testing.go +343 -0
- package/kits/go/src/sdkruntime/transport.go +168 -0
- package/kits/go/src/sdkruntime/wait.go +49 -0
- package/kits/go/src/sdkruntime/webhooks.go +129 -0
- package/kits/go/tests/client_test.go +185 -0
- package/kits/go/tests/http_test.go +78 -0
- package/kits/go/tests/json_test.go +127 -0
- package/kits/go/tests/lang_test.go +98 -0
- package/kits/go/tests/multipart_test.go +29 -0
- package/kits/go/tests/pagination_test.go +69 -0
- package/kits/go/tests/sse_test.go +129 -0
- package/kits/go/tests/testing_test.go +121 -0
- package/kits/go/tests/transport_test.go +133 -0
- package/kits/go/tests/wait_test.go +46 -0
- package/kits/go/tests/webhooks_test.go +62 -0
- package/kits/go/versions.json +12 -0
- package/kits/java/kit.json +6 -0
- package/kits/java/src/dev/sdkloom/runtime/ApiException.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/ArgumentException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Assert.java +72 -0
- package/kits/java/src/dev/sdkloom/runtime/ClientRuntime.java +220 -0
- package/kits/java/src/dev/sdkloom/runtime/Clock.java +39 -0
- package/kits/java/src/dev/sdkloom/runtime/Context.java +79 -0
- package/kits/java/src/dev/sdkloom/runtime/Decode.java +200 -0
- package/kits/java/src/dev/sdkloom/runtime/DecodeException.java +26 -0
- package/kits/java/src/dev/sdkloom/runtime/Decoder.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/Emitter.java +56 -0
- package/kits/java/src/dev/sdkloom/runtime/ErrorFactory.java +18 -0
- package/kits/java/src/dev/sdkloom/runtime/EventListener.java +20 -0
- package/kits/java/src/dev/sdkloom/runtime/EventStream.java +232 -0
- package/kits/java/src/dev/sdkloom/runtime/FakeClock.java +37 -0
- package/kits/java/src/dev/sdkloom/runtime/Headers.java +42 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpRequest.java +88 -0
- package/kits/java/src/dev/sdkloom/runtime/HttpResponse.java +69 -0
- package/kits/java/src/dev/sdkloom/runtime/JdkTransport.java +97 -0
- package/kits/java/src/dev/sdkloom/runtime/Js.java +230 -0
- package/kits/java/src/dev/sdkloom/runtime/Json.java +281 -0
- package/kits/java/src/dev/sdkloom/runtime/JsonValue.java +15 -0
- package/kits/java/src/dev/sdkloom/runtime/Links.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/Middleware.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/MiddlewareEntry.java +43 -0
- package/kits/java/src/dev/sdkloom/runtime/MockRoute.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/MockTransport.java +62 -0
- package/kits/java/src/dev/sdkloom/runtime/MultipartForm.java +111 -0
- package/kits/java/src/dev/sdkloom/runtime/Next.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationInfo.java +35 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationMock.java +105 -0
- package/kits/java/src/dev/sdkloom/runtime/OperationSpec.java +125 -0
- package/kits/java/src/dev/sdkloom/runtime/Pages.java +55 -0
- package/kits/java/src/dev/sdkloom/runtime/Pipeline.java +29 -0
- package/kits/java/src/dev/sdkloom/runtime/RawResult.java +31 -0
- package/kits/java/src/dev/sdkloom/runtime/RecordedEvent.java +36 -0
- package/kits/java/src/dev/sdkloom/runtime/SdkInfo.java +38 -0
- package/kits/java/src/dev/sdkloom/runtime/SendOptions.java +34 -0
- package/kits/java/src/dev/sdkloom/runtime/ServerSentEvent.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/State.java +58 -0
- package/kits/java/src/dev/sdkloom/runtime/Test.java +21 -0
- package/kits/java/src/dev/sdkloom/runtime/TestRunner.java +63 -0
- package/kits/java/src/dev/sdkloom/runtime/Transport.java +16 -0
- package/kits/java/src/dev/sdkloom/runtime/TransportException.java +30 -0
- package/kits/java/src/dev/sdkloom/runtime/UrlConnectionTransport.java +195 -0
- package/kits/java/src/dev/sdkloom/runtime/Urls.java +116 -0
- package/kits/java/src/dev/sdkloom/runtime/Waits.java +53 -0
- package/kits/java/src/dev/sdkloom/runtime/WebhookSignatureException.java +24 -0
- package/kits/java/src/dev/sdkloom/runtime/Webhooks.java +146 -0
- package/kits/java/tests/dev/sdkloom/runtime/ClientTest.java +266 -0
- package/kits/java/tests/dev/sdkloom/runtime/EventStreamTest.java +131 -0
- package/kits/java/tests/dev/sdkloom/runtime/HttpTest.java +95 -0
- package/kits/java/tests/dev/sdkloom/runtime/JdkTransportTest.java +12 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsTest.java +102 -0
- package/kits/java/tests/dev/sdkloom/runtime/JsonTest.java +87 -0
- package/kits/java/tests/dev/sdkloom/runtime/LinksTest.java +22 -0
- package/kits/java/tests/dev/sdkloom/runtime/MultipartFormTest.java +30 -0
- package/kits/java/tests/dev/sdkloom/runtime/PagesTest.java +75 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestData.java +53 -0
- package/kits/java/tests/dev/sdkloom/runtime/TestingTest.java +156 -0
- package/kits/java/tests/dev/sdkloom/runtime/TransportContract.java +201 -0
- package/kits/java/tests/dev/sdkloom/runtime/UrlConnectionTransportTest.java +49 -0
- package/kits/java/tests/dev/sdkloom/runtime/WaitsTest.java +43 -0
- package/kits/java/tests/dev/sdkloom/runtime/WebhooksTest.java +45 -0
- package/kits/java/versions.json +41 -0
- package/kits/python/kit.json +6 -0
- package/kits/python/src/sdkloom_runtime/__init__.py +1 -0
- package/kits/python/src/sdkloom_runtime/client.py +232 -0
- package/kits/python/src/sdkloom_runtime/compat.py +195 -0
- package/kits/python/src/sdkloom_runtime/context.py +134 -0
- package/kits/python/src/sdkloom_runtime/errors.py +65 -0
- package/kits/python/src/sdkloom_runtime/http.py +192 -0
- package/kits/python/src/sdkloom_runtime/lang.py +207 -0
- package/kits/python/src/sdkloom_runtime/models.py +221 -0
- package/kits/python/src/sdkloom_runtime/multipart.py +69 -0
- package/kits/python/src/sdkloom_runtime/pagination.py +61 -0
- package/kits/python/src/sdkloom_runtime/pipeline.py +39 -0
- package/kits/python/src/sdkloom_runtime/sse.py +121 -0
- package/kits/python/src/sdkloom_runtime/testing.py +224 -0
- package/kits/python/src/sdkloom_runtime/transport.py +120 -0
- package/kits/python/src/sdkloom_runtime/wait.py +44 -0
- package/kits/python/src/sdkloom_runtime/webhooks.py +84 -0
- package/kits/python/tests/support.py +28 -0
- package/kits/python/tests/test_client.py +275 -0
- package/kits/python/tests/test_compat.py +87 -0
- package/kits/python/tests/test_http.py +94 -0
- package/kits/python/tests/test_lang.py +104 -0
- package/kits/python/tests/test_models.py +110 -0
- package/kits/python/tests/test_multipart.py +37 -0
- package/kits/python/tests/test_pagination.py +70 -0
- package/kits/python/tests/test_sse.py +61 -0
- package/kits/python/tests/test_testing.py +130 -0
- package/kits/python/tests/test_transport.py +124 -0
- package/kits/python/tests/test_wait.py +46 -0
- package/kits/python/tests/test_webhooks.py +57 -0
- package/kits/python/versions.json +33 -0
- package/kits/rust/kit.json +6 -0
- package/kits/rust/src/runtime/client.rs +212 -0
- package/kits/rust/src/runtime/context.rs +182 -0
- package/kits/rust/src/runtime/crypto.rs +84 -0
- package/kits/rust/src/runtime/errors.rs +147 -0
- package/kits/rust/src/runtime/http.rs +248 -0
- package/kits/rust/src/runtime/json.rs +424 -0
- package/kits/rust/src/runtime/lang.rs +296 -0
- package/kits/rust/src/runtime/mod.rs +30 -0
- package/kits/rust/src/runtime/models.rs +266 -0
- package/kits/rust/src/runtime/multipart.rs +81 -0
- package/kits/rust/src/runtime/pagination.rs +117 -0
- package/kits/rust/src/runtime/sse.rs +195 -0
- package/kits/rust/src/runtime/testing.rs +308 -0
- package/kits/rust/src/runtime/transport.rs +324 -0
- package/kits/rust/src/runtime/wait.rs +29 -0
- package/kits/rust/src/runtime/webhooks.rs +142 -0
- package/kits/rust/tests/client.rs +152 -0
- package/kits/rust/tests/http.rs +44 -0
- package/kits/rust/tests/json.rs +82 -0
- package/kits/rust/tests/lang.rs +74 -0
- package/kits/rust/tests/multipart.rs +21 -0
- package/kits/rust/tests/pagination.rs +61 -0
- package/kits/rust/tests/sse.rs +77 -0
- package/kits/rust/tests/testing.rs +90 -0
- package/kits/rust/tests/transport.rs +218 -0
- package/kits/rust/tests/wait.rs +32 -0
- package/kits/rust/tests/webhooks.rs +73 -0
- package/kits/rust/versions.json +6 -0
- package/kits/typescript/kit.json +6 -0
- package/kits/typescript/src/client.ts +223 -0
- package/kits/typescript/src/context.ts +108 -0
- package/kits/typescript/src/errors.ts +63 -0
- package/kits/typescript/src/helpers.ts +49 -0
- package/kits/typescript/src/http.ts +192 -0
- package/kits/typescript/src/models.ts +139 -0
- package/kits/typescript/src/multipart.ts +66 -0
- package/kits/typescript/src/pagination.ts +54 -0
- package/kits/typescript/src/pipeline.ts +25 -0
- package/kits/typescript/src/sse.ts +102 -0
- package/kits/typescript/src/testing.ts +217 -0
- package/kits/typescript/src/transport.ts +101 -0
- package/kits/typescript/src/wait.ts +36 -0
- package/kits/typescript/src/webhooks.ts +168 -0
- package/kits/typescript/tests/client.test.ts +269 -0
- package/kits/typescript/tests/helpers.test.ts +27 -0
- package/kits/typescript/tests/http.test.ts +80 -0
- package/kits/typescript/tests/models.test.ts +71 -0
- package/kits/typescript/tests/multipart.test.ts +28 -0
- package/kits/typescript/tests/pagination.test.ts +76 -0
- package/kits/typescript/tests/sse.test.ts +82 -0
- package/kits/typescript/tests/testing.test.ts +103 -0
- package/kits/typescript/tests/transport.test.ts +127 -0
- package/kits/typescript/tests/wait.test.ts +31 -0
- package/kits/typescript/tests/webhooks.test.ts +53 -0
- package/kits/typescript/versions.json +15 -0
- package/package.json +37 -4
- package/packages/cli/recipes/apiKeyHeader.ts +16 -0
- package/packages/cli/recipes/apiVersionHeader.ts +16 -0
- package/packages/cli/recipes/basic.ts +13 -0
- package/packages/cli/recipes/bearer.ts +10 -0
- package/packages/cli/recipes/hmacSign.ts +23 -0
- package/packages/cli/recipes/idempotencyKey.ts +19 -0
- package/packages/cli/recipes/oauthClientCredentials.ts +50 -0
- package/packages/cli/recipes/retry.ts +42 -0
- package/packages/cli/recipes/timing.ts +12 -0
- package/packages/cli/recipes/userAgent.ts +8 -0
- package/packages/cli/src/build.js +93 -0
- package/packages/cli/src/commands/build.js +31 -0
- package/packages/cli/src/commands/conformance-check.js +50 -0
- package/packages/cli/src/commands/contract.js +181 -0
- package/packages/cli/src/commands/diff.js +113 -0
- package/packages/cli/src/commands/doctor.js +43 -0
- package/packages/cli/src/commands/import-openapi.js +42 -0
- package/packages/cli/src/commands/init.js +27 -0
- package/packages/cli/src/commands/kit-check.js +145 -0
- package/packages/cli/src/commands/publish.js +81 -0
- package/packages/cli/src/commands/recipe.js +49 -0
- package/packages/cli/src/commands/record.js +181 -0
- package/packages/cli/src/commands/serve.js +156 -0
- package/packages/cli/src/commands/spec-check.js +57 -0
- package/packages/cli/src/commands/test.js +177 -0
- package/packages/cli/src/commands/variants.js +32 -0
- package/packages/cli/src/commands/verify.js +57 -0
- package/packages/cli/src/commands/version.js +63 -0
- package/packages/cli/src/containers.js +82 -0
- package/packages/cli/src/coverage.js +72 -0
- package/packages/cli/src/git.js +25 -0
- package/packages/cli/src/io.js +50 -0
- package/packages/cli/src/java/ScenarioHarness.java +297 -0
- package/packages/cli/src/java/SchemaRoundTrip.java +63 -0
- package/packages/cli/src/java.js +70 -0
- package/packages/cli/src/js/harness.mjs +120 -0
- package/packages/cli/src/kits.js +103 -0
- package/packages/cli/src/languages/csharp.js +274 -0
- package/packages/cli/src/languages/go.js +185 -0
- package/packages/cli/src/languages/index.js +18 -0
- package/packages/cli/src/languages/java.js +187 -0
- package/packages/cli/src/languages/python.js +120 -0
- package/packages/cli/src/languages/rust.js +189 -0
- package/packages/cli/src/languages/types.js +93 -0
- package/packages/cli/src/languages/typescript.js +112 -0
- package/packages/cli/src/main.js +13 -0
- package/packages/cli/src/packaging.js +39 -0
- package/packages/cli/src/portability.js +45 -0
- package/packages/cli/src/profiles.js +55 -0
- package/packages/cli/src/project.js +56 -0
- package/packages/cli/src/python/scenario_harness.py +162 -0
- package/packages/cli/src/python/schema_roundtrip.py +42 -0
- package/packages/cli/src/registries.js +214 -0
- package/packages/cli/src/resolve-core.js +39 -0
- package/packages/cli/src/run.js +133 -0
- package/packages/cli/src/runtimes.js +61 -0
- package/packages/cli/src/scenarios-java.js +82 -0
- package/packages/cli/src/scenarios-javascript.js +54 -0
- package/packages/cli/src/scenarios-python.js +90 -0
- package/packages/cli/src/scenarios.js +183 -0
- package/packages/cli/src/schema.js +84 -0
- package/packages/cli/src/templates.js +85 -0
- package/packages/cli/src/toolchains.js +68 -0
- package/packages/cli/src/transpiled-tests.js +92 -0
- package/packages/codegen/src/canonical-json.js +31 -0
- package/packages/codegen/src/capabilities.js +50 -0
- package/packages/codegen/src/code-writer.js +54 -0
- package/packages/codegen/src/file-tree.js +25 -0
- package/packages/codegen/src/hash.js +6 -0
- package/packages/codegen/src/index.js +16 -0
- package/packages/codegen/src/lock.js +106 -0
- package/packages/conformance/src/case.js +143 -0
- package/packages/conformance/src/checks.js +40 -0
- package/packages/conformance/src/index.js +3 -0
- package/packages/conformance/src/matrix.js +48 -0
- package/packages/conformance/src/suite.js +48 -0
- package/packages/differ/src/diff.js +500 -0
- package/packages/differ/src/index.js +3 -0
- package/packages/differ/src/notes.js +112 -0
- package/packages/differ/src/support.js +34 -0
- package/packages/gen-csharp/src/emit-client.js +397 -0
- package/packages/gen-csharp/src/emit-harness.js +333 -0
- package/packages/gen-csharp/src/emit-mock.js +111 -0
- package/packages/gen-csharp/src/emit-models.js +119 -0
- package/packages/gen-csharp/src/emit-webhooks.js +57 -0
- package/packages/gen-csharp/src/generate.js +245 -0
- package/packages/gen-csharp/src/index.js +5 -0
- package/packages/gen-csharp/src/names.js +284 -0
- package/packages/gen-go/src/emit-client.js +405 -0
- package/packages/gen-go/src/emit-harness.js +294 -0
- package/packages/gen-go/src/emit-mock.js +112 -0
- package/packages/gen-go/src/emit-models.js +146 -0
- package/packages/gen-go/src/emit-webhooks.js +58 -0
- package/packages/gen-go/src/generate.js +205 -0
- package/packages/gen-go/src/index.js +5 -0
- package/packages/gen-go/src/names.js +197 -0
- package/packages/gen-java/src/emit-client.js +441 -0
- package/packages/gen-java/src/emit-models.js +342 -0
- package/packages/gen-java/src/emit-testing.js +123 -0
- package/packages/gen-java/src/emit-webhooks.js +70 -0
- package/packages/gen-java/src/generate.js +244 -0
- package/packages/gen-java/src/index.js +4 -0
- package/packages/gen-java/src/names.js +197 -0
- package/packages/gen-python/src/emit-client.js +272 -0
- package/packages/gen-python/src/emit-models.js +139 -0
- package/packages/gen-python/src/emit-testing.js +93 -0
- package/packages/gen-python/src/emit-webhooks.js +48 -0
- package/packages/gen-python/src/generate.js +237 -0
- package/packages/gen-python/src/index.js +2 -0
- package/packages/gen-python/src/names.js +161 -0
- package/packages/gen-python/src/style.js +67 -0
- package/packages/gen-rust/src/emit-client.js +379 -0
- package/packages/gen-rust/src/emit-harness.js +278 -0
- package/packages/gen-rust/src/emit-mock.js +121 -0
- package/packages/gen-rust/src/emit-models.js +117 -0
- package/packages/gen-rust/src/emit-webhooks.js +58 -0
- package/packages/gen-rust/src/generate.js +243 -0
- package/packages/gen-rust/src/index.js +5 -0
- package/packages/gen-rust/src/names.js +223 -0
- package/packages/gen-typescript/src/emit-client.js +434 -0
- package/packages/gen-typescript/src/emit-models.js +93 -0
- package/packages/gen-typescript/src/emit-package.js +90 -0
- package/packages/gen-typescript/src/emit-testing.js +138 -0
- package/packages/gen-typescript/src/emit-webhooks.js +52 -0
- package/packages/gen-typescript/src/generate.js +70 -0
- package/packages/gen-typescript/src/index.js +3 -0
- package/packages/gen-typescript/src/naming.js +64 -0
- package/packages/gen-typescript/src/runtime.js +81 -0
- package/packages/gen-typescript/src/types.js +136 -0
- package/packages/ir/src/api-surface.js +22 -0
- package/packages/ir/src/checks.js +52 -0
- package/packages/ir/src/index.js +32 -0
- package/packages/ir/src/ir.js +214 -0
- package/packages/ir/src/pages.js +18 -0
- package/packages/ir/src/validate-ir.js +462 -0
- package/packages/ir/src/validate-types.js +140 -0
- package/packages/kit/src/block-parser.js +196 -0
- package/packages/kit/src/index.js +15 -0
- package/packages/kit/src/kit-check.js +203 -0
- package/packages/kit/src/sources.js +53 -0
- package/packages/kit/src/version-policy.js +133 -0
- package/packages/kit/src/version-range.js +132 -0
- package/packages/openapi/src/import.js +292 -0
- package/packages/openapi/src/index.js +1 -0
- package/packages/openapi/src/names.js +50 -0
- package/packages/openapi/src/schemas.js +264 -0
- package/packages/packager/src/archive.js +106 -0
- package/packages/packager/src/crate.js +39 -0
- package/packages/packager/src/gomodule.js +11 -0
- package/packages/packager/src/index.js +9 -0
- package/packages/packager/src/maven.js +85 -0
- package/packages/packager/src/metadata.js +34 -0
- package/packages/packager/src/npm.js +71 -0
- package/packages/packager/src/nuget.js +78 -0
- package/packages/packager/src/sbom.js +56 -0
- package/packages/packager/src/wheel.js +62 -0
- package/packages/spec/src/index.js +11 -0
- package/packages/spec/src/requirements.js +139 -0
- package/packages/transpile/src/ast.js +51 -0
- package/packages/transpile/src/check.js +669 -0
- package/packages/transpile/src/emit-csharp.js +469 -0
- package/packages/transpile/src/emit-go.js +529 -0
- package/packages/transpile/src/emit-java.js +360 -0
- package/packages/transpile/src/emit-python.js +320 -0
- package/packages/transpile/src/emit-rust.js +499 -0
- package/packages/transpile/src/errors.js +15 -0
- package/packages/transpile/src/index.js +13 -0
- package/packages/transpile/src/lexer.js +161 -0
- package/packages/transpile/src/parser.js +543 -0
- package/packages/transpile/src/tests/ast.js +46 -0
- package/packages/transpile/src/tests/parse.js +376 -0
- package/packages/transpile/src/tests/translate.js +849 -0
- package/packages/transpile/src/transpile.js +57 -0
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
// Generates the Rust SDK as an in-memory file tree: one crate with the vendored kit as its runtime module.
|
|
2
|
+
|
|
3
|
+
import { createFileTree, sha256, } from "@sdkloom/codegen";
|
|
4
|
+
|
|
5
|
+
import { parseBlocks, } from "@sdkloom/kit";
|
|
6
|
+
import { rustName, rustType as checkedRustType, inputType, transpileLogic, transpileMiddleware, TranspileError } from "@sdkloom/transpile";
|
|
7
|
+
import { emitClientFile } from "./emit-client.ts";
|
|
8
|
+
import { emitWebhooks } from "./emit-webhooks.ts";
|
|
9
|
+
import { emitMock } from "./emit-mock.ts";
|
|
10
|
+
import { emitModels } from "./emit-models.ts";
|
|
11
|
+
import { doc, rust, snake, toml } from "./names.ts";
|
|
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
|
+
const TAG_LINE = /^\s*\/\/\s*@([a-z][a-z-]*)\b/;
|
|
45
|
+
const CRATE_NAME = /^[a-zA-Z][a-zA-Z0-9_-]{0,63}$/;
|
|
46
|
+
export const CUSTOM_FILE = "custom/rust/middleware.rs";
|
|
47
|
+
|
|
48
|
+
/** "acme-payments" → "acme_payments": the name Rust code uses for the crate. */
|
|
49
|
+
export function libraryName(crateName ) {
|
|
50
|
+
return crateName.replaceAll("-", "_");
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function blockDoc(block , kit ) {
|
|
54
|
+
return [
|
|
55
|
+
block.summary ?? "",
|
|
56
|
+
"",
|
|
57
|
+
`Supported versions: Rust ${block.versions ?? kit.supports}`,
|
|
58
|
+
"Sources:",
|
|
59
|
+
...block.sources.flatMap((source) => [` [${source.index}] ${source.url}`, ...block.quotes.filter((quote) => quote.index === source.index).map((quote) => ` "${quote.text}"`)]),
|
|
60
|
+
` Retrieved: ${block.retrieved ?? "unknown"}`,
|
|
61
|
+
`Provenance: block ${block.id} · kit ${kit.name} ${kit.version} · sha256:${sha256(block.code).slice(0, 12)}`,
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function vendor(file , kit , header ) {
|
|
66
|
+
const parsed = parseBlocks(file.text, { commentPrefix: "//", file: file.path });
|
|
67
|
+
if (parsed.errors.length > 0) throw new Error(`kit file ${file.path} is invalid: ${parsed.errors.map((error) => error.message).join("; ")}`);
|
|
68
|
+
const writer = rust().line(header);
|
|
69
|
+
let index = 0;
|
|
70
|
+
let inMetadata = false;
|
|
71
|
+
for (const line of file.text.split("\n")) {
|
|
72
|
+
const tag = TAG_LINE.exec(line)?.[1];
|
|
73
|
+
if (tag === "block") {
|
|
74
|
+
const block = parsed.blocks[index];
|
|
75
|
+
index += 1;
|
|
76
|
+
inMetadata = true;
|
|
77
|
+
if (block !== undefined) blockDoc(block, kit).forEach((text) => writer.line(text === "" ? "//" : `// ${text}`));
|
|
78
|
+
continue;
|
|
79
|
+
}
|
|
80
|
+
if (tag === "end" || (inMetadata && tag !== undefined)) continue;
|
|
81
|
+
inMetadata = false;
|
|
82
|
+
writer.line(line);
|
|
83
|
+
}
|
|
84
|
+
return { text: writer.toString(), blocks: parsed.blocks };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/** Middleware the author wrote natively in custom/rust/middleware.rs. */
|
|
88
|
+
export function nativeMiddleware(ir , custom ) {
|
|
89
|
+
return new Set(ir.middlewares.map((entry) => entry.name).filter((name) => custom !== undefined && new RegExp(`^\\s*(pub(\\(crate\\))?\\s+)?fn ${rustName(name)}\\s*\\(`, "m").test(custom)));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function transpile(ir , name , sources ) {
|
|
93
|
+
const middleware = ir.middlewares.find((candidate) => candidate.name === name);
|
|
94
|
+
const source = sources.get(name);
|
|
95
|
+
if (middleware === undefined || source === undefined) throw new Error(`middleware ${name} has no source to transpile`);
|
|
96
|
+
try {
|
|
97
|
+
return transpileMiddleware({ name, source, config: middleware.config }, "rust", logicOptions(ir));
|
|
98
|
+
} catch (error ) {
|
|
99
|
+
if (!(error instanceof TranspileError)) throw error;
|
|
100
|
+
throw new Error(
|
|
101
|
+
`${error.message}\nTo keep this middleware as written, write pub fn ${rustName(name)}(req: Request, next: Next, ctx: &Context) -> Result<Response, Error> in ${CUSTOM_FILE}; it replaces the transpiled one.`,
|
|
102
|
+
);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function emitMiddleware(ir , sources , custom , header ) {
|
|
107
|
+
const native = nativeMiddleware(ir, custom);
|
|
108
|
+
const writer = rust().line(header).line("//! Middleware transpiled from the TypeScript definition (spec/subset.md).").line(`//! Functions in ${CUSTOM_FILE} replace transpiled ones of the same name.`);
|
|
109
|
+
writer.line("#![allow(unused_imports, non_snake_case)]").line().line("use crate::runtime::lang::*;").line("use crate::runtime::{Context, Error, Json, Next, Request, Response};");
|
|
110
|
+
ir.middlewares
|
|
111
|
+
.filter((entry) => !native.has(entry.name))
|
|
112
|
+
.forEach((entry) => {
|
|
113
|
+
writer.line();
|
|
114
|
+
transpile(ir, entry.name, sources).trimEnd().split("\n").forEach((line) => writer.line(line));
|
|
115
|
+
});
|
|
116
|
+
if (custom === undefined) return writer.toString();
|
|
117
|
+
return `${writer.line().line(`// Copied from ${CUSTOM_FILE}.`).toString()}${custom}`;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** Middleware and logic call the project's logic functions through their public snake_case wrappers. */
|
|
121
|
+
function logicOptions(ir ) {
|
|
122
|
+
return { functions: ir.logic ?? [], logicName: (name) => `crate::logic::${rustName(snake(name))}` };
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* logic.rs: the definition's logic functions, transpiled into a private module and exported as snake_case functions
|
|
127
|
+
* with the same inputs (strings as &str).
|
|
128
|
+
*/
|
|
129
|
+
function emitLogic(ir , sources , header ) {
|
|
130
|
+
const writer = rust().line(header).line("//! Logic functions transpiled from the TypeScript definition (spec/subset.md).").line().line("use crate::runtime::Error;").line();
|
|
131
|
+
writer.line("mod transpiled {").indent((module) => {
|
|
132
|
+
module.line("#![allow(unused_imports, non_snake_case)]").line("use crate::runtime::lang::*;").line("use crate::runtime::{Error, Json};");
|
|
133
|
+
for (const logic of ir.logic ?? []) {
|
|
134
|
+
const source = sources.get(logic.name);
|
|
135
|
+
if (source === undefined) throw new Error(`logic function ${logic.name} has no source to transpile`);
|
|
136
|
+
module.line();
|
|
137
|
+
transpileLogic(logic, source, "rust", logicOptions(ir)).trimEnd().split("\n").forEach((line) => module.line(line));
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
writer.line("}");
|
|
141
|
+
for (const logic of ir.logic ?? []) {
|
|
142
|
+
const params = logic.input.map((field) => {
|
|
143
|
+
const type = checkedRustType(inputType(field));
|
|
144
|
+
return `${rustName(snake(field.name))}: ${type === "String" ? "&str" : type}`;
|
|
145
|
+
});
|
|
146
|
+
writer.line();
|
|
147
|
+
if (logic.doc !== undefined) doc(writer, [logic.doc]);
|
|
148
|
+
const result = checkedRustType(inputType({ name: logic.name, type: logic.returns, optional: false, nullable: false }));
|
|
149
|
+
writer.line(`pub fn ${rustName(snake(logic.name))}(${params.join(", ")}) -> Result<${result}, Error> {`);
|
|
150
|
+
writer.indent((body) => body.line(`transpiled::${rustName(logic.name)}(${logic.input.map((field) => rustName(snake(field.name))).join(", ")})`));
|
|
151
|
+
writer.line("}");
|
|
152
|
+
}
|
|
153
|
+
return writer.toString();
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
function emitLib(input , header ) {
|
|
157
|
+
const hooks = (input.ir.webhooks ?? []).length > 0;
|
|
158
|
+
const logic = (input.ir.logic ?? []).length > 0;
|
|
159
|
+
const writer = rust()
|
|
160
|
+
.line(header)
|
|
161
|
+
.line(`//! ${input.ir.doc ?? input.ir.name}`)
|
|
162
|
+
.line("//!")
|
|
163
|
+
.line("//! Standard library only. Start with `Client::new(Options::default())`; test with `mock::mock_client`.")
|
|
164
|
+
.line()
|
|
165
|
+
.line("pub mod client;");
|
|
166
|
+
if (logic) writer.line("pub mod logic;");
|
|
167
|
+
writer
|
|
168
|
+
.line("mod middleware;")
|
|
169
|
+
.line("pub mod mock;")
|
|
170
|
+
.line("pub mod models;")
|
|
171
|
+
.line("pub mod runtime;");
|
|
172
|
+
if (hooks) writer.line("pub mod webhooks;");
|
|
173
|
+
writer
|
|
174
|
+
.line()
|
|
175
|
+
.line("pub use client::*;")
|
|
176
|
+
.line("pub use models::*;")
|
|
177
|
+
.line("pub use runtime::{ApiError, CallOptions, Decimal, Error, EventStream, Json, MiddlewareEntry, MultipartForm, Pager, RawResult, Request, Response, ServerSentEvent, ToJson, Transport};");
|
|
178
|
+
if (logic) writer.line("pub use logic::*;");
|
|
179
|
+
if (hooks) writer.line("pub use webhooks::*;");
|
|
180
|
+
return writer.toString();
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function emitCargo(input ) {
|
|
184
|
+
return rust()
|
|
185
|
+
.line("[package]")
|
|
186
|
+
.line(`name = ${toml(input.crateName)}`)
|
|
187
|
+
.line(`version = ${toml(input.version)}`)
|
|
188
|
+
.line('edition = "2021"')
|
|
189
|
+
.line(`rust-version = ${toml(input.kit.supports.replace(/^>=/, ""))}`)
|
|
190
|
+
.line(`description = ${toml(input.ir.doc ?? input.ir.name)}`)
|
|
191
|
+
.line(`license = ${toml(input.license ?? "UNLICENSED")}`)
|
|
192
|
+
.line('readme = "README.md"')
|
|
193
|
+
.line()
|
|
194
|
+
.line("[dependencies]")
|
|
195
|
+
.toString();
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
function emitReadme(input ) {
|
|
199
|
+
return rust()
|
|
200
|
+
.line(`# ${input.crateName}`)
|
|
201
|
+
.line()
|
|
202
|
+
.line(input.ir.doc ?? "")
|
|
203
|
+
.line()
|
|
204
|
+
.line(`> Generated by sdkloom ${input.sdkloomVersion} from \`${input.source}\`. Do not edit by hand: run \`sdkloom build\`.`)
|
|
205
|
+
.line()
|
|
206
|
+
.line("```rust")
|
|
207
|
+
.line(`let client = ${libraryName(input.crateName)}::Client::new(${libraryName(input.crateName)}::Options::default())?;`)
|
|
208
|
+
.line("```")
|
|
209
|
+
.line()
|
|
210
|
+
.line(`Standard library only. Rust ${input.kit.supports}. The default transport speaks plain HTTP; for https, give \`Options::transport\` an implementation of \`Transport\` on a TLS-capable HTTP library. Test helpers: \`${libraryName(input.crateName)}::mock::mock_client\` and \`mock::examples\`.`)
|
|
211
|
+
.toString();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function generateRust(input ) {
|
|
215
|
+
const { ir } = input;
|
|
216
|
+
if (!CRATE_NAME.test(input.crateName)) throw new Error(`invalid crate name ${input.crateName}: use letters, digits, - and _`);
|
|
217
|
+
const header = `// Generated by sdkloom ${input.sdkloomVersion} from ${input.source}. Do not edit: run \`sdkloom build\` to regenerate.`;
|
|
218
|
+
const files = [];
|
|
219
|
+
const trace = new Map ();
|
|
220
|
+
const add = (path , text , from ) => {
|
|
221
|
+
files.push([path, text]);
|
|
222
|
+
trace.set(path, from);
|
|
223
|
+
};
|
|
224
|
+
const operations = ir.operations.map((operation) => `ir:operations.${operation.name}`);
|
|
225
|
+
const types = ir.types.map((named) => `ir:types.${named.name}`);
|
|
226
|
+
|
|
227
|
+
const blocks = [];
|
|
228
|
+
for (const file of [...input.kit.files].sort((a, b) => (a.path < b.path ? -1 : 1))) {
|
|
229
|
+
const vendored = vendor(file, input.kit, header);
|
|
230
|
+
add(`src/runtime/${file.path}`, vendored.text, vendored.blocks.map((block) => `block:${block.id}`));
|
|
231
|
+
vendored.blocks.forEach((block) => blocks.push({ id: block.id, versions: block.versions ?? "*", code: block.code }));
|
|
232
|
+
}
|
|
233
|
+
add("src/models.rs", emitModels(ir, header), types);
|
|
234
|
+
add("src/middleware.rs", emitMiddleware(ir, input.middlewareSources, input.middleware, header), ir.middlewares.map((entry) => `middleware:${entry.name}`));
|
|
235
|
+
add("src/client.rs", emitClientFile(ir, header, { name: input.crateName, version: input.version }), ["ir:servers", ...operations]);
|
|
236
|
+
add("src/mock.rs", emitMock(ir, header), [...types, ...operations]);
|
|
237
|
+
if ((ir.webhooks ?? []).length > 0) add("src/webhooks.rs", emitWebhooks(ir, header), (ir.webhooks ?? []).map((hook) => `ir:webhooks.${hook.name}`));
|
|
238
|
+
if ((ir.logic ?? []).length > 0) add("src/logic.rs", emitLogic(ir, input.logicSources ?? new Map(), header), (ir.logic ?? []).map((entry) => `ir:logic.${entry.name}`));
|
|
239
|
+
add("src/lib.rs", emitLib(input, header), ["ir"]);
|
|
240
|
+
add("Cargo.toml", emitCargo(input), ["config"]);
|
|
241
|
+
add("README.md", emitReadme(input), ["config", "ir"]);
|
|
242
|
+
return { files: createFileTree(files), trace, blocks };
|
|
243
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { CUSTOM_FILE, generateRust, libraryName, } from "./generate.ts";
|
|
2
|
+
export { configFields, kindOf, paramsType, resultType, signature } from "./emit-client.ts";
|
|
3
|
+
export { emitHarness } from "./emit-harness.ts";
|
|
4
|
+
export { decoder, fieldType, pascal, rustType, snake, typeName } from "./names.ts";
|
|
5
|
+
export { webhookFunction } from "./emit-webhooks.ts";
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
// Rust names, literals, comments and the IR → Rust type mapping.
|
|
2
|
+
|
|
3
|
+
import { createCodeWriter, } from "@sdkloom/codegen";
|
|
4
|
+
import { rustString } from "@sdkloom/transpile";
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
export const rust = () => createCodeWriter({ indent: " " });
|
|
8
|
+
|
|
9
|
+
const KEYWORDS = new Set([
|
|
10
|
+
"as", "async", "await", "break", "const", "continue", "crate", "dyn", "else", "enum", "extern", "false", "fn", "for", "if", "impl", "in",
|
|
11
|
+
"let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return", "self", "Self", "static", "struct", "super", "trait", "true", "type",
|
|
12
|
+
"unsafe", "use", "where", "while", "abstract", "become", "box", "do", "final", "macro", "override", "priv", "typeof", "unsized", "virtual",
|
|
13
|
+
"yield", "try",
|
|
14
|
+
]);
|
|
15
|
+
|
|
16
|
+
function words(name ) {
|
|
17
|
+
return name
|
|
18
|
+
.replace(/([a-z0-9])([A-Z])/g, "$1 $2")
|
|
19
|
+
.replace(/([A-Z]+)([A-Z][a-z])/g, "$1 $2")
|
|
20
|
+
.split(/[^A-Za-z0-9]+/)
|
|
21
|
+
.filter((word) => word !== "");
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Names a generated method uses for its own arguments and locals; a path parameter with one of them gets "_value". */
|
|
25
|
+
const LOCALS = new Set(["options", "body", "params", "args", "response", "value", "data", "client", "op", "result", "path"]);
|
|
26
|
+
|
|
27
|
+
/** A path parameter's Rust name: snake_case, never one of the generated method's own names. */
|
|
28
|
+
export function pathParamName(name ) {
|
|
29
|
+
const result = snake(name);
|
|
30
|
+
return LOCALS.has(result) ? `${result}_value` : result;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** getPayment → get_payment, x-request-id → x_request_id; keywords get a trailing underscore. */
|
|
34
|
+
export function snake(name ) {
|
|
35
|
+
const result = words(name).map((word) => word.toLowerCase()).join("_");
|
|
36
|
+
const safe = /^[0-9]/.test(result) ? `n${result}` : result;
|
|
37
|
+
return KEYWORDS.has(safe) ? `${safe}_` : safe;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** payment-method → PaymentMethod */
|
|
41
|
+
export function pascal(name ) {
|
|
42
|
+
const result = words(name).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
|
|
43
|
+
return /^[0-9]/.test(result) ? `N${result}` : result;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Names a model may not take: they would hide std, prelude or runtime types the generated code uses. */
|
|
47
|
+
const TAKEN_TYPES = new Set(["Kind", "Runtime", "Args", "CallOptions", "Clock", "State", "EventListener", "MiddlewareEntry", "OperationSpec", "RawResult", "SdkInfo", "StdTransport", "SystemClock", "HashMap", "BTreeMap", "Arc", "Mutex", "DecodeError", "DecodeFn", "FakeClock", "MockTransport", "OperationMock", "RecordedEvent", "Transport", "Next", "Middleware", "Context", "ToJson", "Mock", "Client", "Options", "RawClient", "Box", "Option", "Result", "Vec", "String", "Some", "None", "Ok", "Err", "Json", "Error", "Decimal", "Request", "Response", "Context", "Client", "Options", "RawClient", "Mock", "DecodeError", "BTreeMap", "Arc", "Mutex", "ToJson", "Transport", "Self"]);
|
|
48
|
+
|
|
49
|
+
/** The Rust type name of a model or union: PascalCase, with "Model" appended when it would hide another type. */
|
|
50
|
+
export function typeName(name ) {
|
|
51
|
+
const result = pascal(name);
|
|
52
|
+
return TAKEN_TYPES.has(result) ? `${result}Model` : result;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** SCREAMING_SNAKE for constants. */
|
|
56
|
+
export function constant(name ) {
|
|
57
|
+
return snake(name).replace(/_$/, "").toUpperCase();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** A Rust string literal (the transpiler's escaping: Rust has no \b or \f, and \u needs braces). */
|
|
61
|
+
export const str = rustString;
|
|
62
|
+
|
|
63
|
+
/** A TOML basic string, for Cargo.toml: JSON's escapes are TOML's, plus DEL; lone surrogates cannot be written. */
|
|
64
|
+
export function toml(text ) {
|
|
65
|
+
return JSON.stringify(text.toWellFormed()).replaceAll("\u007f", "\\u007F");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** /// doc comment lines. */
|
|
69
|
+
export function doc(writer , lines ) {
|
|
70
|
+
lines.filter((line, index, all) => line !== "" || index < all.length - 1).forEach((line) => writer.line(line === "" ? "///" : `/// ${line}`));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
const RUST_TYPE = {
|
|
74
|
+
string: "String",
|
|
75
|
+
secret: "String",
|
|
76
|
+
datetime: "String",
|
|
77
|
+
decimal: "Decimal",
|
|
78
|
+
int: "i64",
|
|
79
|
+
number: "f64",
|
|
80
|
+
bool: "bool",
|
|
81
|
+
bytes: "Vec<u8>",
|
|
82
|
+
stream: "Vec<u8>",
|
|
83
|
+
multipart: "MultipartForm",
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
const DECODER = {
|
|
87
|
+
string: "as_string",
|
|
88
|
+
secret: "as_string",
|
|
89
|
+
datetime: "as_string",
|
|
90
|
+
decimal: "as_decimal",
|
|
91
|
+
int: "as_integer",
|
|
92
|
+
number: "as_number",
|
|
93
|
+
bool: "as_boolean",
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
function inline(path ) {
|
|
97
|
+
throw new Error(`${path}: inline object and union types are not supported; use model() or union()`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function only(name , path ) {
|
|
101
|
+
throw new Error(`${path}: ${name} values are only supported as an operation body or result`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function namedType(ir , name ) {
|
|
105
|
+
const found = ir.types.find((named) => named.name === name);
|
|
106
|
+
if (found === undefined) throw new Error(`unknown type ${name}`);
|
|
107
|
+
return found;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export function rustType(node ) {
|
|
111
|
+
switch (node.kind) {
|
|
112
|
+
case "primitive":
|
|
113
|
+
return RUST_TYPE[node.name];
|
|
114
|
+
case "enum":
|
|
115
|
+
return "String";
|
|
116
|
+
case "list":
|
|
117
|
+
return `Vec<${rustType(node.item)}>`;
|
|
118
|
+
case "map":
|
|
119
|
+
return `BTreeMap<String, ${rustType(node.value)}>`;
|
|
120
|
+
case "ref":
|
|
121
|
+
return typeName(node.name);
|
|
122
|
+
case "object":
|
|
123
|
+
case "union":
|
|
124
|
+
return inline("type");
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** A field's type: nullable and optional are each an Option. */
|
|
129
|
+
export function fieldType(item ) {
|
|
130
|
+
const base = rustType(item.type);
|
|
131
|
+
const nullable = item.nullable ? `Option<${base}>` : base;
|
|
132
|
+
return item.optional ? `Option<${nullable}>` : nullable;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** A decoder: something callable as (&Json, &str) -> Result<T, DecodeError>. */
|
|
136
|
+
export function decoder(ir , node , path ) {
|
|
137
|
+
switch (node.kind) {
|
|
138
|
+
case "primitive":
|
|
139
|
+
return DECODER[node.name] ?? only(node.name, path);
|
|
140
|
+
case "enum":
|
|
141
|
+
return `|v: &Json, p: &str| as_enum(v, p, &[${node.values.map(str).join(", ")}])`;
|
|
142
|
+
case "list":
|
|
143
|
+
return `|v: &Json, p: &str| as_list(v, p, ${decoder(ir, node.item, path)})`;
|
|
144
|
+
case "map":
|
|
145
|
+
return `|v: &Json, p: &str| as_map(v, p, ${decoder(ir, node.value, path)})`;
|
|
146
|
+
case "ref":
|
|
147
|
+
return `${typeName(node.name)}::from_json`;
|
|
148
|
+
case "object":
|
|
149
|
+
case "union":
|
|
150
|
+
return inline(path);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** Named types reachable from a type through its fields and variants. */
|
|
155
|
+
function reachable(ir , from , seen = new Set()) {
|
|
156
|
+
const visit = (node ) => {
|
|
157
|
+
if (node.kind === "list") visit(node.item);
|
|
158
|
+
else if (node.kind === "map") visit(node.value);
|
|
159
|
+
else if (node.kind === "ref" && !seen.has(node.name)) {
|
|
160
|
+
seen.add(node.name);
|
|
161
|
+
reachable(ir, node.name, seen);
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
const named = namedType(ir, from).type;
|
|
165
|
+
if (named.kind === "object") named.fields.forEach((item) => visit(item.type));
|
|
166
|
+
else if (named.kind === "union") named.variants.forEach((variant) => visit({ kind: "ref", name: variant.type }));
|
|
167
|
+
return seen;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** A model field that refers back to its own model, directly or through others, is boxed so the struct has a size. */
|
|
171
|
+
export function boxed(ir , owner , node ) {
|
|
172
|
+
return node.kind === "ref" && reachable(ir, node.name).has(owner);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** A model field's type: like fieldType, with recursive references boxed. */
|
|
176
|
+
export function modelFieldType(ir , owner , item ) {
|
|
177
|
+
if (!boxed(ir, owner, item.type)) return fieldType(item);
|
|
178
|
+
const base = `std::boxed::Box<${rustType(item.type)}>`;
|
|
179
|
+
const nullable = item.nullable ? `Option<${base}>` : base;
|
|
180
|
+
return item.optional ? `Option<${nullable}>` : nullable;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/** A model field's decoder: like fieldDecoder, with recursive references boxed. */
|
|
184
|
+
export function modelFieldDecoder(ir , owner , item , path ) {
|
|
185
|
+
if (!boxed(ir, owner, item.type)) return fieldDecoder(ir, item, path);
|
|
186
|
+
const base = `|v: &Json, p: &str| ${decoder(ir, item.type, path)}(v, p).map(std::boxed::Box::new)`;
|
|
187
|
+
return item.nullable ? `|v: &Json, p: &str| nullable(v, p, ${base})` : base;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** The decoder of a field, with null handled for nullable fields. */
|
|
191
|
+
export function fieldDecoder(ir , item , path ) {
|
|
192
|
+
const base = decoder(ir, item.type, path);
|
|
193
|
+
return item.nullable ? `|v: &Json, p: &str| nullable(v, p, ${base})` : base;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const EXAMPLE = {
|
|
197
|
+
string: `${str("string")}.to_string()`,
|
|
198
|
+
secret: `${str("secret")}.to_string()`,
|
|
199
|
+
datetime: `${str("2026-01-01T00:00:00Z")}.to_string()`,
|
|
200
|
+
decimal: `Decimal::new("1.00")`,
|
|
201
|
+
int: "1",
|
|
202
|
+
number: "1.5",
|
|
203
|
+
bool: "true",
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
/** A small valid example value. */
|
|
207
|
+
export function example(node , path ) {
|
|
208
|
+
switch (node.kind) {
|
|
209
|
+
case "primitive":
|
|
210
|
+
return EXAMPLE[node.name] ?? only(node.name, path);
|
|
211
|
+
case "enum":
|
|
212
|
+
return `${str(node.values[0] ?? "")}.to_string()`;
|
|
213
|
+
case "list":
|
|
214
|
+
return "Vec::new()";
|
|
215
|
+
case "map":
|
|
216
|
+
return "BTreeMap::new()";
|
|
217
|
+
case "ref":
|
|
218
|
+
return `${snake(node.name)}()`;
|
|
219
|
+
case "object":
|
|
220
|
+
case "union":
|
|
221
|
+
return inline(path);
|
|
222
|
+
}
|
|
223
|
+
}
|