guts-p2p 0.1.0

P2P networking layer for Guts decentralized code collaboration
Documentation
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
//! P2P protocol messages for collaboration replication (PRs, Issues, Comments, Reviews).

use bytes::{Buf, BufMut, Bytes, BytesMut};
use guts_collaboration::{
    Comment, CommentTarget, Issue, IssueState, Label, PullRequest, PullRequestState, Review,
    ReviewState,
};
use guts_storage::ObjectId;
use serde::{Deserialize, Serialize};

use crate::{P2PError, Result};

/// Collaboration message type discriminator.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum CollaborationMessageType {
    /// Pull request created.
    PullRequestCreated = 10,
    /// Pull request updated.
    PullRequestUpdated = 11,
    /// Issue created.
    IssueCreated = 12,
    /// Issue updated.
    IssueUpdated = 13,
    /// Comment created.
    CommentCreated = 14,
    /// Review created.
    ReviewCreated = 15,
    /// Request collaboration data sync.
    SyncCollaborationRequest = 16,
    /// Response with collaboration data.
    SyncCollaborationResponse = 17,
}

impl CollaborationMessageType {
    /// Parse a message type from a byte.
    pub fn from_byte(b: u8) -> Result<Self> {
        match b {
            10 => Ok(CollaborationMessageType::PullRequestCreated),
            11 => Ok(CollaborationMessageType::PullRequestUpdated),
            12 => Ok(CollaborationMessageType::IssueCreated),
            13 => Ok(CollaborationMessageType::IssueUpdated),
            14 => Ok(CollaborationMessageType::CommentCreated),
            15 => Ok(CollaborationMessageType::ReviewCreated),
            16 => Ok(CollaborationMessageType::SyncCollaborationRequest),
            17 => Ok(CollaborationMessageType::SyncCollaborationResponse),
            _ => Err(P2PError::InvalidMessage(format!(
                "unknown collaboration message type: {}",
                b
            ))),
        }
    }
}

/// Serializable version of a pull request for P2P transmission.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializablePullRequest {
    pub id: u64,
    pub repo_key: String,
    pub number: u32,
    pub title: String,
    pub description: String,
    pub author: String,
    pub state: String,
    pub source_branch: String,
    pub target_branch: String,
    pub source_commit: String,
    pub target_commit: String,
    pub labels: Vec<SerializableLabel>,
    pub created_at: u64,
    pub updated_at: u64,
    pub merged_at: Option<u64>,
    pub merged_by: Option<String>,
}

impl From<PullRequest> for SerializablePullRequest {
    fn from(pr: PullRequest) -> Self {
        Self {
            id: pr.id,
            repo_key: pr.repo_key,
            number: pr.number,
            title: pr.title,
            description: pr.description,
            author: pr.author,
            state: pr.state.to_string(),
            source_branch: pr.source_branch,
            target_branch: pr.target_branch,
            source_commit: pr.source_commit.to_hex(),
            target_commit: pr.target_commit.to_hex(),
            labels: pr.labels.into_iter().map(Into::into).collect(),
            created_at: pr.created_at,
            updated_at: pr.updated_at,
            merged_at: pr.merged_at,
            merged_by: pr.merged_by,
        }
    }
}

impl SerializablePullRequest {
    /// Convert back to a PullRequest.
    pub fn into_pull_request(self) -> Result<PullRequest> {
        let source_commit = ObjectId::from_hex(&self.source_commit)
            .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
        let target_commit = ObjectId::from_hex(&self.target_commit)
            .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;

        let state = match self.state.as_str() {
            "open" => PullRequestState::Open,
            "closed" => PullRequestState::Closed,
            "merged" => PullRequestState::Merged,
            s => return Err(P2PError::InvalidMessage(format!("invalid PR state: {}", s))),
        };

        let mut pr = PullRequest::new(
            self.id,
            self.repo_key,
            self.number,
            self.title,
            self.description,
            self.author,
            self.source_branch,
            self.target_branch,
            source_commit,
            target_commit,
        );

        // Set the stored values
        pr.id = self.id;
        pr.number = self.number;
        pr.state = state;
        pr.created_at = self.created_at;
        pr.updated_at = self.updated_at;
        pr.merged_at = self.merged_at;
        pr.merged_by = self.merged_by;

        for label in self.labels {
            pr.labels.push(label.into_label());
        }

        Ok(pr)
    }
}

