// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Turns the vendored Forgejo API description into Zig. The build runs this //! with `api/swagger.v1.json` on standard input and captures the Zig source it //! writes to standard output, so the generated bindings never land in the //! source tree and cannot drift from the specification they came from. const std = @import("std"); const openapi2zig = @import("openapi2zig"); pub fn main(init: std.process.Init) !u8 { const io = init.io; const allocator = init.gpa; var stdin_buffer: [4096]u8 = undefined; var stdin_file: std.Io.File = .stdin(); var stdin_reader = stdin_file.reader(io, &stdin_buffer); const stdin = &stdin_reader.interface; var spec: std.Io.Writer.Allocating = .init(allocator); defer spec.deinit(); _ = try stdin.streamRemaining(&spec.writer); const args = openapi2zig.CliArgs{ // Only ever read back in diagnostics: the input arrives on stdin and // the output leaves on stdout. .input_path = "api/swagger.v1.json", .output_path = "api.zig", // One tag client per Swagger tag, so an operation is reached as // `IssueClient.init(&client).issueGetIssue(...)` as well as out of // the single namespace holding all 506 of them. An operation filed // under two tags is on both tag clients. .multiple_clients = .per_tag, // The tag clients are the API surface; the path-shaped wrappers would // be a second one over the same operations. .resource_wrappers = .none, // Forgejo operations take up to a dozen parameters, and an options // struct keeps a call site readable and its arguments named. .parameters_as_struct = true, // Forgejo already uses `Option` and `Options` in the names of its own // request body models (`CreateIssueOption`), so an operation's path // and query values are its `Params` instead. .params_suffix = "Params", // The operationId with its first letter lowercased, and the params // type with its first capitalized: Forgejo writes some ids in // PascalCase, and one of them, `ActionRun`, is also a model's name. // See docs/adr/0001-operation-names-follow-forgejo.md. .operation_names = .camel_case, // The specification's summaries and descriptions, as doc comments on // the operations, their params and the models, so that they reach // `zig build docs` and an editor's hover. .doc_comments = true, // `zfj.operation_info.`: each operation's method, path and // params with their descriptions and allowed values, for the // command-line tool's `--help`. .operation_info = true, // Every name that would otherwise collide, named by hand. Nothing // else departs from Forgejo's operationIds, and each entry says // which collision it is here for. .operation_name_overrides = &.{ // `renderMarkdown`'s Raw form is already `renderMarkdownRaw`. .{ .operation_id = "renderMarkdownRaw", .name = "renderRawMarkdown" }, }, }; var document = try openapi2zig.parseToUnified(allocator, spec.written()); defer document.deinit(allocator); const generated = try openapi2zig.generateCode(allocator, document, args); defer allocator.free(generated); var stdout_buffer: [4096]u8 = undefined; var stdout_file: std.Io.File = .stdout(); var stdout_writer = stdout_file.writer(io, &stdout_buffer); const stdout = &stdout_writer.interface; try stdout.writeAll(generated); // Which Forgejo the bindings describe, for a program to report: the // specification's own version, which `zig build update-spec` checks is // the release it was taken from. try stdout.print( \\ \\/// The Forgejo release whose API description these bindings were generated from. \\pub const forgejo_version = "{f}"; \\ , .{std.zig.fmtString(document.info.version)}); try stdout.flush(); return 0; }