rivet-envoy-protocol 2.3.0-rc.15

Versioned Envoy protocol types for Rivet actor hosts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
// @generated initial scaffold by scripts/vbare-gen-converters
// from: v2.bare, to: v3.bare
// Replace each todo!() with the migration semantics, then drop the @generated marker.

#![allow(dead_code, unused_variables)]

use anyhow::{Result, bail};

use crate::generated::{v2, v3};

pub fn convert_kv_metadata_v2_to_v3(x: v2::KvMetadata) -> Result<v3::KvMetadata> {
	Ok(v3::KvMetadata {
		version: x.version,
		update_ts: x.update_ts,
	})
}

pub fn convert_kv_list_range_query_v2_to_v3(
	x: v2::KvListRangeQuery,
) -> Result<v3::KvListRangeQuery> {
	Ok(v3::KvListRangeQuery {
		start: x.start,
		end: x.end,
		exclusive: x.exclusive,
	})
}

pub fn convert_kv_list_prefix_query_v2_to_v3(
	x: v2::KvListPrefixQuery,
) -> Result<v3::KvListPrefixQuery> {
	Ok(v3::KvListPrefixQuery { key: x.key })
}

pub fn convert_kv_list_query_v2_to_v3(x: v2::KvListQuery) -> Result<v3::KvListQuery> {
	Ok(match x {
		v2::KvListQuery::KvListAllQuery => v3::KvListQuery::KvListAllQuery,
		v2::KvListQuery::KvListRangeQuery(v) => {
			v3::KvListQuery::KvListRangeQuery(convert_kv_list_range_query_v2_to_v3(v)?)
		}
		v2::KvListQuery::KvListPrefixQuery(v) => {
			v3::KvListQuery::KvListPrefixQuery(convert_kv_list_prefix_query_v2_to_v3(v)?)
		}
	})
}

pub fn convert_kv_get_request_v2_to_v3(x: v2::KvGetRequest) -> Result<v3::KvGetRequest> {
	Ok(v3::KvGetRequest { keys: x.keys })
}

pub fn convert_kv_list_request_v2_to_v3(x: v2::KvListRequest) -> Result<v3::KvListRequest> {
	Ok(v3::KvListRequest {
		query: convert_kv_list_query_v2_to_v3(x.query)?,
		reverse: x.reverse,
		limit: x.limit,
	})
}

pub fn convert_kv_put_request_v2_to_v3(x: v2::KvPutRequest) -> Result<v3::KvPutRequest> {
	Ok(v3::KvPutRequest {
		keys: x.keys,
		values: x.values,
	})
}

pub fn convert_kv_delete_request_v2_to_v3(x: v2::KvDeleteRequest) -> Result<v3::KvDeleteRequest> {
	Ok(v3::KvDeleteRequest { keys: x.keys })
}

pub fn convert_kv_delete_range_request_v2_to_v3(
	x: v2::KvDeleteRangeRequest,
) -> Result<v3::KvDeleteRangeRequest> {
	Ok(v3::KvDeleteRangeRequest {
		start: x.start,
		end: x.end,
	})
}

pub fn convert_kv_error_response_v2_to_v3(x: v2::KvErrorResponse) -> Result<v3::KvErrorResponse> {
	Ok(v3::KvErrorResponse { message: x.message })
}

pub fn convert_kv_get_response_v2_to_v3(x: v2::KvGetResponse) -> Result<v3::KvGetResponse> {
	Ok(v3::KvGetResponse {
		keys: x.keys,
		values: x.values,
		metadata: x
			.metadata
			.into_iter()
			.map(|v| convert_kv_metadata_v2_to_v3(v))
			.collect::<Result<Vec<_>>>()?,
	})
}

pub fn convert_kv_list_response_v2_to_v3(x: v2::KvListResponse) -> Result<v3::KvListResponse> {
	Ok(v3::KvListResponse {
		keys: x.keys,
		values: x.values,
		metadata: x
			.metadata
			.into_iter()
			.map(|v| convert_kv_metadata_v2_to_v3(v))
			.collect::<Result<Vec<_>>>()?,
	})
}

pub fn convert_kv_request_data_v2_to_v3(x: v2::KvRequestData) -> Result<v3::KvRequestData> {
	Ok(match x {
		v2::KvRequestData::KvGetRequest(v) => {
			v3::KvRequestData::KvGetRequest(convert_kv_get_request_v2_to_v3(v)?)
		}
		v2::KvRequestData::KvListRequest(v) => {
			v3::KvRequestData::KvListRequest(convert_kv_list_request_v2_to_v3(v)?)
		}
		v2::KvRequestData::KvPutRequest(v) => {
			v3::KvRequestData::KvPutRequest(convert_kv_put_request_v2_to_v3(v)?)
		}
		v2::KvRequestData::KvDeleteRequest(v) => {
			v3::KvRequestData::KvDeleteRequest(convert_kv_delete_request_v2_to_v3(v)?)
		}
		v2::KvRequestData::KvDeleteRangeRequest(v) => {
			v3::KvRequestData::KvDeleteRangeRequest(convert_kv_delete_range_request_v2_to_v3(v)?)
		}
		v2::KvRequestData::KvDropRequest => v3::KvRequestData::KvDropRequest,
	})
}

