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
//! A JSONRPC `method` enum.
#![warn(missing_docs)]
use super::error::Error;
use super::error::Result;

/// supported methods.
#[derive(Debug, Clone)]
pub enum Method {
    /// Connect peer with remote jsonrpc server url
    ConnectPeerViaHttp,
    /// Connect peer with remote peer's did
    ConnectWithDid,
    /// Connect peers from a seed file
    ConnectWithSeed,
    /// List all connected peers
    ListPeers,
    /// Create offer for manually handshake
    CreateOffer,
    /// Answer offer for manually handshake
    AnswerOffer,
    /// Accept Answer for manually handshake
    AcceptAnswer,
    /// Send custom message to peer
    SendTo,
    /// Disconnect a peer
    Disconnect,
    /// Send simple text message
    SendSimpleText,
    /// SendHttpRequestMessage,
    SendHttpRequestMessage,
    /// SendCustomMessage,
    SendCustomMessage,
    /// Append data to topic
    PublishMessageToTopic,
    /// Fetch data of topic
    FetchMessagesOfTopic,
    /// Register service
    RegisterService,
    /// Lookup service
    LookupService,
    /// Poll message
    PollMessage,
    /// Retrieve Node info
    NodeInfo,
    /// Retrieve Node DID
    NodeDid,
}

impl Method {
    /// Return method's name as `&str`
    pub fn as_str(&self) -> &str {
        match self {
            Method::ConnectPeerViaHttp => "connectPeerViaHttp",
            Method::ConnectWithDid => "connectWithDid",
            Method::ConnectWithSeed => "connectWithSeed",
            Method::ListPeers => "listPeers",
            Method::CreateOffer => "createOffer",
            Method::AnswerOffer => "answerOffer",
            Method::SendTo => "sendTo",
            Method::Disconnect => "disconnect",
            Method::AcceptAnswer => "acceptAnswer",
            Method::SendSimpleText => "sendSimpleText",
            Method::SendHttpRequestMessage => "sendHttpRequestMessage",
            Method::SendCustomMessage => "sendCustomMessage",
            Method::PublishMessageToTopic => "publishMessageToTopic",
            Method::FetchMessagesOfTopic => "fetchMessagesOfTopic",
            Method::RegisterService => "registerService",
            Method::LookupService => "lookupService",
            Method::PollMessage => "pollMessage",
            Method::NodeInfo => "nodeInfo",
            Method::NodeDid => "nodeDid",
        }
    }
}

impl ToString for Method {
    fn to_string(&self) -> String {
        self.as_str().to_owned()
    }
}

impl TryFrom<&str> for Method {
    type Error = crate::error::Error;

    fn try_from(value: &str) -> Result<Self> {
        Ok(match value {
            "connectPeerViaHttp" => Self::ConnectPeerViaHttp,
            "connectWithDid" => Self::ConnectWithDid,
            "connectWithSeed" => Self::ConnectWithSeed,
            "listPeers" => Self::ListPeers,
            "createOffer" => Self::CreateOffer,
            "answerOffer" => Self::AnswerOffer,
            "sendTo" => Self::SendTo,
            "disconnect" => Self::Disconnect,
            "acceptAnswer" => Self::AcceptAnswer,
            "sendSimpleText" => Self::SendSimpleText,
            "sendHttpRequestMessage" => Self::SendHttpRequestMessage,
            "sendCustomMessage" => Self::SendCustomMessage,
            "publishMessageToTopic" => Method::PublishMessageToTopic,
            "fetchMessagesOfTopic" => Method::FetchMessagesOfTopic,
            "registerService" => Method::RegisterService,
            "lookupService" => Method::LookupService,
            "pollMessage" => Method::PollMessage,
            "nodeInfo" => Method::NodeInfo,
            "nodeDid" => Method::NodeDid,
            _ => return Err(Error::InvalidMethod),
        })
    }
}