jss_core/schema/traits/
mod.rs1use super::*;
2
3mod debug;
4mod display;
5
6impl Default for JssKind {
7 fn default() -> Self {
8 Self::Property
9 }
10}
11
12impl Default for JssType {
13 fn default() -> Self {
14 Self::Undefined
15 }
16}
17
18impl Default for JssSchema {
19 fn default() -> Self {
21 Self {
22 kind: JssKind::Property,
23 name: Some("_".to_string()),
24 description: "".to_string(),
25 typing: Default::default(),
26 property: Default::default(),
27 definition: Default::default(),
28 attribute: Default::default(),
29 }
31 }
32}
33
34impl JssSchema {
35 #[inline]
36 pub fn property() -> Self {
37 Self::default()
38 }
39 pub fn definition() -> Self {
40 Self { kind: JssKind::Definition, typing: JssType::Undefined, ..Default::default() }
41 }
42 #[inline]
43 pub fn anything() -> Self {
44 Self { kind: JssKind::Scheme, typing: JssType::Anything, ..Default::default() }
45 }
46 #[inline]
47 pub fn nothing() -> Self {
48 Self { kind: JssKind::Scheme, typing: JssType::Nothing, ..Default::default() }
49 }
50 #[inline]
51 pub fn top() -> Self {
52 Self { kind: JssKind::Scheme, typing: JssType::Undefined, ..Default::default() }
53 }
54 #[inline]
55 pub fn is_anything(&self) -> bool {
56 matches!(self.typing, JssType::Anything)
57 }
58 #[inline]
59 pub fn is_noting(&self) -> bool {
60 matches!(self.typing, JssType::Nothing)
61 }
62 pub fn is_top(&self) -> bool {
63 matches!(self.kind, JssKind::Scheme)
64 }
65 pub fn is_ref_type(&self) -> bool {
66 matches!(self.typing, JssType::Reference(_))
67 }
68}
69
70impl JssSchema {
71 pub fn get_name(&self) -> &str {
72 debug_assert!(self.name.is_some());
73 match &self.name {
74 Some(s) => s.as_str(),
75 None => "",
76 }
77 }
78 pub fn set_name<S>(&mut self, name: S)
79 where
80 S: Into<String>,
81 {
82 self.name = Some(name.into())
83 }
84
85 pub fn get_type(&self) -> &JssType {
86 &self.typing
87 }
88 pub fn set_type<S>(&mut self, typing: S)
89 where
90 S: Into<JssType>,
91 {
92 self.typing = typing.into()
93 }
94
95 pub fn has_description(&self) -> bool {
96 !self.description.is_empty()
97 }
98
99 pub fn get_description(&self) -> &str {
100 &self.description
101 }
102 pub fn set_description<S>(&mut self, description: S)
103 where
104 S: Into<String>,
105 {
106 self.description = description.into()
107 }
108
109 pub fn properties(&self) -> Iter<'_, String, JssSchema> {
110 self.property.iter()
111 }
112 pub fn insert_property<K, V>(&mut self, key: K, value: V) -> Option<JssSchema>
113 where
114 K: Into<String>,
115 V: Into<JssSchema>,
116 {
117 self.property.insert(key.into(), value.into())
118 }
119
120 pub fn attributes(&self) -> Iter<'_, String, JssValue> {
121 self.attribute.iter()
122 }
123 pub fn insert_attribute<K, V>(&mut self, key: K, value: V) -> Option<JssValue>
124 where
125 K: Into<String>,
126 V: Into<JssValue>,
127 {
128 self.attribute.insert(key.into(), value.into())
129 }
130
131 pub fn definitions(&self) -> Iter<'_, String, JssSchema> {
132 self.definition.iter()
133 }
134 pub fn insert_definition<K, V>(&mut self, key: K, value: V) -> Option<JssSchema>
135 where
136 K: Into<String>,
137 V: Into<JssSchema>,
138 {
139 self.definition.insert(key.into(), value.into())
140 }
141}