pub fn convert_kv_response_data_v2_to_v3(x: v2::KvResponseData) -> Result<v3::KvResponseData> {
	Ok(match x {
		v2::KvResponseData::KvErrorResponse(v) => {
			v3::KvResponseData::KvErrorResponse(convert_kv_error_response_v2_to_v3(v)?)
		}
		v2::KvResponseData::KvGetResponse(v) => {
			v3::KvResponseData::KvGetResponse(convert_kv_get_response_v2_to_v3(v)?)
		}
		v2::KvResponseData::KvListResponse(v) => {
			v3::KvResponseData::KvListResponse(convert_kv_list_response_v2_to_v3(v)?)
		}
		v2::KvResponseData::KvPutResponse => v3::KvResponseData::KvPutResponse,
		v2::KvResponseData::KvDeleteResponse => v3::KvResponseData::KvDeleteResponse,
		v2::KvResponseData::KvDropResponse => v3::KvResponseData::KvDropResponse,
	})
}

pub fn convert_sqlite_dirty_page_v2_to_v3(x: v2::SqliteDirtyPage) -> Result<v3::SqliteDirtyPage> {
	Ok(v3::SqliteDirtyPage {
		pgno: x.pgno,
		bytes: x.bytes,
	})
}

pub fn convert_sqlite_fetched_page_v2_to_v3(
	x: v2::SqliteFetchedPage,
) -> Result<v3::SqliteFetchedPage> {
	Ok(v3::SqliteFetchedPage {
		pgno: x.pgno,
		bytes: x.bytes,
	})
}

pub fn convert_sqlite_get_pages_request_v2_to_v3(
	x: v2::SqliteGetPagesRequest,
) -> Result<v3::SqliteGetPagesRequest> {
	Ok(v3::SqliteGetPagesRequest {
		actor_id: x.actor_id,
		pgnos: x.pgnos,
		expected_generation: todo!(),
		expected_head_txid: todo!(),
	})
}

pub fn convert_sqlite_get_pages_ok_v2_to_v3(
	x: v2::SqliteGetPagesOk,
) -> Result<v3::SqliteGetPagesOk> {
	Ok(v3::SqliteGetPagesOk {
		pages: x
			.pages
			.into_iter()
			.map(|v| convert_sqlite_fetched_page_v2_to_v3(v))
			.collect::<Result<Vec<_>>>()?,
	})
}

pub fn convert_sqlite_error_response_v2_to_v3(
	x: v2::SqliteErrorResponse,
) -> Result<v3::SqliteErrorResponse> {
	Ok(v3::SqliteErrorResponse { message: x.message })
}

pub fn convert_sqlite_get_pages_response_v2_to_v3(
	x: v2::SqliteGetPagesResponse,
) -> Result<v3::SqliteGetPagesResponse> {
	Ok(match x {
		v2::SqliteGetPagesResponse::SqliteGetPagesOk(v) => {
			v3::SqliteGetPagesResponse::SqliteGetPagesOk(convert_sqlite_get_pages_ok_v2_to_v3(v)?)
		}
		v2::SqliteGetPagesResponse::SqliteFenceMismatch(_) => todo!(),
		v2::SqliteGetPagesResponse::SqliteErrorResponse(v) => {
			v3::SqliteGetPagesResponse::SqliteErrorResponse(convert_sqlite_error_response_v2_to_v3(
				v,
			)?)
		}
	})
}

pub fn convert_sqlite_commit_request_v2_to_v3(
	x: v2::SqliteCommitRequest,
) -> Result<v3::SqliteCommitRequest> {
	Ok(v3::SqliteCommitRequest {
		actor_id: x.actor_id,
		dirty_pages: x
			.dirty_pages
			.into_iter()
			.map(|v| convert_sqlite_dirty_page_v2_to_v3(v))
			.collect::<Result<Vec<_>>>()?,
		db_size_pages: todo!(),
		now_ms: todo!(),
		expected_generation: todo!(),
		expected_head_txid: todo!(),
	})
}

pub fn convert_sqlite_commit_response_v2_to_v3(
	x: v2::SqliteCommitResponse,
) -> Result<v3::SqliteCommitResponse> {
	Ok(match x {
		v2::SqliteCommitResponse::SqliteCommitOk(_) => todo!(),
		v2::SqliteCommitResponse::SqliteFenceMismatch(_) => todo!(),
		v2::SqliteCommitResponse::SqliteCommitTooLarge(_) => todo!(),
		v2::SqliteCommitResponse::SqliteErrorResponse(v) => {
			v3::SqliteCommitResponse::SqliteErrorResponse(convert_sqlite_error_response_v2_to_v3(
				v,
			)?)
		}
	})
}

