Skip to main content

substrate_data/schema/description/
field_doc.rs

1//! [`FieldDoc`]: a struct's or a variant's field as data.
2
3use super::ShapeNode;
4use super::definition_doc::non_empty;
5use super::definitions::Definitions;
6use crate::schema::Field;
7
8/// A field: its name, doc, shape, whether it must be written, its range.
9#[derive(Debug, Clone, PartialEq, crate::Schema)]
10pub struct FieldDoc {
11    /// Its key.
12    pub name: String,
13    /// Its doc comment.
14    pub doc: Option<String>,
15    /// What it holds.
16    pub shape: ShapeNode,
17    /// Must be written (no default).
18    pub required: bool,
19    /// The numbers it holds must be in `[min, max]`.
20    pub range: Option<[f64; 2]>,
21}
22
23impl FieldDoc {
24    /// `fields` as data, their structs and enums defined in `defs`.
25    pub(super) fn all(fields: &[Field], defs: &mut Definitions) -> Vec<FieldDoc> {
26        fields
27            .iter()
28            .map(|f| FieldDoc {
29                name: f.name.to_owned(),
30                doc: non_empty(f.doc),
31                shape: ShapeNode::of(&(f.shape)(), defs),
32                required: f.required,
33                range: f.range.map(|(lo, hi)| [lo, hi]),
34            })
35            .collect()
36    }
37}