schematic_types/
structs.rs

1use crate::schema::SchemaField;
2use std::collections::BTreeMap;
3use std::fmt;
4
5#[derive(Clone, Debug, Default, PartialEq)]
6#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
7pub struct StructType {
8    pub fields: BTreeMap<String, Box<SchemaField>>,
9
10    // The type is a partial nested config, like `PartialConfig`.
11    // This doesn't mean it's been partialized.
12    pub partial: bool,
13
14    #[cfg_attr(
15        feature = "serde",
16        serde(default, skip_serializing_if = "Option::is_none")
17    )]
18    pub required: Option<Vec<String>>,
19}
20
21impl StructType {
22    /// Create a struct/shape schema with the provided fields.
23    pub fn new<I, F>(fields: I) -> Self
24    where
25        I: IntoIterator<Item = (String, F)>,
26        F: Into<SchemaField>,
27    {
28        StructType {
29            fields: fields
30                .into_iter()
31                .map(|(k, v)| (k, Box::new(v.into())))
32                .collect(),
33            ..StructType::default()
34        }
35    }
36
37    pub fn is_hidden(&self) -> bool {
38        self.fields.values().all(|field| field.hidden)
39    }
40}
41
42impl fmt::Display for StructType {
43    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44        write!(f, "struct")
45    }
46}