pub fn convert_stop_code_v2_to_v3(x: v2::StopCode) -> Result<v3::StopCode> {
	Ok(match x {
		v2::StopCode::Ok => v3::StopCode::Ok,
		v2::StopCode::Error => v3::StopCode::Error,
	})
}

pub fn convert_actor_name_v2_to_v3(x: v2::ActorName) -> Result<v3::ActorName> {
	Ok(v3::ActorName {
		metadata: x.metadata,
	})
}

pub fn convert_actor_config_v2_to_v3(x: v2::ActorConfig) -> Result<v3::ActorConfig> {
	Ok(v3::ActorConfig {
		name: x.name,
		key: x.key,
		create_ts: x.create_ts,
		input: x.input,
	})
}

pub fn convert_actor_checkpoint_v2_to_v3(x: v2::ActorCheckpoint) -> Result<v3::ActorCheckpoint> {
	Ok(v3::ActorCheckpoint {
		actor_id: x.actor_id,
		generation: x.generation,
		index: x.index,
	})
}

pub fn convert_actor_intent_v2_to_v3(x: v2::ActorIntent) -> Result<v3::ActorIntent> {
	Ok(match x {
		v2::ActorIntent::ActorIntentSleep => v3::ActorIntent::ActorIntentSleep,
		v2::ActorIntent::ActorIntentStop => v3::ActorIntent::ActorIntentStop,
	})
}

pub fn convert_actor_state_stopped_v2_to_v3(
	x: v2::ActorStateStopped,
) -> Result<v3::ActorStateStopped> {
	Ok(v3::ActorStateStopped {
		code: convert_stop_code_v2_to_v3(x.code)?,
		message: x.message,
	})
}

pub fn convert_actor_state_v2_to_v3(x: v2::ActorState) -> Result<v3::ActorState> {
	Ok(match x {
		v2::ActorState::ActorStateRunning => v3::ActorState::ActorStateRunning,
		v2::ActorState::ActorStateStopped(v) => {
			v3::ActorState::ActorStateStopped(convert_actor_state_stopped_v2_to_v3(v)?)
		}
	})
}

pub fn convert_event_actor_intent_v2_to_v3(
	x: v2::EventActorIntent,
) -> Result<v3::EventActorIntent> {
	Ok(v3::EventActorIntent {
		intent: convert_actor_intent_v2_to_v3(x.intent)?,
	})
}

pub fn convert_event_actor_state_update_v2_to_v3(
	x: v2::EventActorStateUpdate,
) -> Result<v3::EventActorStateUpdate> {
	Ok(v3::EventActorStateUpdate {
		state: convert_actor_state_v2_to_v3(x.state)?,
	})
}

pub fn convert_event_actor_set_alarm_v2_to_v3(
	x: v2::EventActorSetAlarm,
) -> Result<v3::EventActorSetAlarm> {
	Ok(v3::EventActorSetAlarm {
		alarm_ts: x.alarm_ts,
	})
}

pub fn convert_event_v2_to_v3(x: v2::Event) -> Result<v3::Event> {
	Ok(match x {
		v2::Event::EventActorIntent(v) => {
			v3::Event::EventActorIntent(convert_event_actor_intent_v2_to_v3(v)?)
		}
		v2::Event::EventActorStateUpdate(v) => {
			v3::Event::EventActorStateUpdate(convert_event_actor_state_update_v2_to_v3(v)?)
		}
		v2::Event::EventActorSetAlarm(v) => {
			v3::Event::EventActorSetAlarm(convert_event_actor_set_alarm_v2_to_v3(v)?)
		}
	})
}

pub fn convert_event_wrapper_v2_to_v3(x: v2::EventWrapper) -> Result<v3::EventWrapper> {
	Ok(v3::EventWrapper {
		checkpoint: convert_actor_checkpoint_v2_to_v3(x.checkpoint)?,
		inner: convert_event_v2_to_v3(x.inner)?,
	})
}

pub fn convert_preloaded_kv_entry_v2_to_v3(
	x: v2::PreloadedKvEntry,
) -> Result<v3::PreloadedKvEntry> {
	Ok(v3::PreloadedKvEntry {
		key: x.key,
		value: x.value,
		metadata: convert_kv_metadata_v2_to_v3(x.metadata)?,
	})
}

pub fn convert_preloaded_kv_v2_to_v3(x: v2::PreloadedKv) -> Result<v3::PreloadedKv> {
	Ok(v3::PreloadedKv {
		entries: x
			.entries
			.into_iter()
			.map(|v| convert_preloaded_kv_entry_v2_to_v3(v))
			.collect::<Result<Vec<_>>>()?,
		requested_get_keys: x.requested_get_keys,
		requested_prefixes: x.requested_prefixes,
	})
}

