Skip to main content

aptu_coder_core/
schema_helpers.rs

1// SPDX-FileCopyrightText: 2026 aptu-coder 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/// `None` = library default, `0` = unlimited traversal depth, `n` = limit to n levels.
35pub fn option_ast_limit_schema(_gen: &mut schemars::SchemaGenerator) -> Schema {
36    let map = json!({
37        "type": ["integer", "null"],
38        "minimum": 0
39    })
40    .as_object()
41    .expect("json! object literal is always a Value::Object")
42    .clone();
43    Schema::from(map)
44}
45
46/// Regex matching all supported source file extensions (case-insensitive).
47///
48/// Used as the `inputSchema` `pattern` constraint on `path` fields in
49/// `AnalyzeFileParams` and `AnalyzeModuleParams`. Covers every extension in
50/// `lang.rs` `EXTENSION_MAP`. Centralised here so adding a language requires
51/// one change, not two.
52pub const SUPPORTED_FILE_EXT_PATTERN: &str = r"(?i)\.(rs|py|go|ts|tsx|js|mjs|cjs|java|kt|kts|cs|cpp|cc|cxx|c|h|hpp|hxx|f|f77|f90|f95|f03|f08|for|ftn)$";
53
54/// Returns a string schema with a `pattern` constraint covering all supported
55/// source file extensions. Used as `schema_with` on `path` fields.
56pub fn supported_file_path_schema(_gen: &mut schemars::SchemaGenerator) -> Schema {
57    let map = serde_json::json!({
58        "type": "string",
59        "pattern": SUPPORTED_FILE_EXT_PATTERN
60    })
61    .as_object()
62    .expect("json! object literal is always a Value::Object")
63    .clone();
64    Schema::from(map)
65}
66
67/// Returns a nullable integer schema for `Option<usize>` `page_size` fields.
68/// Enforces minimum: 1 to prevent callers from sending `page_size=0`, which
69/// would cause `paginate_slice` to make no progress and loop on the same cursor.
70pub fn option_page_size_schema(_gen: &mut schemars::SchemaGenerator) -> Schema {
71    let map = json!({
72        "type": ["integer", "null"],
73        "minimum": 1
74    })
75    .as_object()
76    .expect("json! object literal is always a Value::Object")
77    .clone();
78    Schema::from(map)
79}