substrate_data/schema/description/
shape_node.rs1use super::DefinitionDoc;
4use super::definitions::Definitions;
5use crate::model::value::KindName;
6use crate::schema::Shape;
7
8#[derive(Debug, Clone, PartialEq, crate::Schema)]
11#[data(tag = "kind")]
12pub enum ShapeNode {
13 Any,
15 Bool,
17 Int {
19 min: i64,
21 max: i64,
23 },
24 Float {
26 bits: u32,
28 },
29 String,
31 Atom,
33 Bytes,
35 Ref {
37 of: Vec<KindName>,
39 id: Box<ShapeNode>,
41 },
42 Named {
44 name: String,
46 of: Box<ShapeNode>,
48 },
49 List {
51 of: Box<ShapeNode>,
53 },
54 Map {
56 of: Box<ShapeNode>,
58 },
59 Optional {
61 of: Box<ShapeNode>,
63 },
64 Def {
66 name: String,
68 },
69}
70
71impl ShapeNode {
72 pub(super) fn of(shape: &Shape, defs: &mut Definitions) -> ShapeNode {
74 let mut inner = |s: Shape| Box::new(ShapeNode::of(&s, defs));
75 match shape {
76 Shape::Any => ShapeNode::Any,
77 Shape::Bool => ShapeNode::Bool,
78 Shape::Int { min, max } => ShapeNode::Int {
79 min: *min,
80 max: *max,
81 },
82 Shape::Float { bits } => ShapeNode::Float { bits: *bits },
83 Shape::Str => ShapeNode::String,
84 Shape::Atom => ShapeNode::Atom,
85 Shape::Bytes => ShapeNode::Bytes,
86 Shape::Ref { kinds, id } => ShapeNode::Ref {
87 of: kinds.iter().filter_map(|k| KindName::new(k).ok()).collect(),
88 id: inner(id()),
89 },
90 Shape::Named(n) => ShapeNode::Named {
91 name: n.name().to_owned(),
92 of: inner(n.stored()),
93 },
94 Shape::List(s) => ShapeNode::List { of: inner(s()) },
95 Shape::Map(s) => ShapeNode::Map { of: inner(s()) },
96 Shape::Optional(s) => ShapeNode::Optional { of: inner(s()) },
97 Shape::Struct(s) => DefinitionDoc::define_struct(s, defs),
98 Shape::Enum(e) => DefinitionDoc::define_enum(e, defs),
99 }
100 }
101}