architect_schemars/json_schema_impls/
arrayvec05.rs

1use crate::r#gen::SchemaGenerator;
2use crate::schema::*;
3use crate::JsonSchema;
4use arrayvec05::{Array, ArrayString, ArrayVec};
5use std::convert::TryInto;
6
7// Do not set maxLength on the schema as that describes length in characters, but we only
8// know max length in bytes.
9forward_impl!((<A> JsonSchema for ArrayString<A> where A: Array<Item = u8> + Copy) => String);
10
11impl<A: Array> JsonSchema for ArrayVec<A>
12where
13    A::Item: JsonSchema,
14{
15    no_ref_schema!();
16
17    fn schema_name() -> String {
18        format!(
19            "Array_up_to_size_{}_of_{}",
20            A::CAPACITY,
21            A::Item::schema_name()
22        )
23    }
24
25    fn json_schema(generator: &mut SchemaGenerator) -> Schema {
26        SchemaObject {
27            instance_type: Some(InstanceType::Array.into()),
28            array: Some(Box::new(ArrayValidation {
29                items: Some(generator.subschema_for::<A::Item>().into()),
30                max_items: A::CAPACITY.try_into().ok(),
31                ..Default::default()
32            })),
33            ..Default::default()
34        }
35        .into()
36    }
37}