teo_runtime/model/field/
field.rs1use std::collections::BTreeMap;
2use std::sync::Arc;
3use serde::Serialize;
4use teo_parser::availability::Availability;
5use teo_parser::r#type::Type;
6pub use super::decorator::Decorator;
7use crate::comment::Comment;
8use crate::database::r#type::DatabaseType;
9use crate::model::field::column_named::ColumnNamed;
10use crate::model::field::Index;
11use crate::model::field::indexable::{Indexable};
12use crate::model::field::is_optional::IsOptional;
13use crate::model::field::Migration;
14use crate::traits::named::Named;
15use crate::model::field::typed::Typed;
16use crate::optionality::Optionality;
17use crate::pipeline::pipeline::Pipeline;
18use crate::readwrite::read::Read;
19use crate::readwrite::write::Write;
20use crate::traits::documentable::Documentable;
21use crate::value::Value;
22
23#[derive(Debug, Clone)]
24pub struct Field {
25 pub(super) inner: Arc<Inner>,
26}
27
28#[derive(Debug, Serialize)]
29pub(super) struct Inner {
30 pub(super) name: String,
31 pub(super) comment: Option<Comment>,
32 pub(super) column_name: String,
33 pub(super) foreign_key: bool,
34 pub(super) dropped: bool,
35 pub(super) migration: Option<Migration>,
36 pub(super) r#type: Type,
37 pub(super) database_type: DatabaseType,
38 pub(super) optionality: Optionality,
39 pub(super) copy: bool,
40 pub(super) read: Read,
41 pub(super) write: Write,
42 pub(super) atomic: bool,
43 pub(super) r#virtual: bool,
44 pub(super) input_omissible: bool,
45 pub(super) output_omissible: bool,
46 pub(super) index: Option<Index>,
47 pub(super) queryable: bool,
48 pub(super) sortable: bool,
49 pub(super) auto: bool,
50 pub(super) auto_increment: bool,
51 pub(super) default: Option<Value>,
52 pub(super) on_set: Pipeline,
53 pub(super) on_save: Pipeline,
54 pub(super) on_output: Pipeline,
55 pub(super) can_mutate: Pipeline,
56 pub(super) can_read: Pipeline,
57 pub(super) data: BTreeMap<String, Value>,
58 pub(super) availability: Availability,
59}
60
61impl Field {
62
63 pub fn foreign_key(&self) -> bool {
64 self.inner.foreign_key
65 }
66
67 pub fn dropped(&self) -> bool {
68 self.inner.dropped
69 }
70
71 pub fn database_type(&self) -> &DatabaseType {
72 &self.inner.database_type
73 }
74
75 pub fn optionality(&self) -> &Optionality {
76 &self.inner.optionality
77 }
78
79 pub fn copy(&self) -> bool {
80 self.inner.copy
81 }
82
83 pub fn read(&self) -> &Read {
84 &self.inner.read
85 }
86
87 pub fn write(&self) -> &Write {
88 &self.inner.write
89 }
90
91 pub fn atomic(&self) -> bool {
92 self.inner.atomic
93 }
94
95 pub fn r#virtual(&self) -> bool {
96 self.inner.r#virtual
97 }
98
99 pub fn input_omissible(&self) -> bool {
100 self.inner.input_omissible
101 }
102
103 pub fn output_omissible(&self) -> bool {
104 self.inner.output_omissible
105 }
106
107 pub fn queryable(&self) -> bool {
108 self.inner.queryable
109 }
110
111 pub fn sortable(&self) -> bool {
112 self.inner.sortable
113 }
114
115 pub fn auto(&self) -> bool {
116 self.inner.auto
117 }
118
119 pub fn auto_increment(&self) -> bool {
120 self.inner.auto_increment
121 }
122
123 pub fn default(&self) -> Option<&Value> {
124 self.inner.default.as_ref()
125 }
126
127 pub fn on_set(&self) -> &Pipeline {
128 &self.inner.on_set
129 }
130
131 pub fn on_save(&self) -> &Pipeline {
132 &self.inner.on_save
133 }
134
135 pub fn on_output(&self) -> &Pipeline {
136 &self.inner.on_output
137 }
138
139 pub fn can_mutate(&self) -> &Pipeline {
140 &self.inner.can_mutate
141 }
142
143 pub fn can_read(&self) -> &Pipeline {
144 &self.inner.can_read
145 }
146
147 pub fn data(&self) -> &BTreeMap<String, Value> {
148 &self.inner.data
149 }
150
151 pub fn availability(&self) -> &Availability {
152 &self.inner.availability
153 }
154
155 pub fn migration(&self) -> Option<&Migration> {
156 self.inner.migration.as_ref()
157 }
158}
159
160impl Named for Field {
161
162 fn name(&self) -> &str {
163 self.inner.name.as_str()
164 }
165}
166
167impl ColumnNamed for Field {
168
169 fn column_name(&self) -> &str {
170 self.inner.column_name.as_str()
171 }
172
173}
174
175impl Indexable for Field {
176
177 fn index(&self) -> Option<&Index> {
178 self.inner.index.as_ref()
179 }
180}
181
182impl IsOptional for Field {
183
184 fn is_optional(&self) -> bool {
185 self.inner.optionality.is_any_optional()
186 }
187
188 fn is_required(&self) -> bool {
189 self.inner.optionality.is_required()
190 }
191}
192
193impl Typed for Field {
194
195 fn r#type(&self) -> &Type {
196 &self.inner.r#type
197 }
198}
199
200impl Documentable for Field {
201
202 fn comment(&self) -> Option<&Comment> {
203 self.inner.comment.as_ref()
204 }
205
206 fn kind(&self) -> &'static str {
207 "field"
208 }
209}
210
211impl Serialize for Field {
212
213 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: serde::Serializer {
214 self.inner.serialize(serializer)
215 }
216}