/// Serializable version of an issue for P2P transmission.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializableIssue {
    pub id: u64,
    pub repo_key: String,
    pub number: u32,
    pub title: String,
    pub description: String,
    pub author: String,
    pub state: String,
    pub labels: Vec<SerializableLabel>,
    pub created_at: u64,
    pub updated_at: u64,
    pub closed_at: Option<u64>,
    pub closed_by: Option<String>,
}

impl From<Issue> for SerializableIssue {
    fn from(issue: Issue) -> Self {
        Self {
            id: issue.id,
            repo_key: issue.repo_key,
            number: issue.number,
            title: issue.title,
            description: issue.description,
            author: issue.author,
            state: issue.state.to_string(),
            labels: issue.labels.into_iter().map(Into::into).collect(),
            created_at: issue.created_at,
            updated_at: issue.updated_at,
            closed_at: issue.closed_at,
            closed_by: issue.closed_by,
        }
    }
}

impl SerializableIssue {
    /// Convert back to an Issue.
    pub fn into_issue(self) -> Result<Issue> {
        let state = match self.state.as_str() {
            "open" => IssueState::Open,
            "closed" => IssueState::Closed,
            s => {
                return Err(P2PError::InvalidMessage(format!(
                    "invalid issue state: {}",
                    s
                )))
            }
        };

        let mut issue = Issue::new(
            self.id,
            self.repo_key,
            self.number,
            self.title,
            self.description,
            self.author,
        );

        // Set the stored values
        issue.id = self.id;
        issue.number = self.number;
        issue.state = state;
        issue.created_at = self.created_at;
        issue.updated_at = self.updated_at;
        issue.closed_at = self.closed_at;
        issue.closed_by = self.closed_by;

        for label in self.labels {
            issue.labels.push(label.into_label());
        }

        Ok(issue)
    }
}

/// Serializable version of a label.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializableLabel {
    pub name: String,
    pub color: String,
    pub description: Option<String>,
}

impl From<Label> for SerializableLabel {
    fn from(label: Label) -> Self {
        Self {
            name: label.name,
            color: label.color,
            description: label.description,
        }
    }
}

impl SerializableLabel {
    /// Convert back to a Label.
    pub fn into_label(self) -> Label {
        let mut label = Label::new(self.name, self.color);
        if let Some(desc) = self.description {
            label = label.with_description(desc);
        }
        label
    }
}

/// Serializable version of a comment.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializableComment {
    pub id: u64,
    pub target_type: String,
    pub repo_key: String,
    pub number: u32,
    pub author: String,
    pub body: String,
    pub created_at: u64,
    pub updated_at: u64,
}

impl From<Comment> for SerializableComment {
    fn from(comment: Comment) -> Self {
        let (target_type, repo_key, number) = match &comment.target {
            CommentTarget::PullRequest { repo_key, number } => {
                ("pull_request".to_string(), repo_key.clone(), *number)
            }
            CommentTarget::Issue { repo_key, number } => {
                ("issue".to_string(), repo_key.clone(), *number)
            }
        };

        Self {
            id: comment.id,
            target_type,
            repo_key,
            number,
            author: comment.author,
            body: comment.body,
            created_at: comment.created_at,
            updated_at: comment.updated_at,
        }
    }
}

impl SerializableComment {
    /// Convert back to a Comment.
    pub fn into_comment(self) -> Result<Comment> {
        let target = match self.target_type.as_str() {
            "pull_request" => CommentTarget::pull_request(&self.repo_key, self.number),
            "issue" => CommentTarget::issue(&self.repo_key, self.number),
            t => {
                return Err(P2PError::InvalidMessage(format!(
                    "invalid comment target type: {}",
                    t
                )))
            }
        };

        let mut comment = Comment::new(self.id, target, self.author, self.body);
        comment.id = self.id;
        comment.created_at = self.created_at;
        comment.updated_at = self.updated_at;

        Ok(comment)
    }
}

