-
Notifications
You must be signed in to change notification settings - Fork 80
/
macros.rs
44 lines (43 loc) · 1.17 KB
/
macros.rs
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
// Copyright 2020-2021 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
/// Creates a new IOTA DID from a `public` key and optional `network`.
///
/// # Errors
///
/// Errors if the [`IotaDID`][crate::did::IotaDID] is invalid.
///
/// # Example
///
/// ```
/// # use identity_iota::try_did;
/// #
/// let did = try_did!(b"public-key").unwrap();
/// assert_eq!(did.as_str(), "did:iota:2xQiiGHDq5gCi1H7utY1ni7cf65fTay3G11S4xKp1vkS");
///
/// let did = try_did!(b"public-key", "com").unwrap();
/// assert_eq!(
/// did.as_str(),
/// "did:iota:com:2xQiiGHDq5gCi1H7utY1ni7cf65fTay3G11S4xKp1vkS"
/// );
/// ```
#[macro_export]
macro_rules! try_did {
// Defining explicit branches rather than `$($tt:tt)+` gives much better docs.
($public:expr, $network:expr) => {
$crate::did::IotaDID::parse(format!(
"{}:{}:{}:{}",
$crate::did::IotaDID::SCHEME,
$crate::did::IotaDID::METHOD,
$network,
$crate::did::IotaDID::encode_key($public),
))
};
($public:expr) => {
$crate::did::IotaDID::parse(format!(
"{}:{}:{}",
$crate::did::IotaDID::SCHEME,
$crate::did::IotaDID::METHOD,
$crate::did::IotaDID::encode_key($public),
))
};
}