schemars/json_schema_impls/
array.rs

1use crate::SchemaGenerator;
2use crate::_alloc_prelude::*;
3use crate::{json_schema, JsonSchema, Schema};
4use alloc::borrow::Cow;
5
6// Does not require T: JsonSchema.
7impl<T> JsonSchema for [T; 0] {
8    inline_schema!();
9
10    fn schema_name() -> Cow<'static, str> {
11        "EmptyArray".into()
12    }
13
14    fn schema_id() -> Cow<'static, str> {
15        "[]".into()
16    }
17
18    fn json_schema(_: &mut SchemaGenerator) -> Schema {
19        json_schema!({
20            "type": "array",
21            "maxItems": 0,
22        })
23    }
24}
25
26macro_rules! array_impls {
27    ($($len:tt)+) => {
28        $(
29            impl<T: JsonSchema> JsonSchema for [T; $len] {
30                inline_schema!();
31
32                fn schema_name() -> Cow<'static, str> {
33                    format!("Array_size_{}_of_{}", $len, T::schema_name()).into()
34                }
35
36                fn schema_id() -> Cow<'static, str> {
37                    format!("[{}; {}]", $len, T::schema_id()).into()
38                }
39
40                fn json_schema(generator: &mut SchemaGenerator) -> Schema {
41                    json_schema!({
42                        "type": "array",
43                        "items": serde_json::Value::from(generator.subschema_for::<T>()),
44                        "minItems": $len,
45                        "maxItems": $len,
46                    })
47                }
48            }
49        )+
50    }
51}
52
53array_impls! {
54     1  2  3  4  5  6  7  8  9 10
55    11 12 13 14 15 16 17 18 19 20
56    21 22 23 24 25 26 27 28 29 30
57    31 32
58}