1
1
use crate :: SemanticVersion ;
2
2
3
3
/// Network type, either mainnet or testnet.
4
- #[ derive( Default , Debug , Clone , Copy , PartialEq ) ]
4
+ #[ derive( Debug , Clone , Copy , PartialEq ) ]
5
5
pub enum DriaNetwork {
6
6
Mainnet ,
7
- #[ default]
8
7
Testnet ,
9
8
}
10
9
11
- impl From < & str > for DriaNetwork {
12
- fn from ( s : & str ) -> Self {
10
+ impl TryFrom < & str > for DriaNetwork {
11
+ type Error = ( ) ;
12
+
13
+ /// Converts a string to a `DriaNetwork`, using the same name as in:
14
+ ///
15
+ /// - "mainnet" for `DriaNetwork::Mainnet`
16
+ /// - "testnet" for `DriaNetwork::Testnet`
17
+ fn try_from ( s : & str ) -> Result < Self , Self :: Error > {
13
18
match s {
14
- "mainnet" => DriaNetwork :: Mainnet ,
15
- "testnet" => DriaNetwork :: Testnet ,
16
- _ => Default :: default ( ) ,
19
+ "mainnet" => Ok ( DriaNetwork :: Mainnet ) ,
20
+ "testnet" => Ok ( DriaNetwork :: Testnet ) ,
21
+ _ => Err ( ( ) ) ,
17
22
}
18
23
}
19
24
}
@@ -28,13 +33,17 @@ impl std::fmt::Display for DriaNetwork {
28
33
}
29
34
30
35
impl DriaNetwork {
36
+ /// Returns the protocol name for the given network, which can be used by
37
+ /// libp2p `identify` protocol.
31
38
pub fn protocol_name ( & self ) -> & str {
32
39
match self {
33
40
DriaNetwork :: Mainnet => "dria" ,
34
41
DriaNetwork :: Testnet => "dria-test" ,
35
42
}
36
43
}
37
44
45
+ /// Returns the discovery URL for the given version, where the
46
+ /// major.minor version is appended to the URL as a path variable.
38
47
pub fn discovery_url ( & self , version : & SemanticVersion ) -> String {
39
48
let base_url = match self {
40
49
DriaNetwork :: Mainnet => "https://mainnet.dkn.dria.co/discovery/v0/available-nodes" ,
0 commit comments