pub fn convert_hibernating_request_v2_to_v3(
	x: v2::HibernatingRequest,
) -> Result<v3::HibernatingRequest> {
	Ok(v3::HibernatingRequest {
		gateway_id: x.gateway_id,
		request_id: x.request_id,
	})
}

pub fn convert_command_start_actor_v2_to_v3(
	x: v2::CommandStartActor,
) -> Result<v3::CommandStartActor> {
	Ok(v3::CommandStartActor {
		config: convert_actor_config_v2_to_v3(x.config)?,
		hibernating_requests: x
			.hibernating_requests
			.into_iter()
			.map(|v| convert_hibernating_request_v2_to_v3(v))
			.collect::<Result<Vec<_>>>()?,
		preloaded_kv: x
			.preloaded_kv
			.map(|v| convert_preloaded_kv_v2_to_v3(v))
			.transpose()?,
	})
}

pub fn convert_stop_actor_reason_v2_to_v3(x: v2::StopActorReason) -> Result<v3::StopActorReason> {
	Ok(match x {
		v2::StopActorReason::SleepIntent => v3::StopActorReason::SleepIntent,
		v2::StopActorReason::StopIntent => v3::StopActorReason::StopIntent,
		v2::StopActorReason::Destroy => v3::StopActorReason::Destroy,
		v2::StopActorReason::GoingAway => v3::StopActorReason::GoingAway,
		v2::StopActorReason::Lost => v3::StopActorReason::Lost,
	})
}

pub fn convert_command_stop_actor_v2_to_v3(
	x: v2::CommandStopActor,
) -> Result<v3::CommandStopActor> {
	Ok(v3::CommandStopActor {
		reason: convert_stop_actor_reason_v2_to_v3(x.reason)?,
	})
}

pub fn convert_command_v2_to_v3(x: v2::Command) -> Result<v3::Command> {
	Ok(match x {
		v2::Command::CommandStartActor(v) => {
			v3::Command::CommandStartActor(convert_command_start_actor_v2_to_v3(v)?)
		}
		v2::Command::CommandStopActor(v) => {
			v3::Command::CommandStopActor(convert_command_stop_actor_v2_to_v3(v)?)
		}
	})
}

pub fn convert_command_wrapper_v2_to_v3(x: v2::CommandWrapper) -> Result<v3::CommandWrapper> {
	Ok(v3::CommandWrapper {
		checkpoint: convert_actor_checkpoint_v2_to_v3(x.checkpoint)?,
		inner: convert_command_v2_to_v3(x.inner)?,
	})
}

pub fn convert_actor_command_key_data_v2_to_v3(
	x: v2::ActorCommandKeyData,
) -> Result<v3::ActorCommandKeyData> {
	Ok(match x {
		v2::ActorCommandKeyData::CommandStartActor(v) => {
			v3::ActorCommandKeyData::CommandStartActor(convert_command_start_actor_v2_to_v3(v)?)
		}
		v2::ActorCommandKeyData::CommandStopActor(v) => {
			v3::ActorCommandKeyData::CommandStopActor(convert_command_stop_actor_v2_to_v3(v)?)
		}
	})
}

pub fn convert_message_id_v2_to_v3(x: v2::MessageId) -> Result<v3::MessageId> {
	Ok(v3::MessageId {
		gateway_id: x.gateway_id,
		request_id: x.request_id,
		message_index: x.message_index,
	})
}

pub fn convert_to_envoy_request_start_v2_to_v3(
	x: v2::ToEnvoyRequestStart,
) -> Result<v3::ToEnvoyRequestStart> {
	Ok(v3::ToEnvoyRequestStart {
		actor_id: x.actor_id,
		method: x.method,
		path: x.path,
		headers: x.headers,
		body: x.body,
		stream: x.stream,
	})
}

pub fn convert_to_envoy_request_chunk_v2_to_v3(
	x: v2::ToEnvoyRequestChunk,
) -> Result<v3::ToEnvoyRequestChunk> {
	Ok(v3::ToEnvoyRequestChunk {
		body: x.body,
		finish: x.finish,
	})
}

pub fn convert_to_rivet_response_start_v2_to_v3(
	x: v2::ToRivetResponseStart,
) -> Result<v3::ToRivetResponseStart> {
	Ok(v3::ToRivetResponseStart {
		status: x.status,
		headers: x.headers,
		body: x.body,
		stream: x.stream,
	})
}

pub fn convert_to_rivet_response_chunk_v2_to_v3(
	x: v2::ToRivetResponseChunk,
) -> Result<v3::ToRivetResponseChunk> {
	Ok(v3::ToRivetResponseChunk {
		body: x.body,
		finish: x.finish,
	})
}

