teo_runtime/interface/field/
field.rs

1use std::collections::BTreeMap;
2use serde::Serialize;
3use teo_parser::r#type::Type;
4use crate::comment::Comment;
5use crate::model::field::is_optional::{IsOptional};
6use crate::model::field::typed::Typed;
7use crate::optionality::Optionality;
8use crate::traits::documentable::Documentable;
9use crate::traits::named::Named;
10use crate::Value;
11
12#[derive(Debug, Serialize, Clone)]
13pub struct Field {
14    pub(super) name: String,
15    pub(super) comment: Option<Comment>,
16    pub(super) r#type: Type,
17    pub(super) optionality: Optionality,
18    pub(super) data: BTreeMap<String, Value>,
19}
20
21impl Field {
22
23    pub fn name(&self) -> &str {
24        self.name.as_str()
25    }
26
27    pub fn comment(&self) -> Option<&Comment> {
28        self.comment.as_ref()
29    }
30
31    pub fn r#type(&self) -> &Type {
32        &self.r#type
33    }
34
35    pub fn optionality(&self) -> &Optionality {
36        &self.optionality
37    }
38
39    pub fn data(&self) -> &BTreeMap<String, Value> {
40        &self.data
41    }
42}
43
44impl IsOptional for Field {
45
46    fn is_optional(&self) -> bool {
47        self.optionality.is_any_optional()
48    }
49
50    fn is_required(&self) -> bool {
51        self.optionality.is_required()
52    }
53}
54
55impl Named for Field {
56
57    fn name(&self) -> &str {
58        self.name.as_str()
59    }
60}
61
62impl Documentable for Field {
63
64    fn comment(&self) -> Option<&Comment> {
65        self.comment.as_ref()
66    }
67
68    fn kind(&self) -> &'static str {
69        "interface field"
70    }
71}
72
73impl Typed for Field {
74
75    fn r#type(&self) -> &Type {
76        &self.r#type
77    }
78}