From: Matt Corallo Date: Mon, 4 Apr 2022 01:53:24 +0000 (+0000) Subject: Add additional error type conversion logic X-Git-Tag: v0.0.106.0^2~4 X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-c-bindings;a=commitdiff_plain;h=529b93b9b39972119e2569c5dc4913ffdf4a8d8f Add additional error type conversion logic --- diff --git a/c-bindings-gen/src/types.rs b/c-bindings-gen/src/types.rs index ced1f34..da2d180 100644 --- a/c-bindings-gen/src/types.rs +++ b/c-bindings-gen/src/types.rs @@ -883,6 +883,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "core::convert::Infallible" => Some("crate::c_types::NotConstructable"), + "bitcoin::bech32::Error"|"bech32::Error" + if !is_ref => Some("crate::c_types::Bech32Error"), + "bitcoin::secp256k1::Error"|"secp256k1::Error" + if !is_ref => Some("crate::c_types::Secp256k1Error"), + + "core::num::ParseIntError" => Some("crate::c_types::Error"), + "core::str::Utf8Error" => Some("crate::c_types::Error"), + "bitcoin::bech32::u5"|"bech32::u5" => Some("crate::c_types::u5"), "core::num::NonZeroU8" => Some("u8"), @@ -894,8 +902,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if is_ref => Some("*const [u8; 32]"), "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey" if !is_ref => Some("crate::c_types::SecretKey"), - "bitcoin::secp256k1::Error"|"secp256k1::Error" - if !is_ref => Some("crate::c_types::Secp256k1Error"), "bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice"), "bitcoin::blockdata::script::Script" if !is_ref => Some("crate::c_types::derived::CVec_u8Z"), "bitcoin::blockdata::transaction::OutPoint" => Some("crate::lightning::chain::transaction::OutPoint"), @@ -961,6 +967,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "core::convert::Infallible" => Some("panic!(\"You must never construct a NotConstructable! : "), + "bitcoin::bech32::Error"|"bech32::Error" if !is_ref => Some(""), + "bitcoin::secp256k1::Error"|"secp256k1::Error" if !is_ref => Some(""), + + "core::num::ParseIntError" => Some("u8::from_str_radix(\" a\", 10).unwrap_err() /*"), + "core::str::Utf8Error" => Some("core::str::from_utf8(&[0xff]).unwrap_err() /*"), + "std::time::Duration"|"core::time::Duration" => Some("core::time::Duration::from_secs("), "std::time::SystemTime" => Some("(::std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs("), @@ -1043,6 +1055,12 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "core::convert::Infallible" => Some("\")"), + "bitcoin::bech32::Error"|"bech32::Error" if !is_ref => Some(".into_rust()"), + "bitcoin::secp256k1::Error"|"secp256k1::Error" if !is_ref => Some(".into_rust()"), + + "core::num::ParseIntError" => Some("*/"), + "core::str::Utf8Error" => Some("*/"), + "std::time::Duration"|"core::time::Duration" => Some(")"), "std::time::SystemTime" => Some("))"), @@ -1133,6 +1151,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "core::convert::Infallible" => Some("panic!(\"Cannot construct an Infallible: "), + "bitcoin::bech32::Error"|"bech32::Error" + if !is_ref => Some("crate::c_types::Bech32Error::from_rust("), + "bitcoin::secp256k1::Error"|"secp256k1::Error" + if !is_ref => Some("crate::c_types::Secp256k1Error::from_rust("), + + "core::num::ParseIntError" => Some("crate::c_types::Error { _dummy: 0 } /*"), + "core::str::Utf8Error" => Some("crate::c_types::Error { _dummy: 0 } /*"), + "bitcoin::bech32::u5"|"bech32::u5" => Some(""), "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey" @@ -1143,8 +1169,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if is_ref => Some(""), "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey" if !is_ref => Some("crate::c_types::SecretKey::from_rust("), - "bitcoin::secp256k1::Error"|"secp256k1::Error" - if !is_ref => Some("crate::c_types::Secp256k1Error::from_rust("), "bitcoin::blockdata::script::Script" if is_ref => Some("crate::c_types::u8slice::from_slice(&"), "bitcoin::blockdata::script::Script" if !is_ref => Some(""), "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" if is_ref => Some("crate::c_types::Transaction::from_bitcoin("), @@ -1206,6 +1230,14 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { "core::convert::Infallible" => Some("\")"), + "bitcoin::secp256k1::Error"|"bech32::Error" + if !is_ref => Some(")"), + "bitcoin::secp256k1::Error"|"secp256k1::Error" + if !is_ref => Some(")"), + + "core::num::ParseIntError" => Some("*/"), + "core::str::Utf8Error" => Some("*/"), + "bitcoin::bech32::u5"|"bech32::u5" => Some(".into()"), "bitcoin::secp256k1::key::PublicKey"|"bitcoin::secp256k1::PublicKey"|"secp256k1::key::PublicKey" @@ -1216,8 +1248,6 @@ impl<'a, 'c: 'a> TypeResolver<'a, 'c> { if !is_ref => Some(")"), "bitcoin::secp256k1::key::SecretKey"|"bitcoin::secp256k1::SecretKey" if is_ref => Some(".as_ref()"), - "bitcoin::secp256k1::Error"|"secp256k1::Error" - if !is_ref => Some(")"), "bitcoin::blockdata::script::Script" if is_ref => Some("[..])"), "bitcoin::blockdata::script::Script" if !is_ref => Some(".into_bytes().into()"), "bitcoin::blockdata::transaction::Transaction"|"bitcoin::Transaction" => Some(")"), diff --git a/lightning-c-bindings/src/c_types/mod.rs b/lightning-c-bindings/src/c_types/mod.rs index f31d55b..80f04c5 100644 --- a/lightning-c-bindings/src/c_types/mod.rs +++ b/lightning-c-bindings/src/c_types/mod.rs @@ -169,6 +169,71 @@ impl Secp256k1Error { SecpError::NotEnoughMemory => Secp256k1Error::NotEnoughMemory, } } + pub(crate) fn into_rust(self) -> SecpError { + match self { + Secp256k1Error::IncorrectSignature => SecpError::IncorrectSignature, + Secp256k1Error::InvalidMessage => SecpError::InvalidMessage, + Secp256k1Error::InvalidPublicKey => SecpError::InvalidPublicKey, + Secp256k1Error::InvalidSignature => SecpError::InvalidSignature, + Secp256k1Error::InvalidSecretKey => SecpError::InvalidSecretKey, + Secp256k1Error::InvalidRecoveryId => SecpError::InvalidRecoveryId, + Secp256k1Error::InvalidTweak => SecpError::InvalidTweak, + Secp256k1Error::TweakCheckFailed => SecpError::TweakCheckFailed, + Secp256k1Error::NotEnoughMemory => SecpError::NotEnoughMemory, + } + } +} + +#[repr(C)] +#[derive(Copy, Clone)] +/// Represents an error returned from the bech32 library during validation of some bech32 data +pub enum Bech32Error { + /// String does not contain the separator character + MissingSeparator, + /// The checksum does not match the rest of the data + InvalidChecksum, + /// The data or human-readable part is too long or too short + InvalidLength, + /// Some part of the string contains an invalid character + InvalidChar(u32), + /// Some part of the data has an invalid value + InvalidData(u8), + /// The bit conversion failed due to a padding issue + InvalidPadding, + /// The whole string must be of one case + MixedCase, +} +impl Bech32Error { + pub(crate) fn from_rust(err: bech32::Error) -> Self { + match err { + bech32::Error::MissingSeparator => Self::MissingSeparator, + bech32::Error::InvalidChecksum => Self::InvalidChecksum, + bech32::Error::InvalidLength => Self::InvalidLength, + bech32::Error::InvalidChar(c) => Self::InvalidChar(c as u32), + bech32::Error::InvalidData(d) => Self::InvalidData(d), + bech32::Error::InvalidPadding => Self::InvalidPadding, + bech32::Error::MixedCase => Self::MixedCase, + } + } + pub(crate) fn into_rust(self) -> bech32::Error { + match self { + Self::MissingSeparator => bech32::Error::MissingSeparator, + Self::InvalidChecksum => bech32::Error::InvalidChecksum, + Self::InvalidLength => bech32::Error::InvalidLength, + Self::InvalidChar(c) => bech32::Error::InvalidChar(core::char::from_u32(c).expect("Invalid UTF-8 character in Bech32Error::InvalidChar")), + Self::InvalidData(d) => bech32::Error::InvalidData(d), + Self::InvalidPadding => bech32::Error::InvalidPadding, + Self::MixedCase => bech32::Error::MixedCase, + } + } +} + +#[repr(C)] +#[derive(Clone, Copy, PartialEq)] +/// Sub-errors which don't have specific information in them use this type. +pub struct Error { + /// Zero-Sized_types aren't consistent across Rust/C/C++, so we add some size here + pub _dummy: u8, } #[repr(C)]