pub fn convert_to_envoy_web_socket_open_v2_to_v3(
	x: v2::ToEnvoyWebSocketOpen,
) -> Result<v3::ToEnvoyWebSocketOpen> {
	Ok(v3::ToEnvoyWebSocketOpen {
		actor_id: x.actor_id,
		path: x.path,
		headers: x.headers,
	})
}

pub fn convert_to_envoy_web_socket_message_v2_to_v3(
	x: v2::ToEnvoyWebSocketMessage,
) -> Result<v3::ToEnvoyWebSocketMessage> {
	Ok(v3::ToEnvoyWebSocketMessage {
		data: x.data,
		binary: x.binary,
	})
}

pub fn convert_to_envoy_web_socket_close_v2_to_v3(
	x: v2::ToEnvoyWebSocketClose,
) -> Result<v3::ToEnvoyWebSocketClose> {
	Ok(v3::ToEnvoyWebSocketClose {
		code: x.code,
		reason: x.reason,
	})
}

pub fn convert_to_rivet_web_socket_open_v2_to_v3(
	x: v2::ToRivetWebSocketOpen,
) -> Result<v3::ToRivetWebSocketOpen> {
	Ok(v3::ToRivetWebSocketOpen {
		can_hibernate: x.can_hibernate,
	})
}

pub fn convert_to_rivet_web_socket_message_v2_to_v3(
	x: v2::ToRivetWebSocketMessage,
) -> Result<v3::ToRivetWebSocketMessage> {
	Ok(v3::ToRivetWebSocketMessage {
		data: x.data,
		binary: x.binary,
	})
}

pub fn convert_to_rivet_web_socket_message_ack_v2_to_v3(
	x: v2::ToRivetWebSocketMessageAck,
) -> Result<v3::ToRivetWebSocketMessageAck> {
	Ok(v3::ToRivetWebSocketMessageAck { index: x.index })
}

pub fn convert_to_rivet_web_socket_close_v2_to_v3(
	x: v2::ToRivetWebSocketClose,
) -> Result<v3::ToRivetWebSocketClose> {
	Ok(v3::ToRivetWebSocketClose {
		code: x.code,
		reason: x.reason,
		hibernate: x.hibernate,
	})
}

pub fn convert_to_rivet_tunnel_message_kind_v2_to_v3(
	x: v2::ToRivetTunnelMessageKind,
) -> Result<v3::ToRivetTunnelMessageKind> {
	Ok(match x {
		v2::ToRivetTunnelMessageKind::ToRivetResponseStart(v) => {
			v3::ToRivetTunnelMessageKind::ToRivetResponseStart(
				convert_to_rivet_response_start_v2_to_v3(v)?,
			)
		}
		v2::ToRivetTunnelMessageKind::ToRivetResponseChunk(v) => {
			v3::ToRivetTunnelMessageKind::ToRivetResponseChunk(
				convert_to_rivet_response_chunk_v2_to_v3(v)?,
			)
		}
		v2::ToRivetTunnelMessageKind::ToRivetResponseAbort => {
			v3::ToRivetTunnelMessageKind::ToRivetResponseAbort
		}
		v2::ToRivetTunnelMessageKind::ToRivetWebSocketOpen(v) => {
			v3::ToRivetTunnelMessageKind::ToRivetWebSocketOpen(
				convert_to_rivet_web_socket_open_v2_to_v3(v)?,
			)
		}
		v2::ToRivetTunnelMessageKind::ToRivetWebSocketMessage(v) => {
			v3::ToRivetTunnelMessageKind::ToRivetWebSocketMessage(
				convert_to_rivet_web_socket_message_v2_to_v3(v)?,
			)
		}
		v2::ToRivetTunnelMessageKind::ToRivetWebSocketMessageAck(v) => {
			v3::ToRivetTunnelMessageKind::ToRivetWebSocketMessageAck(
				convert_to_rivet_web_socket_message_ack_v2_to_v3(v)?,
			)
		}
		v2::ToRivetTunnelMessageKind::ToRivetWebSocketClose(v) => {
			v3::ToRivetTunnelMessageKind::ToRivetWebSocketClose(
				convert_to_rivet_web_socket_close_v2_to_v3(v)?,
			)
		}
	})
}

pub fn convert_to_rivet_tunnel_message_v2_to_v3(
	x: v2::ToRivetTunnelMessage,
) -> Result<v3::ToRivetTunnelMessage> {
	Ok(v3::ToRivetTunnelMessage {
		message_id: convert_message_id_v2_to_v3(x.message_id)?,
		message_kind: convert_to_rivet_tunnel_message_kind_v2_to_v3(x.message_kind)?,
	})
}

