substrate_data/schema/description/
definition_doc.rs1use super::definitions::Definitions;
4use super::{FieldDoc, ShapeNode, VariantDoc};
5use crate::schema::{EnumShape, StructShape};
6
7#[derive(Debug, Clone, PartialEq, crate::Schema)]
9#[data(tag = "kind")]
10pub enum DefinitionDoc {
11 Struct {
13 doc: Option<String>,
15 fields: Vec<FieldDoc>,
17 },
18 Enum {
20 doc: Option<String>,
22 tag: Option<String>,
24 variants: Vec<VariantDoc>,
26 },
27}
28
29impl DefinitionDoc {
30 pub(super) fn define_struct(s: &StructShape, defs: &mut Definitions) -> ShapeNode {
32 if !defs.has(s.name) {
33 defs.reserve(s.name);
34 let fields = FieldDoc::all(&s.fields, defs);
35 let doc = non_empty(s.doc);
36 defs.set(s.name, DefinitionDoc::Struct { doc, fields });
37 }
38 ShapeNode::Def {
39 name: s.name.to_owned(),
40 }
41 }
42
43 pub(super) fn define_enum(e: &EnumShape, defs: &mut Definitions) -> ShapeNode {
45 if !defs.has(e.name) {
46 defs.reserve(e.name);
47 let variants = e
48 .variants
49 .iter()
50 .map(|v| VariantDoc::of(v, e.tag.is_some(), defs))
51 .collect();
52 let definition = DefinitionDoc::Enum {
53 doc: non_empty(e.doc),
54 tag: e.tag.map(str::to_owned),
55 variants,
56 };
57 defs.set(e.name, definition);
58 }
59 ShapeNode::Def {
60 name: e.name.to_owned(),
61 }
62 }
63}
64
65pub(super) fn non_empty(doc: &str) -> Option<String> {
67 (!doc.is_empty()).then(|| doc.to_owned())
68}