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
use crate::types::{Id, PublicKey};

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub enum PageInfo {
    Primary(Primary),
    Secondary(Secondary),
    Data(()),
}

impl PageInfo {
    pub fn primary(pub_key: PublicKey) -> Self {
        PageInfo::Primary(Primary { pub_key })
    }

    pub fn secondary(peer_id: Id) -> Self {
        PageInfo::Secondary(Secondary { peer_id })
    }

    pub fn is_primary(&self) -> bool {
        match self {
            PageInfo::Primary(_) => true,
            _ => false,
        }
    }

    pub fn is_secondary(&self) -> bool {
        match self {
            PageInfo::Secondary(_) => true,
            _ => false,
        }
    }

    pub fn pub_key(&self) -> Option<PublicKey> {
        match self {
            PageInfo::Primary(p) => Some(p.pub_key.clone()),
            _ => None,
        }
    }

    pub fn peer_id(&self) -> Option<Id> {
        match self {
            PageInfo::Secondary(s) => Some(s.peer_id.clone()),
            _ => None,
        }
    }
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Primary {
    pub pub_key: PublicKey,
}

#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Secondary {
    pub peer_id: Id,
}