pub fn convert_to_envoy_tunnel_message_kind_v2_to_v3(
	x: v2::ToEnvoyTunnelMessageKind,
) -> Result<v3::ToEnvoyTunnelMessageKind> {
	Ok(match x {
		v2::ToEnvoyTunnelMessageKind::ToEnvoyRequestStart(v) => {
			v3::ToEnvoyTunnelMessageKind::ToEnvoyRequestStart(
				convert_to_envoy_request_start_v2_to_v3(v)?,
			)
		}
		v2::ToEnvoyTunnelMessageKind::ToEnvoyRequestChunk(v) => {
			v3::ToEnvoyTunnelMessageKind::ToEnvoyRequestChunk(
				convert_to_envoy_request_chunk_v2_to_v3(v)?,
			)
		}
		v2::ToEnvoyTunnelMessageKind::ToEnvoyRequestAbort => {
			v3::ToEnvoyTunnelMessageKind::ToEnvoyRequestAbort
		}
		v2::ToEnvoyTunnelMessageKind::ToEnvoyWebSocketOpen(v) => {
			v3::ToEnvoyTunnelMessageKind::ToEnvoyWebSocketOpen(
				convert_to_envoy_web_socket_open_v2_to_v3(v)?,
			)
		}
		v2::ToEnvoyTunnelMessageKind::ToEnvoyWebSocketMessage(v) => {
			v3::ToEnvoyTunnelMessageKind::ToEnvoyWebSocketMessage(
				convert_to_envoy_web_socket_message_v2_to_v3(v)?,
			)
		}
		v2::ToEnvoyTunnelMessageKind::ToEnvoyWebSocketClose(v) => {
			v3::ToEnvoyTunnelMessageKind::ToEnvoyWebSocketClose(
				convert_to_envoy_web_socket_close_v2_to_v3(v)?,
			)
		}
	})
}

pub fn convert_to_envoy_tunnel_message_v2_to_v3(
	x: v2::ToEnvoyTunnelMessage,
) -> Result<v3::ToEnvoyTunnelMessage> {
	Ok(v3::ToEnvoyTunnelMessage {
		message_id: convert_message_id_v2_to_v3(x.message_id)?,
		message_kind: convert_to_envoy_tunnel_message_kind_v2_to_v3(x.message_kind)?,
	})
}

pub fn convert_to_envoy_ping_v2_to_v3(x: v2::ToEnvoyPing) -> Result<v3::ToEnvoyPing> {
	Ok(v3::ToEnvoyPing { ts: x.ts })
}

pub fn convert_to_rivet_metadata_v2_to_v3(x: v2::ToRivetMetadata) -> Result<v3::ToRivetMetadata> {
	Ok(v3::ToRivetMetadata {
		prepopulate_actor_names: x
			.prepopulate_actor_names
			.map(|v| {
				v.into_iter()
					.map(|(k, v)| -> Result<_> { Ok((k, convert_actor_name_v2_to_v3(v)?)) })
					.collect::<Result<_>>()
			})
			.transpose()?,
		metadata: x.metadata,
	})
}

pub fn convert_to_rivet_events_v2_to_v3(x: v2::ToRivetEvents) -> Result<v3::ToRivetEvents> {
	Ok(x.into_iter()
		.map(|v| convert_event_wrapper_v2_to_v3(v))
		.collect::<Result<Vec<_>>>()?)
}

pub fn convert_to_rivet_ack_commands_v2_to_v3(
	x: v2::ToRivetAckCommands,
) -> Result<v3::ToRivetAckCommands> {
	Ok(v3::ToRivetAckCommands {
		last_command_checkpoints: x
			.last_command_checkpoints
			.into_iter()
			.map(|v| convert_actor_checkpoint_v2_to_v3(v))
			.collect::<Result<Vec<_>>>()?,
	})
}

pub fn convert_to_rivet_pong_v2_to_v3(x: v2::ToRivetPong) -> Result<v3::ToRivetPong> {
	Ok(v3::ToRivetPong { ts: x.ts })
}

pub fn convert_to_rivet_kv_request_v2_to_v3(
	x: v2::ToRivetKvRequest,
) -> Result<v3::ToRivetKvRequest> {
	Ok(v3::ToRivetKvRequest {
		actor_id: x.actor_id,
		request_id: x.request_id,
		data: convert_kv_request_data_v2_to_v3(x.data)?,
	})
}

pub fn convert_to_rivet_sqlite_get_pages_request_v2_to_v3(
	x: v2::ToRivetSqliteGetPagesRequest,
) -> Result<v3::ToRivetSqliteGetPagesRequest> {
	Ok(v3::ToRivetSqliteGetPagesRequest {
		request_id: x.request_id,
		data: convert_sqlite_get_pages_request_v2_to_v3(x.data)?,
	})
}

pub fn convert_to_rivet_sqlite_commit_request_v2_to_v3(
	x: v2::ToRivetSqliteCommitRequest,
) -> Result<v3::ToRivetSqliteCommitRequest> {
	Ok(v3::ToRivetSqliteCommitRequest {
		request_id: x.request_id,
		data: convert_sqlite_commit_request_v2_to_v3(x.data)?,
	})
}