/// Serializable version of a review.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SerializableReview {
    pub id: u64,
    pub repo_key: String,
    pub pr_number: u32,
    pub author: String,
    pub state: String,
    pub body: Option<String>,
    pub commit_id: String,
    pub created_at: u64,
}

impl From<Review> for SerializableReview {
    fn from(review: Review) -> Self {
        Self {
            id: review.id,
            repo_key: review.repo_key,
            pr_number: review.pr_number,
            author: review.author,
            state: review.state.to_string(),
            body: review.body,
            commit_id: review.commit_id,
            created_at: review.created_at,
        }
    }
}

impl SerializableReview {
    /// Convert back to a Review.
    pub fn into_review(self) -> Result<Review> {
        let state = match self.state.as_str() {
            "approved" => ReviewState::Approved,
            "changes_requested" => ReviewState::ChangesRequested,
            "commented" => ReviewState::Commented,
            "dismissed" => ReviewState::Dismissed,
            s => {
                return Err(P2PError::InvalidMessage(format!(
                    "invalid review state: {}",
                    s
                )))
            }
        };

        let mut review = Review::new(
            self.id,
            self.repo_key,
            self.pr_number,
            self.author,
            state,
            self.commit_id,
        );

        if let Some(body) = self.body {
            review = review.with_body(body);
        }

        review.id = self.id;
        review.created_at = self.created_at;

        Ok(review)
    }
}

/// Collaboration message for P2P transmission.
#[derive(Debug, Clone)]
pub enum CollaborationMessage {
    /// A new pull request was created.
    PullRequestCreated(SerializablePullRequest),
    /// A pull request was updated.
    PullRequestUpdated(SerializablePullRequest),
    /// A new issue was created.
    IssueCreated(SerializableIssue),
    /// An issue was updated.
    IssueUpdated(SerializableIssue),
    /// A new comment was created.
    CommentCreated(SerializableComment),
    /// A new review was created.
    ReviewCreated(SerializableReview),
    /// Request sync of collaboration data.
    SyncCollaborationRequest { repo_key: String },
    /// Response with collaboration data.
    SyncCollaborationResponse {
        repo_key: String,
        pull_requests: Vec<SerializablePullRequest>,
        issues: Vec<SerializableIssue>,
        comments: Vec<SerializableComment>,
        reviews: Vec<SerializableReview>,
    },
}

impl CollaborationMessage {
    /// Encode the message to bytes.
    pub fn encode(&self) -> Bytes {
        let mut buf = BytesMut::new();

        match self {
            CollaborationMessage::PullRequestCreated(pr) => {
                buf.put_u8(CollaborationMessageType::PullRequestCreated as u8);
                let json = serde_json::to_vec(pr).unwrap();
                buf.put_u32(json.len() as u32);
                buf.put_slice(&json);
            }
            CollaborationMessage::PullRequestUpdated(pr) => {
                buf.put_u8(CollaborationMessageType::PullRequestUpdated as u8);
                let json = serde_json::to_vec(pr).unwrap();
                buf.put_u32(json.len() as u32);
                buf.put_slice(&json);
            }
            CollaborationMessage::IssueCreated(issue) => {
                buf.put_u8(CollaborationMessageType::IssueCreated as u8);
                let json = serde_json::to_vec(issue).unwrap();
                buf.put_u32(json.len() as u32);
                buf.put_slice(&json);
            }
            CollaborationMessage::IssueUpdated(issue) => {
                buf.put_u8(CollaborationMessageType::IssueUpdated as u8);
                let json = serde_json::to_vec(issue).unwrap();
                buf.put_u32(json.len() as u32);
                buf.put_slice(&json);
            }
            CollaborationMessage::CommentCreated(comment) => {
                buf.put_u8(CollaborationMessageType::CommentCreated as u8);
                let json = serde_json::to_vec(comment).unwrap();
                buf.put_u32(json.len() as u32);
                buf.put_slice(&json);
            }
            CollaborationMessage::ReviewCreated(review) => {
                buf.put_u8(CollaborationMessageType::ReviewCreated as u8);
                let json = serde_json::to_vec(review).unwrap();
                buf.put_u32(json.len() as u32);
                buf.put_slice(&json);
            }
            CollaborationMessage::SyncCollaborationRequest { repo_key } => {
                buf.put_u8(CollaborationMessageType::SyncCollaborationRequest as u8);
                let repo_bytes = repo_key.as_bytes();
                buf.put_u16(repo_bytes.len() as u16);
                buf.put_slice(repo_bytes);
            }
            CollaborationMessage::SyncCollaborationResponse {
                repo_key,
                pull_requests,
                issues,
                comments,
                reviews,
            } => {
                buf.put_u8(CollaborationMessageType::SyncCollaborationResponse as u8);

                // Repo key
                let repo_bytes = repo_key.as_bytes();
                buf.put_u16(repo_bytes.len() as u16);
                buf.put_slice(repo_bytes);

                // PRs
                let pr_json = serde_json::to_vec(pull_requests).unwrap();
                buf.put_u32(pr_json.len() as u32);
                buf.put_slice(&pr_json);

                // Issues
                let issue_json = serde_json::to_vec(issues).unwrap();
                buf.put_u32(issue_json.len() as u32);
                buf.put_slice(&issue_json);

                // Comments
                let comment_json = serde_json::to_vec(comments).unwrap();
                buf.put_u32(comment_json.len() as u32);
                buf.put_slice(&comment_json);

                // Reviews
                let review_json = serde_json::to_vec(reviews).unwrap();
                buf.put_u32(review_json.len() as u32);
                buf.put_slice(&review_json);
            }
        }

        buf.freeze()
    }

