poem_openapi/types/external/
slice.rs

1use std::borrow::Cow;
2
3use serde_json::Value;
4
5use crate::{
6    registry::{MetaSchema, MetaSchemaRef, Registry},
7    types::{ToJSON, Type},
8};
9
10impl<T: Type> Type for &[T] {
11    const IS_REQUIRED: bool = true;
12
13    type RawValueType = Self;
14
15    type RawElementValueType = T::RawValueType;
16
17    fn name() -> Cow<'static, str> {
18        format!("slice_{}", T::name()).into()
19    }
20
21    fn schema_ref() -> MetaSchemaRef {
22        MetaSchemaRef::Inline(Box::new(MetaSchema {
23            items: Some(Box::new(T::schema_ref())),
24            ..MetaSchema::new("array")
25        }))
26    }
27
28    fn register(registry: &mut Registry) {
29        T::register(registry);
30    }
31
32    fn as_raw_value(&self) -> Option<&Self::RawValueType> {
33        Some(self)
34    }
35
36    fn raw_element_iter<'a>(
37        &'a self,
38    ) -> Box<dyn Iterator<Item = &'a Self::RawElementValueType> + 'a> {
39        Box::new(self.iter().filter_map(|item| item.as_raw_value()))
40    }
41
42    fn is_empty(&self) -> bool {
43        <[T]>::is_empty(self)
44    }
45}
46
47impl<T: ToJSON> ToJSON for &[T] {
48    fn to_json(&self) -> Option<Value> {
49        let mut values = Vec::with_capacity(self.len());
50        for item in *self {
51            if let Some(value) = item.to_json() {
52                values.push(value);
53            }
54        }
55        Some(Value::Array(values))
56    }
57}