rustapi_core/auto_schema.rs
1//! Auto-schema registration using linkme distributed slices
2//!
3//! This module enables zero-config OpenAPI schema registration.
4//! Route macros can register schemas at link-time, and `RustApi::auto()`
5//! will collect and apply them before serving docs.
6
7use linkme::distributed_slice;
8
9/// Distributed slice containing all auto-registered schema registration functions.
10///
11/// Each element is a function that takes a mutable reference to the current
12/// [`rustapi_openapi::OpenApiSpec`] and registers one or more schemas.
13#[distributed_slice]
14pub static AUTO_SCHEMAS: [fn(&mut rustapi_openapi::OpenApiSpec)];
15
16/// Apply all auto-registered schemas into the given OpenAPI spec.
17pub fn apply_auto_schemas(spec: &mut rustapi_openapi::OpenApiSpec) {
18 for f in AUTO_SCHEMAS.iter() {
19 f(spec);
20 }
21}
22
23/// Get the count of auto-registered schema registration functions.
24pub fn auto_schema_count() -> usize {
25 AUTO_SCHEMAS.len()
26}
27
28#[cfg(test)]
29mod tests {
30 use super::*;
31
32 #[test]
33 fn test_auto_schemas_slice_exists() {
34 let _count = auto_schema_count();
35 }
36
37 #[test]
38 fn test_apply_auto_schemas_does_not_panic() {
39 let mut spec = rustapi_openapi::OpenApiSpec::new("Test", "0.0.0");
40 apply_auto_schemas(&mut spec);
41 }
42}