pub fn convert_to_rivet_v2_to_v3(x: v2::ToRivet) -> Result<v3::ToRivet> {
	Ok(match x {
		v2::ToRivet::ToRivetMetadata(v) => {
			v3::ToRivet::ToRivetMetadata(convert_to_rivet_metadata_v2_to_v3(v)?)
		}
		v2::ToRivet::ToRivetEvents(v) => {
			v3::ToRivet::ToRivetEvents(convert_to_rivet_events_v2_to_v3(v)?)
		}
		v2::ToRivet::ToRivetAckCommands(v) => {
			v3::ToRivet::ToRivetAckCommands(convert_to_rivet_ack_commands_v2_to_v3(v)?)
		}
		v2::ToRivet::ToRivetStopping => v3::ToRivet::ToRivetStopping,
		v2::ToRivet::ToRivetPong(v) => v3::ToRivet::ToRivetPong(convert_to_rivet_pong_v2_to_v3(v)?),
		v2::ToRivet::ToRivetKvRequest(v) => {
			v3::ToRivet::ToRivetKvRequest(convert_to_rivet_kv_request_v2_to_v3(v)?)
		}
		v2::ToRivet::ToRivetTunnelMessage(v) => {
			v3::ToRivet::ToRivetTunnelMessage(convert_to_rivet_tunnel_message_v2_to_v3(v)?)
		}
		v2::ToRivet::ToRivetSqliteGetPagesRequest(_)
		| v2::ToRivet::ToRivetSqliteCommitRequest(_)
		| v2::ToRivet::ToRivetSqliteCommitStageBeginRequest(_)
		| v2::ToRivet::ToRivetSqliteCommitStageRequest(_)
		| v2::ToRivet::ToRivetSqliteCommitFinalizeRequest(_) => {
			bail!("legacy sqlite requests require envoy-protocol v2")
		}
	})
}

pub fn convert_protocol_metadata_v2_to_v3(x: v2::ProtocolMetadata) -> Result<v3::ProtocolMetadata> {
	Ok(v3::ProtocolMetadata {
		envoy_lost_threshold: x.envoy_lost_threshold,
		actor_stop_threshold: x.actor_stop_threshold,
		max_response_payload_size: x.max_response_payload_size,
	})
}

pub fn convert_to_envoy_init_v2_to_v3(x: v2::ToEnvoyInit) -> Result<v3::ToEnvoyInit> {
	Ok(v3::ToEnvoyInit {
		metadata: convert_protocol_metadata_v2_to_v3(x.metadata)?,
	})
}

pub fn convert_to_envoy_commands_v2_to_v3(x: v2::ToEnvoyCommands) -> Result<v3::ToEnvoyCommands> {
	Ok(x.into_iter()
		.map(|v| convert_command_wrapper_v2_to_v3(v))
		.collect::<Result<Vec<_>>>()?)
}

pub fn convert_to_envoy_ack_events_v2_to_v3(
	x: v2::ToEnvoyAckEvents,
) -> Result<v3::ToEnvoyAckEvents> {
	Ok(v3::ToEnvoyAckEvents {
		last_event_checkpoints: x
			.last_event_checkpoints
			.into_iter()
			.map(|v| convert_actor_checkpoint_v2_to_v3(v))
			.collect::<Result<Vec<_>>>()?,
	})
}

pub fn convert_to_envoy_kv_response_v2_to_v3(
	x: v2::ToEnvoyKvResponse,
) -> Result<v3::ToEnvoyKvResponse> {
	Ok(v3::ToEnvoyKvResponse {
		request_id: x.request_id,
		data: convert_kv_response_data_v2_to_v3(x.data)?,
	})
}

pub fn convert_to_envoy_sqlite_get_pages_response_v2_to_v3(
	x: v2::ToEnvoySqliteGetPagesResponse,
) -> Result<v3::ToEnvoySqliteGetPagesResponse> {
	Ok(v3::ToEnvoySqliteGetPagesResponse {
		request_id: x.request_id,
		data: convert_sqlite_get_pages_response_v2_to_v3(x.data)?,
	})
}

pub fn convert_to_envoy_sqlite_commit_response_v2_to_v3(
	x: v2::ToEnvoySqliteCommitResponse,
) -> Result<v3::ToEnvoySqliteCommitResponse> {
	Ok(v3::ToEnvoySqliteCommitResponse {
		request_id: x.request_id,
		data: convert_sqlite_commit_response_v2_to_v3(x.data)?,
	})
}