    /// Decode a message from bytes.
    pub fn decode(data: &[u8]) -> Result<Self> {
        if data.is_empty() {
            return Err(P2PError::InvalidMessage(
                "empty collaboration message".into(),
            ));
        }

        let msg_type = CollaborationMessageType::from_byte(data[0])?;
        let mut payload = &data[1..];

        match msg_type {
            CollaborationMessageType::PullRequestCreated => {
                let len = read_u32(&mut payload)?;
                let pr: SerializablePullRequest = serde_json::from_slice(&payload[..len as usize])
                    .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                Ok(CollaborationMessage::PullRequestCreated(pr))
            }
            CollaborationMessageType::PullRequestUpdated => {
                let len = read_u32(&mut payload)?;
                let pr: SerializablePullRequest = serde_json::from_slice(&payload[..len as usize])
                    .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                Ok(CollaborationMessage::PullRequestUpdated(pr))
            }
            CollaborationMessageType::IssueCreated => {
                let len = read_u32(&mut payload)?;
                let issue: SerializableIssue = serde_json::from_slice(&payload[..len as usize])
                    .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                Ok(CollaborationMessage::IssueCreated(issue))
            }
            CollaborationMessageType::IssueUpdated => {
                let len = read_u32(&mut payload)?;
                let issue: SerializableIssue = serde_json::from_slice(&payload[..len as usize])
                    .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                Ok(CollaborationMessage::IssueUpdated(issue))
            }
            CollaborationMessageType::CommentCreated => {
                let len = read_u32(&mut payload)?;
                let comment: SerializableComment = serde_json::from_slice(&payload[..len as usize])
                    .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                Ok(CollaborationMessage::CommentCreated(comment))
            }
            CollaborationMessageType::ReviewCreated => {
                let len = read_u32(&mut payload)?;
                let review: SerializableReview =
                    serde_json::from_slice(&payload[..len as usize])
                        .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                Ok(CollaborationMessage::ReviewCreated(review))
            }
            CollaborationMessageType::SyncCollaborationRequest => {
                let repo_len = read_u16(&mut payload)?;
                let repo_key = String::from_utf8(payload[..repo_len as usize].to_vec())
                    .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                Ok(CollaborationMessage::SyncCollaborationRequest { repo_key })
            }
            CollaborationMessageType::SyncCollaborationResponse => {
                // Repo key
                let repo_len = read_u16(&mut payload)?;
                let repo_key = String::from_utf8(payload[..repo_len as usize].to_vec())
                    .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                payload = &payload[repo_len as usize..];

                // PRs
                let pr_len = read_u32(&mut payload)?;
                let pull_requests: Vec<SerializablePullRequest> =
                    serde_json::from_slice(&payload[..pr_len as usize])
                        .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                payload = &payload[pr_len as usize..];

                // Issues
                let issue_len = read_u32(&mut payload)?;
                let issues: Vec<SerializableIssue> =
                    serde_json::from_slice(&payload[..issue_len as usize])
                        .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                payload = &payload[issue_len as usize..];

                // Comments
                let comment_len = read_u32(&mut payload)?;
                let comments: Vec<SerializableComment> =
                    serde_json::from_slice(&payload[..comment_len as usize])
                        .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;
                payload = &payload[comment_len as usize..];

                // Reviews
                let review_len = read_u32(&mut payload)?;
                let reviews: Vec<SerializableReview> =
                    serde_json::from_slice(&payload[..review_len as usize])
                        .map_err(|e| P2PError::InvalidMessage(e.to_string()))?;

                Ok(CollaborationMessage::SyncCollaborationResponse {
                    repo_key,
                    pull_requests,
                    issues,
                    comments,
                    reviews,
                })
            }
        }
    }
}

