Skip to main content

code_analyze_core/
schema_helpers.rs

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