pub fn convert_to_envoy_v2_to_v3(x: v2::ToEnvoy) -> Result<v3::ToEnvoy> {
	Ok(match x {
		v2::ToEnvoy::ToEnvoyInit(v) => v3::ToEnvoy::ToEnvoyInit(convert_to_envoy_init_v2_to_v3(v)?),
		v2::ToEnvoy::ToEnvoyCommands(v) => {
			v3::ToEnvoy::ToEnvoyCommands(convert_to_envoy_commands_v2_to_v3(v)?)
		}
		v2::ToEnvoy::ToEnvoyAckEvents(v) => {
			v3::ToEnvoy::ToEnvoyAckEvents(convert_to_envoy_ack_events_v2_to_v3(v)?)
		}
		v2::ToEnvoy::ToEnvoyKvResponse(v) => {
			v3::ToEnvoy::ToEnvoyKvResponse(convert_to_envoy_kv_response_v2_to_v3(v)?)
		}
		v2::ToEnvoy::ToEnvoyTunnelMessage(v) => {
			v3::ToEnvoy::ToEnvoyTunnelMessage(convert_to_envoy_tunnel_message_v2_to_v3(v)?)
		}
		v2::ToEnvoy::ToEnvoyPing(v) => v3::ToEnvoy::ToEnvoyPing(convert_to_envoy_ping_v2_to_v3(v)?),
		v2::ToEnvoy::ToEnvoySqliteGetPagesResponse(_)
		| v2::ToEnvoy::ToEnvoySqliteCommitResponse(_)
		| v2::ToEnvoy::ToEnvoySqliteCommitStageBeginResponse(_)
		| v2::ToEnvoy::ToEnvoySqliteCommitStageResponse(_)
		| v2::ToEnvoy::ToEnvoySqliteCommitFinalizeResponse(_) => {
			bail!("legacy sqlite responses require envoy-protocol v2")
		}
	})
}

pub fn convert_to_envoy_conn_ping_v2_to_v3(x: v2::ToEnvoyConnPing) -> Result<v3::ToEnvoyConnPing> {
	Ok(v3::ToEnvoyConnPing {
		gateway_id: x.gateway_id,
		request_id: x.request_id,
		ts: x.ts,
	})
}

pub fn convert_to_envoy_conn_v2_to_v3(x: v2::ToEnvoyConn) -> Result<v3::ToEnvoyConn> {
	Ok(match x {
		v2::ToEnvoyConn::ToEnvoyConnPing(v) => {
			v3::ToEnvoyConn::ToEnvoyConnPing(convert_to_envoy_conn_ping_v2_to_v3(v)?)
		}
		v2::ToEnvoyConn::ToEnvoyConnClose => v3::ToEnvoyConn::ToEnvoyConnClose,
		v2::ToEnvoyConn::ToEnvoyCommands(v) => {
			v3::ToEnvoyConn::ToEnvoyCommands(convert_to_envoy_commands_v2_to_v3(v)?)
		}
		v2::ToEnvoyConn::ToEnvoyAckEvents(v) => {
			v3::ToEnvoyConn::ToEnvoyAckEvents(convert_to_envoy_ack_events_v2_to_v3(v)?)
		}
		v2::ToEnvoyConn::ToEnvoyTunnelMessage(v) => {
			v3::ToEnvoyConn::ToEnvoyTunnelMessage(convert_to_envoy_tunnel_message_v2_to_v3(v)?)
		}
	})
}

pub fn convert_to_gateway_pong_v2_to_v3(x: v2::ToGatewayPong) -> Result<v3::ToGatewayPong> {
	Ok(v3::ToGatewayPong {
		request_id: x.request_id,
		ts: x.ts,
	})
}

pub fn convert_to_gateway_v2_to_v3(x: v2::ToGateway) -> Result<v3::ToGateway> {
	Ok(match x {
		v2::ToGateway::ToGatewayPong(v) => {
			v3::ToGateway::ToGatewayPong(convert_to_gateway_pong_v2_to_v3(v)?)
		}
		v2::ToGateway::ToRivetTunnelMessage(v) => {
			v3::ToGateway::ToRivetTunnelMessage(convert_to_rivet_tunnel_message_v2_to_v3(v)?)
		}
	})
}

pub fn convert_to_outbound_actor_start_v2_to_v3(
	x: v2::ToOutboundActorStart,
) -> Result<v3::ToOutboundActorStart> {
	Ok(v3::ToOutboundActorStart {
		namespace_id: x.namespace_id,
		pool_name: x.pool_name,
		checkpoint: convert_actor_checkpoint_v2_to_v3(x.checkpoint)?,
		actor_config: convert_actor_config_v2_to_v3(x.actor_config)?,
	})
}

pub fn convert_to_outbound_v2_to_v3(x: v2::ToOutbound) -> Result<v3::ToOutbound> {
	Ok(match x {
		v2::ToOutbound::ToOutboundActorStart(v) => {
			v3::ToOutbound::ToOutboundActorStart(convert_to_outbound_actor_start_v2_to_v3(v)?)
		}
	})
}