Skip to main content

substrate_data/schema/description/
shape_doc.rs

1//! [`ShapeDoc`]: a whole shape as data — its root and the definitions of
2//! the structs and enums in it.
3
4use super::definitions::Definitions;
5use super::{DefinitionDoc, ShapeNode};
6use crate::schema::{Ordered, Shape};
7
8/// A shape as data: written `{ root, defs }`, structs and enums once in
9/// `defs` and referred to by name.
10#[derive(Debug, Clone, PartialEq, crate::Schema)]
11pub struct ShapeDoc {
12    /// The shape described.
13    pub root: ShapeNode,
14    /// Every struct and enum in it, by name, in the order met.
15    pub defs: Ordered<DefinitionDoc>,
16}
17
18impl ShapeDoc {
19    /// `shape` described.
20    #[must_use]
21    pub fn of(shape: &Shape) -> ShapeDoc {
22        let mut defs = Definitions::default();
23        let root = ShapeNode::of(shape, &mut defs);
24        ShapeDoc {
25            root,
26            defs: defs.finish(),
27        }
28    }
29
30    /// The definition named `name`.
31    #[must_use]
32    pub fn definition(&self, name: &str) -> Option<&DefinitionDoc> {
33        self.defs.0.iter().find(|(n, _)| n == name).map(|(_, d)| d)
34    }
35}