fn read_u16(buf: &mut &[u8]) -> Result<u16> {
    if buf.remaining() < 2 {
        return Err(P2PError::InvalidMessage("truncated u16".into()));
    }
    Ok(buf.get_u16())
}

fn read_u32(buf: &mut &[u8]) -> Result<u32> {
    if buf.remaining() < 4 {
        return Err(P2PError::InvalidMessage("truncated u32".into()));
    }
    Ok(buf.get_u32())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pr_message_roundtrip() {
        let pr = SerializablePullRequest {
            id: 1,
            repo_key: "alice/repo".to_string(),
            number: 1,
            title: "Add feature".to_string(),
            description: "Description".to_string(),
            author: "alice".to_string(),
            state: "open".to_string(),
            source_branch: "feature".to_string(),
            target_branch: "main".to_string(),
            source_commit: "0".repeat(40),
            target_commit: "1".repeat(40),
            labels: vec![],
            created_at: 12345,
            updated_at: 12345,
            merged_at: None,
            merged_by: None,
        };

        let msg = CollaborationMessage::PullRequestCreated(pr.clone());
        let encoded = msg.encode();
        let decoded = CollaborationMessage::decode(&encoded).unwrap();

        match decoded {
            CollaborationMessage::PullRequestCreated(decoded_pr) => {
                assert_eq!(decoded_pr.id, pr.id);
                assert_eq!(decoded_pr.title, pr.title);
                assert_eq!(decoded_pr.number, pr.number);
            }
            _ => panic!("wrong message type"),
        }
    }

    #[test]
    fn test_issue_message_roundtrip() {
        let issue = SerializableIssue {
            id: 2,
            repo_key: "bob/project".to_string(),
            number: 5,
            title: "Bug report".to_string(),
            description: "Steps to reproduce".to_string(),
            author: "bob".to_string(),
            state: "open".to_string(),
            labels: vec![SerializableLabel {
                name: "bug".to_string(),
                color: "ff0000".to_string(),
                description: Some("A bug".to_string()),
            }],
            created_at: 54321,
            updated_at: 54321,
            closed_at: None,
            closed_by: None,
        };

        let msg = CollaborationMessage::IssueCreated(issue.clone());
        let encoded = msg.encode();
        let decoded = CollaborationMessage::decode(&encoded).unwrap();

        match decoded {
            CollaborationMessage::IssueCreated(decoded_issue) => {
                assert_eq!(decoded_issue.id, issue.id);
                assert_eq!(decoded_issue.title, issue.title);
                assert_eq!(decoded_issue.labels.len(), 1);
            }
            _ => panic!("wrong message type"),
        }
    }

    #[test]
    fn test_sync_request_roundtrip() {
        let msg = CollaborationMessage::SyncCollaborationRequest {
            repo_key: "carol/test".to_string(),
        };

        let encoded = msg.encode();
        let decoded = CollaborationMessage::decode(&encoded).unwrap();

        match decoded {
            CollaborationMessage::SyncCollaborationRequest { repo_key } => {
                assert_eq!(repo_key, "carol/test");
            }
            _ => panic!("wrong message type"),
        }
    }
}