1package sessionwire
2
3import (
4 "github.com/bnema/vev/internal/protocol"
5 "github.com/bnema/vev/internal/protocol/wire"
6)
7
8func routeNavigationActionToWire(message protocol.RouteNavigationAction) (*wire.RouteNavigationAction, error) {
9 if err := message.Validate(); err != nil {
10 return nil, err
11 }
12 return &wire.RouteNavigationAction{
13 CauseActionId: message.CauseActionID,
14 SnapshotGeneration: message.SnapshotGeneration,
15 Key: message.Key,
16 Generation: message.Generation,
17 }, nil
18}
19
20func routeNavigationActionFromWire(message *wire.RouteNavigationAction) (protocol.RouteNavigationAction, error) {
21 if message == nil {
22 return protocol.RouteNavigationAction{}, protocol.ErrInvalidRouteWire
23 }
24 action := protocol.RouteNavigationAction{
25 CauseActionID: message.GetCauseActionId(),
26 SnapshotGeneration: message.GetSnapshotGeneration(),
27 Key: message.GetKey(),
28 Generation: message.GetGeneration(),
29 }
30 if err := action.Validate(); err != nil {
31 return protocol.RouteNavigationAction{}, err
32 }
33 return action, nil
34}
35
36func routeCreateSessionActionToWire(message protocol.RouteCreateSessionAction) (*wire.RouteCreateSessionAction, error) {
37 if err := message.Validate(); err != nil {
38 return nil, err
39 }
40 return &wire.RouteCreateSessionAction{
41 CauseActionId: message.CauseActionID,
42 RequestId: message.RequestID,
43 SnapshotGeneration: message.SnapshotGeneration,
44 Key: message.Key,
45 Generation: message.Generation,
46 SessionName: message.SessionName,
47 }, nil
48}
49
50func routeCreateSessionActionFromWire(message *wire.RouteCreateSessionAction) (protocol.RouteCreateSessionAction, error) {
51 if message == nil {
52 return protocol.RouteCreateSessionAction{}, protocol.ErrInvalidRouteWire
53 }
54 action := protocol.RouteCreateSessionAction{
55 CauseActionID: message.GetCauseActionId(),
56 RequestID: message.GetRequestId(),
57 SnapshotGeneration: message.GetSnapshotGeneration(),
58 Key: message.GetKey(),
59 Generation: message.GetGeneration(),
60 SessionName: message.GetSessionName(),
61 }
62 if err := action.Validate(); err != nil {
63 return protocol.RouteCreateSessionAction{}, err
64 }
65 return action, nil
66}
67
68func routeRetiredToWire(message protocol.RouteRetired) (*wire.RouteRetired, error) {
69 if err := message.Validate(); err != nil {
70 return nil, err
71 }
72 return &wire.RouteRetired{Ref: routeRefToWire(message.Ref), Target: exactTargetToWire(&message.Target)}, nil
73}
74
75func routeRetiredFromWire(message *wire.RouteRetired) (protocol.RouteRetired, error) {
76 if message == nil {
77 return protocol.RouteRetired{}, protocol.ErrInvalidRouteWire
78 }
79 target, err := exactTargetFromWire(message.GetTarget())
80 if err != nil || target == nil {
81 return protocol.RouteRetired{}, protocol.ErrInvalidRouteWire
82 }
83 retired := protocol.RouteRetired{Ref: routeRefFromWire(message.GetRef()), Target: *target}
84 if err := retired.Validate(); err != nil {
85 return protocol.RouteRetired{}, err
86 }
87 return retired, nil
88}
89
90func routePositionToWire(message protocol.RoutePosition) (*wire.RoutePosition, error) {
91 if err := message.Validate(); err != nil {
92 return nil, err
93 }
94 return &wire.RoutePosition{Target: exactTargetToWire(&message.Target), ActiveTabId: string(message.ActiveTabID)}, nil
95}
96
97func routePositionFromWire(message *wire.RoutePosition) (protocol.RoutePosition, error) {
98 if message == nil {
99 return protocol.RoutePosition{}, protocol.ErrInvalidRouteWire
100 }
101 target, err := exactTargetFromWire(message.GetTarget())
102 if err != nil || target == nil {
103 return protocol.RoutePosition{}, protocol.ErrInvalidRouteWire
104 }
105 position := protocol.RoutePosition{Target: *target, ActiveTabID: mustTabIDString(message.GetActiveTabId())}
106 if err := position.Validate(); err != nil {
107 return protocol.RoutePosition{}, err
108 }
109 return position, nil
110}
111
112func samePeerSwitchRequestToWire(message protocol.SamePeerSwitchRequest) (*wire.SamePeerSwitchRequest, error) {
113 if err := message.Validate(); err != nil {
114 return nil, err
115 }
116 return &wire.SamePeerSwitchRequest{RequestId: message.RequestID, Target: exactTargetToWire(&message.Target), PreferredTabId: string(message.PreferredTabID)}, nil
117}
118
119func samePeerSwitchRequestFromWire(message *wire.SamePeerSwitchRequest) (protocol.SamePeerSwitchRequest, error) {
120 if message == nil {
121 return protocol.SamePeerSwitchRequest{}, protocol.ErrInvalidRouteWire
122 }
123 target, err := exactTargetFromWire(message.GetTarget())
124 if err != nil || target == nil {
125 return protocol.SamePeerSwitchRequest{}, protocol.ErrInvalidRouteWire
126 }
127 request := protocol.SamePeerSwitchRequest{RequestID: message.GetRequestId(), Target: *target, PreferredTabID: mustTabIDString(message.GetPreferredTabId())}
128 if err := request.Validate(); err != nil {
129 return protocol.SamePeerSwitchRequest{}, err
130 }
131 return request, nil
132}
133
134func samePeerSwitchFailureToWire(message protocol.SamePeerSwitchFailure) (*wire.SamePeerSwitchFailure, error) {
135 if err := message.Validate(); err != nil {
136 return nil, err
137 }
138 return &wire.SamePeerSwitchFailure{RequestId: message.RequestID, Code: uint32(message.Code)}, nil
139}
140
141func samePeerSwitchFailureFromWire(message *wire.SamePeerSwitchFailure) (protocol.SamePeerSwitchFailure, error) {
142 if message == nil {
143 return protocol.SamePeerSwitchFailure{}, protocol.ErrInvalidRouteWire
144 }
145 code, err := enum8[protocol.SamePeerSwitchFailureCode](message.GetCode())
146 if err != nil {
147 return protocol.SamePeerSwitchFailure{}, protocol.ErrInvalidRouteWire
148 }
149 failure := protocol.SamePeerSwitchFailure{RequestID: message.GetRequestId(), Code: code}
150 if err := failure.Validate(); err != nil {
151 return protocol.SamePeerSwitchFailure{}, err
152 }
153 return failure, nil
154}
155
156func recentRouteSnapshotToWire(message protocol.RecentRouteSnapshot) (*wire.RecentRouteSnapshot, error) {
157 if err := message.Validate(); err != nil {
158 return nil, err
159 }
160 activeEntry, err := recentRouteEntryToWire(message.ActiveEntry)
161 if err != nil {
162 return nil, err
163 }
164 out := &wire.RecentRouteSnapshot{
165 Generation: message.Generation,
166 Active: routeRefToWire(message.Active),
167 ActiveEntry: activeEntry,
168 Previous: routeRefToWire(message.Previous),
169 Home: routeRefToWire(message.Home),
170 }
171 for _, entry := range message.Entries {
172 converted, err := recentRouteEntryToWire(entry)
173 if err != nil {
174 return nil, err
175 }
176 out.Entries = append(out.Entries, converted)
177 }
178 for _, host := range message.Hosts {
179 out.Hosts = append(out.Hosts, routeHostToWire(host))
180 }
181 return out, nil
182}
183
184func recentRouteSnapshotFromWire(message *wire.RecentRouteSnapshot) (protocol.RecentRouteSnapshot, error) {
185 var snapshot protocol.RecentRouteSnapshot
186 if message == nil {
187 return snapshot, protocol.ErrInvalidRouteWire
188 }
189 snapshot.Generation = message.GetGeneration()
190 snapshot.Active = routeRefFromWire(message.GetActive())
191 activeEntry, err := recentRouteEntryFromWire(message.GetActiveEntry())
192 if err != nil {
193 return protocol.RecentRouteSnapshot{}, err
194 }
195 snapshot.ActiveEntry = activeEntry
196 snapshot.Previous = routeRefFromWire(message.GetPrevious())
197 snapshot.Home = routeRefFromWire(message.GetHome())
198 for _, entry := range message.GetEntries() {
199 converted, err := recentRouteEntryFromWire(entry)
200 if err != nil {
201 return protocol.RecentRouteSnapshot{}, err
202 }
203 snapshot.Entries = append(snapshot.Entries, converted)
204 }
205 for _, host := range message.GetHosts() {
206 converted, err := routeHostFromWire(host)
207 if err != nil {
208 return protocol.RecentRouteSnapshot{}, err
209 }
210 snapshot.Hosts = append(snapshot.Hosts, converted)
211 }
212 if err := snapshot.Validate(); err != nil {
213 return protocol.RecentRouteSnapshot{}, err
214 }
215 return snapshot, nil
216}
217
218func sessionCreationFailureToWire(message protocol.SessionCreationFailure) (*wire.SessionCreationFailure, error) {
219 if err := message.Validate(); err != nil {
220 return nil, err
221 }
222 return &wire.SessionCreationFailure{RequestId: message.RequestID, Code: uint32(message.Code)}, nil
223}
224
225func sessionCreationFailureFromWire(message *wire.SessionCreationFailure) (protocol.SessionCreationFailure, error) {
226 if message == nil {
227 return protocol.SessionCreationFailure{}, protocol.ErrInvalidRouteWire
228 }
229 code, err := enum8[protocol.RouteFailureCode](message.GetCode())
230 if err != nil {
231 return protocol.SessionCreationFailure{}, protocol.ErrInvalidRouteWire
232 }
233 failure := protocol.SessionCreationFailure{RequestID: message.GetRequestId(), Code: code}
234 if err := failure.Validate(); err != nil {
235 return protocol.SessionCreationFailure{}, err
236 }
237 return failure, nil
238}
239
240func attentionSubscriptionFromWire(message *wire.RouteAttentionSubscription) (protocol.RouteAttentionSubscription, error) {
241 var subscription protocol.RouteAttentionSubscription
242 if message == nil {
243 return subscription, nil
244 }
245 for _, target := range message.GetTargets() {
246 converted, err := attentionTargetFromWire(target)
247 if err != nil {
248 return protocol.RouteAttentionSubscription{}, err
249 }
250 subscription.Targets = append(subscription.Targets, converted)
251 }
252 if err := subscription.Validate(); err != nil {
253 return protocol.RouteAttentionSubscription{}, err
254 }
255 return subscription, nil
256}
257
258func inventoryResponseToWire(message protocol.NavigationInventoryResponse) (*wire.NavigationInventoryResponse, error) {
259 if err := protocol.ValidateNavigationInventoryResponse(message); err != nil {
260 return nil, err
261 }
262 groups, err := inventoryGroupsToWire(message.Groups)
263 if err != nil {
264 return nil, err
265 }
266 var resolved *wire.AttachTarget
267 if message.Resolved != nil {
268 converted, err := attachTargetToWire(*message.Resolved)
269 if err != nil {
270 return nil, err
271 }
272 resolved = converted
273 }
274 return &wire.NavigationInventoryResponse{
275 RequestId: message.RequestID,
276 Operation: uint32(message.Operation),
277 Status: uint32(message.Status),
278 Groups: groups,
279 Resolved: resolved,
280 }, nil
281}
282
283func inventoryResponseFromWire(message *wire.NavigationInventoryResponse) (protocol.NavigationInventoryResponse, error) {
284 var response protocol.NavigationInventoryResponse
285 if message == nil {
286 return response, protocol.ErrInvalidNavigation
287 }
288 response.RequestID = message.GetRequestId()
289 operation, err := enum8[protocol.NavigationInventoryOperation](message.GetOperation())
290 if err != nil {
291 return protocol.NavigationInventoryResponse{}, protocol.ErrInvalidNavigation
292 }
293 response.Operation = operation
294 status, err := enum8[protocol.NavigationInventoryStatus](message.GetStatus())
295 if err != nil {
296 return protocol.NavigationInventoryResponse{}, protocol.ErrInvalidNavigation
297 }
298 response.Status = status
299 groups, err := inventoryGroupsFromWire(message.GetGroups())
300 if err != nil {
301 return protocol.NavigationInventoryResponse{}, err
302 }
303 response.Groups = groups
304 if message.GetResolved() != nil {
305 resolved, err := attachTargetFromWire(message.GetResolved())
306 if err != nil {
307 return protocol.NavigationInventoryResponse{}, err
308 }
309 response.Resolved = &resolved
310 }
311 if err := protocol.ValidateNavigationInventoryResponse(response); err != nil {
312 return protocol.NavigationInventoryResponse{}, err
313 }
314 return response, nil
315}
316
317func inventoryDemandToWire(message protocol.NavigationInventoryDemand) (*wire.NavigationInventoryDemand, error) {
318 if err := protocol.ValidateNavigationInventoryDemand(message); err != nil {
319 return nil, err
320 }
321 return &wire.NavigationInventoryDemand{InteractionGeneration: message.InteractionGeneration, Open: message.Open}, nil
322}
323
324func inventoryDemandFromWire(message *wire.NavigationInventoryDemand) (protocol.NavigationInventoryDemand, error) {
325 if message == nil {
326 return protocol.NavigationInventoryDemand{}, protocol.ErrInvalidNavigation
327 }
328 demand := protocol.NavigationInventoryDemand{InteractionGeneration: message.GetInteractionGeneration(), Open: message.GetOpen()}
329 if err := protocol.ValidateNavigationInventoryDemand(demand); err != nil {
330 return protocol.NavigationInventoryDemand{}, err
331 }
332 return demand, nil
333}
334
335func inventorySelectionToWire(message protocol.NavigationInventorySelection) (*wire.NavigationInventorySelection, error) {
336 if err := protocol.ValidateNavigationInventorySelection(message); err != nil {
337 return nil, err
338 }
339 return &wire.NavigationInventorySelection{
340 CauseActionId: message.CauseActionID,
341 InteractionGeneration: message.InteractionGeneration,
342 PublicationGeneration: message.PublicationGeneration,
343 SourceKey: message.SourceKey,
344 EntryKey: message.EntryKey,
345 }, nil
346}
347
348func inventorySelectionFromWire(message *wire.NavigationInventorySelection) (protocol.NavigationInventorySelection, error) {
349 if message == nil {
350 return protocol.NavigationInventorySelection{}, protocol.ErrInvalidNavigation
351 }
352 selection := protocol.NavigationInventorySelection{
353 CauseActionID: message.GetCauseActionId(),
354 InteractionGeneration: message.GetInteractionGeneration(),
355 PublicationGeneration: message.GetPublicationGeneration(),
356 SourceKey: message.GetSourceKey(),
357 EntryKey: message.GetEntryKey(),
358 }
359 if err := protocol.ValidateNavigationInventorySelection(selection); err != nil {
360 return protocol.NavigationInventorySelection{}, err
361 }
362 return selection, nil
363}
364
365func inventoryPublicationToWire(message protocol.NavigationInventoryPublication) (*wire.NavigationInventoryPublication, error) {
366 if err := protocol.ValidateNavigationInventoryPublication(message); err != nil {
367 return nil, err
368 }
369 groups, err := inventoryGroupsToWire(message.Groups)
370 if err != nil {
371 return nil, err
372 }
373 return &wire.NavigationInventoryPublication{InteractionGeneration: message.InteractionGeneration, PublicationGeneration: message.PublicationGeneration, Groups: groups}, nil
374}
375
376func inventoryPublicationFromWire(message *wire.NavigationInventoryPublication) (protocol.NavigationInventoryPublication, error) {
377 if message == nil {
378 return protocol.NavigationInventoryPublication{}, protocol.ErrInvalidNavigation
379 }
380 publication := protocol.NavigationInventoryPublication{InteractionGeneration: message.GetInteractionGeneration(), PublicationGeneration: message.GetPublicationGeneration()}
381 groups, err := inventoryGroupsFromWire(message.GetGroups())
382 if err != nil {
383 return protocol.NavigationInventoryPublication{}, err
384 }
385 publication.Groups = groups
386 if err := protocol.ValidateNavigationInventoryPublication(publication); err != nil {
387 return protocol.NavigationInventoryPublication{}, err
388 }
389 return publication, nil
390}
391
392func inventoryFailureToWire(message protocol.NavigationInventoryFailure) (*wire.NavigationInventoryFailure, error) {
393 if err := protocol.ValidateNavigationInventoryFailure(message); err != nil {
394 return nil, err
395 }
396 return &wire.NavigationInventoryFailure{
397 CauseActionId: message.CauseActionID,
398 InteractionGeneration: message.InteractionGeneration,
399 SourceKey: message.SourceKey,
400 EntryKey: message.EntryKey,
401 Code: uint32(message.Code),
402 }, nil
403}
404
405func inventoryFailureFromWire(message *wire.NavigationInventoryFailure) (protocol.NavigationInventoryFailure, error) {
406 if message == nil {
407 return protocol.NavigationInventoryFailure{}, protocol.ErrInvalidNavigation
408 }
409 code, err := enum8[protocol.NavigationInventoryFailureCode](message.GetCode())
410 if err != nil {
411 return protocol.NavigationInventoryFailure{}, protocol.ErrInvalidNavigation
412 }
413 failure := protocol.NavigationInventoryFailure{
414 CauseActionID: message.GetCauseActionId(),
415 InteractionGeneration: message.GetInteractionGeneration(),
416 SourceKey: message.GetSourceKey(),
417 EntryKey: message.GetEntryKey(),
418 Code: code,
419 }
420 if err := protocol.ValidateNavigationInventoryFailure(failure); err != nil {
421 return protocol.NavigationInventoryFailure{}, err
422 }
423 return failure, nil
424}