Skip to content

Commit 8cbbda6

Browse files
committed
f Various type fixes
1 parent 01adde6 commit 8cbbda6

File tree

3 files changed

+11
-17
lines changed

3 files changed

+11
-17
lines changed

bindings/ldk_node.udl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ enum NodeError {
7373
"ConnectionFailed",
7474
"InvoiceCreationFailed",
7575
"PaymentFailed",
76-
"PeerInfoParseFailed",
7776
"PeerInfoNotFound",
7877
"ChannelCreationFailed",
7978
"ChannelClosingFailed",
@@ -84,6 +83,7 @@ enum NodeError {
8483
"TxSyncFailed",
8584
"GossipUpdateFailed",
8685
"InvalidAddress",
86+
"InvalidNetAddress",
8787
"InvalidPublicKey",
8888
"InvalidPaymentHash",
8989
"InvalidPaymentPreimage",

src/error.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ pub enum Error {
1515
InvoiceCreationFailed,
1616
/// An attempted payment has failed.
1717
PaymentFailed,
18-
/// A given peer info could not be parsed.
19-
PeerInfoParseFailed,
2018
/// A given peer info could not be found.
2119
PeerInfoNotFound,
2220
/// A channel could not be opened.
@@ -37,6 +35,8 @@ pub enum Error {
3735
GossipUpdateFailed,
3836
/// The given address is invalid.
3937
InvalidAddress,
38+
/// The given network address is invalid.
39+
InvalidNetAddress,
4040
/// The given public key is invalid.
4141
InvalidPublicKey,
4242
/// The given payment hash is invalid.
@@ -70,7 +70,6 @@ impl fmt::Display for Error {
7070
Self::ConnectionFailed => write!(f, "Network connection closed."),
7171
Self::InvoiceCreationFailed => write!(f, "Failed to create invoice."),
7272
Self::PaymentFailed => write!(f, "Failed to send the given payment."),
73-
Self::PeerInfoParseFailed => write!(f, "Failed to parse the given peer information."),
7473
Self::PeerInfoNotFound => write!(f, "Failed to resolve the given peer information."),
7574
Self::ChannelCreationFailed => write!(f, "Failed to create channel."),
7675
Self::ChannelClosingFailed => write!(f, "Failed to close channel."),
@@ -81,6 +80,7 @@ impl fmt::Display for Error {
8180
Self::TxSyncFailed => write!(f, "Failed to sync transactions."),
8281
Self::GossipUpdateFailed => write!(f, "Failed to update gossip data."),
8382
Self::InvalidAddress => write!(f, "The given address is invalid."),
83+
Self::InvalidNetAddress => write!(f, "The given network address is invalid."),
8484
Self::InvalidPublicKey => write!(f, "The given public key is invalid."),
8585
Self::InvalidPaymentHash => write!(f, "The given payment hash is invalid."),
8686
Self::InvalidPaymentPreimage => write!(f, "The given payment preimage is invalid."),

src/types.rs

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ impl UniffiCustomTypeConverter for Network {
285285
impl UniffiCustomTypeConverter for Txid {
286286
type Builtin = String;
287287
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
288-
Ok(Txid::from_str(&val).unwrap())
288+
Ok(Txid::from_str(&val)?)
289289
}
290290

291291
fn from_custom(obj: Self) -> Self::Builtin {
@@ -407,11 +407,7 @@ impl UniffiCustomTypeConverter for SocketAddr {
407407
type Builtin = String;
408408

409409
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
410-
if let Ok(addr) = SocketAddr::from_str(&val) {
411-
return Ok(addr);
412-
}
413-
414-
Err(Error::InvalidPublicKey.into())
410+
Ok(SocketAddr::from_str(&val).map_err(|_| Error::InvalidNetAddress)?)
415411
}
416412

417413
fn from_custom(obj: Self) -> Self::Builtin {
@@ -434,7 +430,7 @@ impl Display for NetAddress {
434430
}
435431
LdkNetAddress::IPv6 { addr, port } => {
436432
let ip_addr = Ipv6Addr::from(addr);
437-
write!(f, "{}:{}", ip_addr, port)
433+
write!(f, "[{}]:{}", ip_addr, port)
438434
}
439435
LdkNetAddress::Hostname { ref hostname, port } => {
440436
write!(f, "{}:{}", hostname.as_str(), port)
@@ -454,7 +450,7 @@ impl Display for NetAddress {
454450
impl UniffiCustomTypeConverter for NetAddress {
455451
type Builtin = String;
456452
fn into_custom(val: Self::Builtin) -> uniffi::Result<Self> {
457-
Ok(NetAddress::from_str(&val).unwrap())
453+
Ok(NetAddress::from_str(&val).map_err(|_| Error::InvalidNetAddress)?)
458454
}
459455

460456
fn from_custom(obj: Self) -> Self::Builtin {
@@ -491,11 +487,9 @@ impl FromStr for NetAddress {
491487
Err(_) => return Err(()),
492488
};
493489

494-
if let Ok(hostname) = Hostname::try_from(host.to_string()) {
495-
return Ok(Self(LdkNetAddress::Hostname { hostname, port }));
496-
}
497-
498-
Err(())
490+
Hostname::try_from(host.to_string())
491+
.map(|hostname| Self(LdkNetAddress::Hostname { hostname, port }))
492+
.map_err(|_| ())
499493
}
500494
}
501495
}

0 commit comments

Comments
 (0)