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 			// version
34 			case R_VERSION:
35 				fieldsCount = STYX_MESSAGE_SIZE.R_VERSION;
36 				break;
37 			case T_VERSION:
38 				fieldsCount = STYX_MESSAGE_SIZE.T_VERSION;
39 				break;
40 			// auth
41 			case R_AUTH:
42 				fieldsCount = STYX_MESSAGE_SIZE.R_AUTH;
43 				break;
44 			case T_AUTH:
45 				fieldsCount = STYX_MESSAGE_SIZE.T_AUTH;
46 				break;
47 			// error
48 			case R_ERROR:
49 				fieldsCount = STYX_MESSAGE_SIZE.R_ERROR;
50 				break;
51 			// flush
52 			case R_FLUSH:
53 				fieldsCount = STYX_MESSAGE_SIZE.R_FLUSH;
54 				break;
55 			case T_FLUSH:
56 				fieldsCount = STYX_MESSAGE_SIZE.T_FLUSH;
57 				break;
58 			// attach
59 			case R_ATTACH:
60 				fieldsCount = STYX_MESSAGE_SIZE.R_ATTACH;
61 				break;
62 			case T_ATTACH:
63 				fieldsCount = STYX_MESSAGE_SIZE.T_ATTACH;
64 				break;
65 			// walk
66 			case R_WALK:
67 				fieldsCount = STYX_MESSAGE_SIZE.R_WALK;
68 				break;
69 			case T_WALK:
70 				fieldsCount = STYX_MESSAGE_SIZE.T_WALK;
71 				break;
72 			// open
73 			case R_OPEN:
74 				fieldsCount = STYX_MESSAGE_SIZE.R_OPEN;
75 				break;
76 			case T_OPEN:
77 				fieldsCount = STYX_MESSAGE_SIZE.T_OPEN;
78 				break;
79 			// create
80 			case R_CREATE:
81 				fieldsCount = STYX_MESSAGE_SIZE.R_CREATE;
82 				break;
83 			case T_CREATE:
84 				fieldsCount = STYX_MESSAGE_SIZE.T_CREATE;
85 				break;
86 			// read
87 			case R_READ:
88 				fieldsCount = STYX_MESSAGE_SIZE.R_READ;
89 				break;
90 			case T_READ:
91 				fieldsCount = STYX_MESSAGE_SIZE.T_READ;
92 				break;
93 			// write
94 			case R_WRITE:
95 				fieldsCount = STYX_MESSAGE_SIZE.R_WRITE;
96 				break;
97 			case T_WRITE:
98 				fieldsCount = STYX_MESSAGE_SIZE.T_WRITE;
99 				break;
100 			// clunk
101 			case R_CLUNK:
102 				fieldsCount = STYX_MESSAGE_SIZE.R_CLUNK;
103 				break;
104 			case T_CLUNK:
105 				fieldsCount = STYX_MESSAGE_SIZE.T_CLUNK;
106 				break;
107 			// remove
108 			case R_REMOVE:
109 				fieldsCount = STYX_MESSAGE_SIZE.R_REMOVE;
110 				break;
111 			case T_REMOVE:
112 				fieldsCount = STYX_MESSAGE_SIZE.T_REMOVE;
113 				break;
114 			// stat
115 			case R_STAT:
116 				fieldsCount = STYX_MESSAGE_SIZE.R_STAT;
117 				break;
118 			case T_STAT:
119 				fieldsCount = STYX_MESSAGE_SIZE.T_STAT;
120 				break;
121 			// wstat
122 			case R_WSTAT:
123 				fieldsCount = STYX_MESSAGE_SIZE.R_WSTAT;
124 				break;
125 			case T_WSTAT:
126 				fieldsCount = STYX_MESSAGE_SIZE.T_WSTAT;
127 				break;
128 			default:
129 				//throw new Exception("Bad message type");
130 				break;
131 		}
132 	}
133 	return (messageFieldsCount == fieldsCount);
134 }
135 
136 /// All fields in styx message has right (for concrete type of message) ?
137 auto hasValidFieldsTypes(E...)(StyxObject[] msg...)
138 {
139 	bool isAllTypesValid = true;
140 	foreach (indexOfField, fieldType; E)
141 	{
142 		auto castedField = cast(fieldType) msg[indexOfField];
143 		if (castedField is null) {
144 			isAllTypesValid = false;
145 			break;
146 		}
147 	}
148 	return isAllTypesValid;
149 }
150 
151 /// Message has right count of styx objects (for some message type)?
152 auto hasValidFieldsTypes(Type type, StyxObject[] msg...)
153 {
154 	bool isAllTypesValid = false;		
155 	with (STYX_MESSAGE_TYPE)
156 	{
157 		switch(type.getType) {
158 			// version
159 			case R_VERSION:
160 				isAllTypesValid = hasValidFieldsTypes!Rversion(msg);
161 				break;
162 			case T_VERSION:
163 				isAllTypesValid = hasValidFieldsTypes!Tversion(msg);
164 				break;
165 			// auth
166 			case R_AUTH:
167 				isAllTypesValid = hasValidFieldsTypes!Rauth(msg);
168 				break;
169 			case T_AUTH:
170 				isAllTypesValid = hasValidFieldsTypes!Tauth(msg);
171 				break;
172 			// error
173 			case R_ERROR:
174 				isAllTypesValid = hasValidFieldsTypes!Rerror(msg);
175 				break;
176 			// flush
177 			case R_FLUSH:
178 				isAllTypesValid = hasValidFieldsTypes!Rflush(msg);
179 				break;
180 			case T_FLUSH:
181 				isAllTypesValid = hasValidFieldsTypes!Tflush(msg);
182 				break;
183 			// attach
184 			case R_ATTACH:
185 				isAllTypesValid = hasValidFieldsTypes!Rattach(msg);
186 				break;
187 			case T_ATTACH:
188 				isAllTypesValid = hasValidFieldsTypes!Tattach(msg);
189 				break;
190 			// walk
191 			case R_WALK:
192 				isAllTypesValid = hasValidFieldsTypes!Rwalk(msg);
193 				break;
194 			case T_WALK:
195 				isAllTypesValid = hasValidFieldsTypes!Twalk(msg);
196 				break;
197 			// open
198 			case R_OPEN:
199 				isAllTypesValid = hasValidFieldsTypes!Ropen(msg);
200 				break;
201 			case T_OPEN:
202 				isAllTypesValid = hasValidFieldsTypes!Topen(msg);
203 				break;
204 			// create
205 			case R_CREATE:
206 				isAllTypesValid = hasValidFieldsTypes!Rcreate(msg);
207 				break;
208 			case T_CREATE:
209 				isAllTypesValid = hasValidFieldsTypes!Tcreate(msg);
210 				break;
211 			// read
212 			case R_READ:
213 				isAllTypesValid = hasValidFieldsTypes!Rread(msg);
214 				break;
215 			case T_READ:
216 				isAllTypesValid = hasValidFieldsTypes!Tread(msg);
217 				break;
218 			// write
219 			case R_WRITE:
220 				isAllTypesValid = hasValidFieldsTypes!Rwrite(msg);
221 				break;
222 			case T_WRITE:
223 				isAllTypesValid = hasValidFieldsTypes!Twrite(msg);
224 				break;
225 			// clunk
226 			case R_CLUNK:
227 				isAllTypesValid = hasValidFieldsTypes!Rclunk(msg);
228 				break;
229 			case T_CLUNK:
230 				isAllTypesValid = hasValidFieldsTypes!Tclunk(msg);
231 				break;
232 			// remove
233 			case R_REMOVE:
234 				isAllTypesValid = hasValidFieldsTypes!Rremove(msg);
235 				break;
236 			case T_REMOVE:
237 				isAllTypesValid = hasValidFieldsTypes!Tremove(msg);
238 				break;
239 			// stat
240 			case R_STAT:
241 				isAllTypesValid = hasValidFieldsTypes!Rstat(msg);
242 				break;
243 			case T_STAT:
244 				isAllTypesValid = hasValidFieldsTypes!Tstat(msg);
245 				break;
246 			// wstat
247 			case R_WSTAT:
248 				isAllTypesValid = hasValidFieldsTypes!Rwstat(msg);
249 				break;
250 			case T_WSTAT:
251 				isAllTypesValid = hasValidFieldsTypes!Twstat(msg);
252 				break;
253 			default:
254 				//throw new Exception("Bad message type");
255 				break;
256 		}
257 	}
258 	return isAllTypesValid;
259 }