Skip to main content

code_analyze_core/
schema_helpers.rs

1// SPDX-FileCopyrightText: 2026 code-analyze-mcp contributors
2// SPDX-License-Identifier: Apache-2.0
3#![cfg(feature = "schemars")]
4
5use schemars::Schema;
6use serde_json::json;
7
8/// Returns a plain integer schema without the non-standard "format": "uint"
9/// that schemars emits by default for usize/u32 fields.
10pub fn integer_schema(_gen: &mut schemars::SchemaGenerator) -> Schema {
11    let map = json!({
12        "type": "integer",
13        "minimum": 0
14    })
15    .as_object()
16    .expect("json! object literal is always a Value::Object")
17    .clone();
18    Schema::from(map)
19}
20
21/// Returns a nullable integer schema for Option<usize> / Option<u32> fields.
22pub fn option_integer_schema(_gen: &mut schemars::SchemaGenerator) -> Schema {
23    let map = json!({
24        "type": ["integer", "null"],
25        "minimum": 0
26    })
27    .as_object()
28    .expect("json! object literal is always a Value::Object")
29    .clone();
30    Schema::from(map)
31}
32
33/// Returns a nullable integer schema for `Option<usize>` `ast_recursion_limit` fields.
34/// Enforces minimum: 1 because 0 would limit tree-sitter traversal to the root
35/// node only, silently returning zero results. Passing 0 is treated as unlimited
36/// at runtime; the schema minimum signals to callers that 0 is not a useful value.
37pub fn option_ast_limit_schema(_gen: &mut schemars::SchemaGenerator) -> Schema {
38    let map = json!({
39        "type": ["integer", "null"],
40        "minimum": 1
41    })
42    .as_object()
43    .expect("json! object literal is always a Value::Object")
44    .clone();
45    Schema::from(map)
46}
47
48/// Returns a nullable integer schema for `Option<usize>` `page_size` fields.
49/// Enforces minimum: 1 to prevent callers from sending `page_size=0`, which
50/// would cause `paginate_slice` to make no progress and loop on the same cursor.
51pub fn option_page_size_schema(_gen: &mut schemars::SchemaGenerator) -> Schema {
52    let map = json!({
53        "type": ["integer", "null"],
54        "minimum": 1
55    })
56    .as_object()
57    .expect("json! object literal is always a Value::Object")
58    .clone();
59    Schema::from(map)
60}