1 // Written in the D programming language.
2 
3 /**
4 Module for various 9P / Styx objects checks. For internal use only. 
5 
6 Copyright: LightHouse Software, 2021
7 License:   $(HTTP https://github.com/aquaratixc/ESL-License, Experimental Software License 1.0).
8 Authors:   Oleg Bakharev,
9 		   Ilya Pertsev
10 */
11 module styx2000.protomsg.verificators;
12 
13 private {
14 	 import styx2000.protobj.type;
15 	
16 	 import styx2000.protoconst.messages;
17 	 import styx2000.protoconst.sizes;
18 	
19 	 import styx2000.protobj.styxobject;
20 	 
21 	 import styx2000.protomsg.typeconv;
22 }
23 
24 /// Message has right count of styx objects ?
25 auto hasValidFieldsCount(Type type, StyxObject[] msg...)
26 {
27 	ulong fieldsCount;
28 	ulong messageFieldsCount = msg.length;
29 			
30 	with (STYX_MESSAGE_TYPE)
31 	{
32 		switch(type.getType) 
33 		{
34 			// version
35 			case R_VERSION:
36 				fieldsCount = STYX_MESSAGE_SIZE.R_VERSION;
37 				break;
38 			case T_VERSION:
39 				fieldsCount = STYX_MESSAGE_SIZE.T_VERSION;
40 				break;
41 			// auth
42 			case R_AUTH:
43 				fieldsCount = STYX_MESSAGE_SIZE.R_AUTH;
44 				break;
45 			case T_AUTH:
46 				fieldsCount = STYX_MESSAGE_SIZE.T_AUTH;
47 				break;
48 			// error
49 			case R_ERROR:
50 				fieldsCount = STYX_MESSAGE_SIZE.R_ERROR;
51 				break;
52 			// flush
53 			case R_FLUSH:
54 				fieldsCount = STYX_MESSAGE_SIZE.R_FLUSH;
55 				break;
56 			case T_FLUSH:
57 				fieldsCount = STYX_MESSAGE_SIZE.T_FLUSH;
58 				break;
59 			// attach
60 			case R_ATTACH:
61 				fieldsCount = STYX_MESSAGE_SIZE.R_ATTACH;
62 				break;
63 			case T_ATTACH:
64 				fieldsCount = STYX_MESSAGE_SIZE.T_ATTACH;
65 				break;
66 			// walk
67 			case R_WALK:
68 				fieldsCount = STYX_MESSAGE_SIZE.R_WALK;
69 				break;
70 			case T_WALK:
71 				fieldsCount = STYX_MESSAGE_SIZE.T_WALK;
72 				break;
73 			// open
74 			case R_OPEN:
75 				fieldsCount = STYX_MESSAGE_SIZE.R_OPEN;
76 				break;
77 			case T_OPEN:
78 				fieldsCount = STYX_MESSAGE_SIZE.T_OPEN;
79 				break;
80 			// create
81 			case R_CREATE:
82 				fieldsCount = STYX_MESSAGE_SIZE.R_CREATE;
83 				break;
84 			case T_CREATE:
85 				fieldsCount = STYX_MESSAGE_SIZE.T_CREATE;
86 				break;
87 			// read
88 			case R_READ:
89 				fieldsCount = STYX_MESSAGE_SIZE.R_READ;
90 				break;
91 			case T_READ:
92 				fieldsCount = STYX_MESSAGE_SIZE.T_READ;
93 				break;
94 			// write
95 			case R_WRITE:
96 				fieldsCount = STYX_MESSAGE_SIZE.R_WRITE;
97 				break;
98 			case T_WRITE:
99 				fieldsCount = STYX_MESSAGE_SIZE.T_WRITE;
100 				break;
101 			// clunk
102 			case R_CLUNK:
103 				fieldsCount = STYX_MESSAGE_SIZE.R_CLUNK;
104 				break;
105 			case T_CLUNK:
106 				fieldsCount = STYX_MESSAGE_SIZE.T_CLUNK;
107 				break;
108 			// remove
109 			case R_REMOVE:
110 				fieldsCount = STYX_MESSAGE_SIZE.R_REMOVE;
111 				break;
112 			case T_REMOVE:
113 				fieldsCount = STYX_MESSAGE_SIZE.T_REMOVE;
114 				break;
115 			// stat
116 			case R_STAT:
117 				fieldsCount = STYX_MESSAGE_SIZE.R_STAT;
118 				break;
119 			case T_STAT:
120 				fieldsCount = STYX_MESSAGE_SIZE.T_STAT;
121 				break;
122 			// wstat
123 			case R_WSTAT:
124 				fieldsCount = STYX_MESSAGE_SIZE.R_WSTAT;
125 				break;
126 			case T_WSTAT:
127 				fieldsCount = STYX_MESSAGE_SIZE.T_WSTAT;
128 				break;
129 			default:
130 				//throw new Exception("Bad message type");
131 				break;
132 		}
133 	}
134 	
135 	return (messageFieldsCount == fieldsCount);
136 }
137 
138 /// All fields in styx message has right (for concrete type of message) ?
139 auto hasValidFieldsTypes(E...)(StyxObject[] msg...)
140 {
141 	bool isAllTypesValid = true;
142 	
143 	foreach (indexOfField, fieldType; E)
144 	{
145 		auto castedField = cast(fieldType) msg[indexOfField];
146 		
147 		if (castedField is null) 
148 		{
149 			isAllTypesValid = false;
150 			break;
151 		}
152 	}
153 	
154 	return isAllTypesValid;
155 }
156 
157 /// Message has right count of styx objects (for some message type)?
158 auto hasValidFieldsTypes(Type type, StyxObject[] msg...)
159 {
160 	bool isAllTypesValid = false;		
161 	
162 	with (STYX_MESSAGE_TYPE)
163 	{
164 		switch(type.getType) 
165 		{
166 			// version
167 			case R_VERSION:
168 				isAllTypesValid = hasValidFieldsTypes!Rversion(msg);
169 				break;
170 			case T_VERSION:
171 				isAllTypesValid = hasValidFieldsTypes!Tversion(msg);
172 				break;
173 			// auth
174 			case R_AUTH:
175 				isAllTypesValid = hasValidFieldsTypes!Rauth(msg);
176 				break;
177 			case T_AUTH:
178 				isAllTypesValid = hasValidFieldsTypes!Tauth(msg);
179 				break;
180 			// error
181 			case R_ERROR:
182 				isAllTypesValid = hasValidFieldsTypes!Rerror(msg);
183 				break;
184 			// flush
185 			case R_FLUSH:
186 				isAllTypesValid = hasValidFieldsTypes!Rflush(msg);
187 				break;
188 			case T_FLUSH:
189 				isAllTypesValid = hasValidFieldsTypes!Tflush(msg);
190 				break;
191 			// attach
192 			case R_ATTACH:
193 				isAllTypesValid = hasValidFieldsTypes!Rattach(msg);
194 				break;
195 			case T_ATTACH:
196 				isAllTypesValid = hasValidFieldsTypes!Tattach(msg);
197 				break;
198 			// walk
199 			case R_WALK:
200 				isAllTypesValid = hasValidFieldsTypes!Rwalk(msg);
201 				break;
202 			case T_WALK:
203 				isAllTypesValid = hasValidFieldsTypes!Twalk(msg);
204 				break;
205 			// open
206 			case R_OPEN:
207 				isAllTypesValid = hasValidFieldsTypes!Ropen(msg);
208 				break;
209 			case T_OPEN:
210 				isAllTypesValid = hasValidFieldsTypes!Topen(msg);
211 				break;
212 			// create
213 			case R_CREATE:
214 				isAllTypesValid = hasValidFieldsTypes!Rcreate(msg);
215 				break;
216 			case T_CREATE:
217 				isAllTypesValid = hasValidFieldsTypes!Tcreate(msg);
218 				break;
219 			// read
220 			case R_READ:
221 				isAllTypesValid = hasValidFieldsTypes!Rread(msg);
222 				break;
223 			case T_READ:
224 				isAllTypesValid = hasValidFieldsTypes!Tread(msg);
225 				break;
226 			// write
227 			case R_WRITE:
228 				isAllTypesValid = hasValidFieldsTypes!Rwrite(msg);
229 				break;
230 			case T_WRITE:
231 				isAllTypesValid = hasValidFieldsTypes!Twrite(msg);
232 				break;
233 			// clunk
234 			case R_CLUNK:
235 				isAllTypesValid = hasValidFieldsTypes!Rclunk(msg);
236 				break;
237 			case T_CLUNK:
238 				isAllTypesValid = hasValidFieldsTypes!Tclunk(msg);
239 				break;
240 			// remove
241 			case R_REMOVE:
242 				isAllTypesValid = hasValidFieldsTypes!Rremove(msg);
243 				break;
244 			case T_REMOVE:
245 				isAllTypesValid = hasValidFieldsTypes!Tremove(msg);
246 				break;
247 			// stat
248 			case R_STAT:
249 				isAllTypesValid = hasValidFieldsTypes!Rstat(msg);
250 				break;
251 			case T_STAT:
252 				isAllTypesValid = hasValidFieldsTypes!Tstat(msg);
253 				break;
254 			// wstat
255 			case R_WSTAT:
256 				isAllTypesValid = hasValidFieldsTypes!Rwstat(msg);
257 				break;
258 			case T_WSTAT:
259 				isAllTypesValid = hasValidFieldsTypes!Twstat(msg);
260 				break;
261 			default:
262 				//throw new Exception("Bad message type");
263 				break;
264 		}
265 	}
266 	
267 	return isAllTypesValid;
268 }