hezarfen_backend
Clone
https://git.tahinli.com/archive/hezarfen_backend.git
0 watching0 forksRust8.3 MiB6d68754Ahmet Kaan Gümüş2 months ago566 commits
| 1 | //! The README's `## Endpoints` table is generated, not written by hand. |
| 2 | //! |
| 3 | //! A hand-maintained route table drifts the moment a handler changes: a prose |
| 4 | //! review only catches a *missing* path, never a stale Auth |
| 5 | //! cell nor a row for a route the code deleted. So this test renders the table |
| 6 | //! from the served `/api-docs/openapi.json` and asserts the marker block in |
| 7 | //! README.md matches it. Regenerate after any route change: |
| 8 | //! |
| 9 | //! ```text |
| 10 | //! UPDATE_DOCS=1 cargo test --test doc_sync |
| 11 | //! ``` |
| 12 | //! |
| 13 | //! Two columns come straight out of the spec (Path/Method, and Description = |
| 14 | //! the handler's doc comment, which utoipa publishes as the operation summary). |
| 15 | //! The **Auth** column cannot: OpenAPI has no field for "minimum role", and the |
| 16 | //! `403` descriptions only sometimes spell it out. So the floor is read from the |
| 17 | //! two places that actually enforce it — the declared `403` when it names a role |
| 18 | //! (`"Requires manager role or higher"`), otherwise the `Require*` extractor in |
| 19 | //! the handler's own signature. Every operation must resolve to exactly one |
| 20 | //! floor or this test fails naming it, so the column can never go quietly wrong. |
| 21 | |
| 22 | |
| 23 | |
| 24 | use HashMap; |
| 25 | |
| 26 | use Value; |
| 27 | |
| 28 | const BEGIN: &str = "<!-- BEGIN GENERATED: endpoint-table -->"; |
| 29 | const END: &str = "<!-- END GENERATED -->"; |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | /// Routes no OpenAPI document can carry, in the shape the table wants them. |
| 36 | /// `/` is a plain `axum::routing::get` (no `routes!` macro, so no operation); |
| 37 | /// the two doc endpoints are mounted by `SwaggerUi`, outside the |
| 38 | /// `OpenApiRouter`; the two `…/ws` rooms are WebSocket upgrades, which the spec |
| 39 | /// itself calls out as "Not in this spec (WebSocket)" in their tag text. |
| 40 | /// Everything else is derived — do not grow this list to paper over a missing |
| 41 | /// `#[utoipa::path]`. |
| 42 | const STATIC_ROWS: = |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | |
| 58 | ; |
| 59 | |
| 60 | /// Table order within one path. The repo uses `GET`/`POST`/`PATCH`/`DELETE` |
| 61 | /// only (AGENTS.md: no `PUT`); anything else sorts last. |
| 62 | |
| 63 | |
| 64 | |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | /// `s[open..]` starts at a `(`; returns the index of its matching `)`. |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | /// The value of a `key = "…"` entry inside a `#[utoipa::path(…)]` attribute. |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | /// The role floor a handler's argument list enforces. `CurrentUser` and the |
| 95 | /// bare extractors gate on a session only, which the README calls `student` |
| 96 | /// ("any logged-in user"). |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | |
| 103 | |
| 104 | |
| 105 | |
| 106 | |
| 107 | |
| 108 | |
| 109 | |
| 110 | |
| 111 | |
| 112 | |
| 113 | /// Every `#[utoipa::path]`-annotated handler in `src/`, keyed by the four |
| 114 | /// things the spec also publishes: method, tag, operation id (= the fn name) |
| 115 | /// and the route path *relative to its nest prefix*. That key needs no |
| 116 | /// knowledge of how `lib.rs` nests routers, which is why it is used instead of |
| 117 | /// reconstructing full paths. |
| 118 | type HandlerKey = ; |
| 119 | |
| 120 | /// Every `src/**/*.rs` as (path relative to the repo, contents). |
| 121 | |
| 122 | |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | |
| 130 | |
| 131 | |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | |
| 151 | |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | |
| 160 | |
| 161 | |
| 162 | |
| 163 | |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | /// The minimum role for one operation. `no` when the operation carries no |
| 198 | /// security requirement at all; otherwise the role its `403` declares, and |
| 199 | /// failing that the one its handler's extractor enforces. |
| 200 | |
| 201 | |
| 202 | |
| 203 | |
| 204 | |
| 205 | |
| 206 | |
| 207 | |
| 208 | |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | |
| 215 | |
| 216 | |
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | |
| 236 | |
| 237 | |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | /// The Description cell: the operation summary, which utoipa lifts from the |
| 248 | /// first paragraph of the handler's doc comment. Folded to one line, with `|` |
| 249 | /// escaped so a cell cannot break the table. A handler with no doc comment |
| 250 | /// falls back to its success response's description — still spec text, never |
| 251 | /// invented — and both cases mean "write a doc comment". |
| 252 | |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | /// Render the whole table (header included) from the served spec. |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 | |
| 287 | |
| 288 | |
| 289 | |
| 290 | |
| 291 | |
| 292 | |
| 293 | |
| 294 | |
| 295 | |
| 296 | |
| 297 | |
| 298 | |
| 299 | |
| 300 | |
| 301 | |
| 302 | |
| 303 | |
| 304 | |
| 305 | |
| 306 | |
| 307 | |
| 308 | |
| 309 | |
| 310 | |
| 311 | |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | |
| 324 | |
| 325 | |
| 326 | |
| 327 | /// The served OpenAPI document — the real router's, not a re-derived one. |
| 328 | async |
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | /// Split the README on the generated-block markers: (before, block, after). |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | /// Compared (and written) whitespace-normalised: trailing spaces stripped per |
| 347 | /// line, surrounding blank lines ignored. Cell padding still has to match, |
| 348 | /// which keeps the checked-in table readable as a table. |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | async |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | |
| 387 | |
| 388 | |
| 389 | |
| 390 | |
| 391 | |
| 392 | /// Every plain `.route("…", …)` registered by production code, as (source |
| 393 | /// file, path relative to its nest prefix). `#[cfg(test)]` modules are cut |
| 394 | /// out first — `src/ai/server.rs` mounts throwaway routers in its own tests. |
| 395 | /// Only test *modules* are cut, so a `.route()` inside a `#[cfg(test)] fn` |
| 396 | /// would be reported as untabled; none exists, and the fix is to move it into |
| 397 | /// the file's `mod tests`. |
| 398 | |
| 399 | |
| 400 | |
| 401 | |
| 402 | |
| 403 | |
| 404 | |
| 405 | |
| 406 | |
| 407 | |
| 408 | |
| 409 | |
| 410 | |
| 411 | |
| 412 | |
| 413 | |
| 414 | |
| 415 | |
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | /// Hazard the generated table cannot see on its own: a route registered with |
| 432 | /// `.route()` carries no `#[utoipa::path]`, so it never reaches `spec["paths"]` |
| 433 | /// and never becomes a row unless someone remembered [`STATIC_ROWS`]. Assert |
| 434 | /// every production `.route()` is accounted for by one of the two. |
| 435 | |
| 436 | async |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | /// The other half: [`STATIC_ROWS`] is hand-written, so a row whose route was |
| 460 | /// renamed or deleted would sit in the README forever. Drive the real router |
| 461 | /// and let it answer. Only `404`/`405` refute existence — every one of these |
| 462 | /// is authenticated or a WebSocket upgrade, so `401`/`400`/`426`/a Swagger |
| 463 | /// redirect all prove the route is there. |
| 464 | |
| 465 | async |
| 466 | |
| 467 | |
| 468 | |
| 469 | |
| 470 | |
| 471 | |
| 472 | |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 |