1 // Written in the D programming language.
2 
3 /**
4 The module provides tools for convenient cast to some objects of 9P / Styx protocol.
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.extrautil.casts;
12 
13 private {
14 	import styx2000.protoconst.messages;
15 	
16 	import styx2000.protomsg.typeconv;
17 	
18 	import styx2000.protobj;
19 }
20 
21 /// Transform to R-message type
22 /// Throws: Exception if passed type is not T-type or type is R-error
23 auto toRtype(Type type)
24 {		
25 	auto rtype = new Type;	
26 	with (STYX_MESSAGE_TYPE)
27 	{
28 		switch(type.getType) {
29 			// version
30 			case T_VERSION:
31 				rtype.setType(R_VERSION);
32 				break;
33 			// auth
34 			case T_AUTH:
35 				rtype.setType(R_AUTH);
36 				break;
37 			// flush
38 			case T_FLUSH:
39 				rtype.setType(R_FLUSH);
40 				break;
41 			// attach
42 			case T_ATTACH:
43 				rtype.setType(R_ATTACH);
44 				break;
45 			// walk
46 			case T_WALK:
47 				rtype.setType(R_WALK);
48 				break;
49 			case T_OPEN:
50 				rtype.setType(R_OPEN);
51 				break;
52 			// create
53 			case T_CREATE:
54 				rtype.setType(R_CREATE);
55 				break;
56 			// read
57 			case T_READ:
58 				rtype.setType(R_READ);
59 				break;
60 			// write
61 			case T_WRITE:
62 				rtype.setType(R_WRITE);
63 				break;
64 			// clunk
65 			case T_CLUNK:
66 				rtype.setType(R_CLUNK);
67 				break;
68 			// remove
69 			case T_REMOVE:
70 				rtype.setType(R_REMOVE);
71 				break;
72 			// stat
73 			case T_STAT:
74 				rtype.setType(R_STAT);
75 				break;
76 			// wstat
77 			case T_WSTAT:
78 				rtype.setType(R_WSTAT);
79 				break;
80 			default:
81 				throw new Exception("Wrong message type used to get R-message type");
82 		}
83 	}
84 	return rtype;
85 }
86 
87 /// Transform to T-message type
88 /// Throws: Exception if passed type is not R-type or type is R-error
89 auto toTtype(Type type)
90 {		
91 	auto ttype = new Type;	
92 	with (STYX_MESSAGE_TYPE)
93 	{
94 		switch(type.getType) {
95 			// version
96 			case R_VERSION:
97 				ttype.setType(T_VERSION);
98 				break;
99 			// auth
100 			case R_AUTH:
101 				ttype.setType(T_AUTH);
102 				break;
103 			// flush
104 			case R_FLUSH:
105 				ttype.setType(T_FLUSH);
106 				break;
107 			// attach
108 			case R_ATTACH:
109 				ttype.setType(T_ATTACH);
110 				break;
111 			// walk
112 			case R_WALK:
113 				ttype.setType(T_WALK);
114 				break;
115 			case R_OPEN:
116 				ttype.setType(T_OPEN);
117 				break;
118 			// create
119 			case R_CREATE:
120 				ttype.setType(T_CREATE);
121 				break;
122 			// read
123 			case R_READ:
124 				ttype.setType(T_READ);
125 				break;
126 			// write
127 			case R_WRITE:
128 				ttype.setType(T_WRITE);
129 				break;
130 			// clunk
131 			case R_CLUNK:
132 				ttype.setType(T_CLUNK);
133 				break;
134 			// remove
135 			case R_REMOVE:
136 				ttype.setType(T_REMOVE);
137 				break;
138 			// stat
139 			case R_STAT:
140 				ttype.setType(T_STAT);
141 				break;
142 			// wstat
143 			case R_WSTAT:
144 				ttype.setType(T_WSTAT);
145 				break;
146 			default:
147 				throw new Exception("Wrong message type used to get T-message type");
148 		}
149 	}
150 	return ttype;
151 }
152 
153 /// Conversion from StyxObject to Afid type
154 alias toAfid = fromStyxObject!Afid;
155 
156 /// Conversion from StyxObject to Aname type
157 alias toAname = fromStyxObject!Aname;
158 
159 /// Conversion from StyxObject to Aqid type
160 alias toAqid = fromStyxObject!Aqid;
161 
162 /// Conversion from StyxObject to Count type
163 alias toCount = fromStyxObject!Count;
164 
165 /// Conversion from StyxObject to Data type
166 alias toData = fromStyxObject!Data;
167 
168 /// Conversion from StyxObject to Ename type
169 alias toEname = fromStyxObject!Ename;
170 
171 /// Conversion from StyxObject to Fid type
172 alias toFid = fromStyxObject!Fid;
173 
174 /// Conversion from StyxObject to Iounit type
175 alias toIounit = fromStyxObject!Iounit;
176 
177 /// Conversion from StyxObject to Mode type
178 alias toMode = fromStyxObject!Mode;
179 
180 /// Conversion from StyxObject to Msize type
181 alias toMsize = fromStyxObject!Msize;
182 
183 /// Conversion from StyxObject to Name type
184 alias toName = fromStyxObject!Name;
185 
186 /// Conversion from StyxObject to NewFid type
187 alias toNewFid = fromStyxObject!NewFid;
188 
189 /// Conversion from StyxObject to Nwname type
190 alias toNwname = fromStyxObject!Nwname;
191 
192 /// Conversion from StyxObject to Nwqid type
193 alias toNwqid = fromStyxObject!Nwqid;
194 
195 /// Conversion from StyxObject to Offset type
196 alias toOffset = fromStyxObject!Offset;
197 
198 /// Conversion from StyxObject to OldTag type
199 alias toOldTag = fromStyxObject!OldTag;
200 
201 /// Conversion from StyxObject to Perm type
202 alias toPerm = fromStyxObject!Perm;
203 
204 /// Conversion from StyxObject to Qid type
205 alias toQid = fromStyxObject!Qid;
206 
207 /// Conversion from StyxObject to Size type
208 alias toSize = fromStyxObject!Size;
209 
210 /// Conversion from StyxObject to Stat type
211 alias toStat = fromStyxObject!Stat;
212 
213 /// Conversion from StyxObject to Version type
214 alias toVersion = fromStyxObject!Version;
215 
216 /// Conversion from StyxObject to Tag type
217 alias toTag = fromStyxObject!Tag;
218 
219 /// Conversion from StyxObject to Type type
220 alias toType = fromStyxObject!Type;
221 
222 /// Conversion from StyxObject to Uname type
223 alias toUname = fromStyxObject!Uname;
224