Skip to content

Commit

Permalink
Use doc_auto_cfg
Browse files Browse the repository at this point in the history
Manually specifying cfgs is laborious and error-prone. This replaces mot
of it with automatic documenting.
  • Loading branch information
Kixunil committed Jul 3, 2024
1 parent 72e1de1 commit 8d0224b
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 57 deletions.
11 changes: 0 additions & 11 deletions src/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,6 @@ impl<'a> TryFrom<&'a str> for Amount {
/// Accepts an unsigned integer up to 21 000 000 BTC
/// The amount may optionally be followed by denomination ` msat`.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<String> for Amount {
type Error = ParseError;

Expand All @@ -353,7 +352,6 @@ impl TryFrom<String> for Amount {
/// Accepts an unsigned integer up to 21 000 000 BTC
/// The amount may optionally be followed by denomination ` msat`.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<Box<str>> for Amount {
type Error = ParseError;

Expand Down Expand Up @@ -382,7 +380,6 @@ impl fmt::Display for ParseError {
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for ParseError {
#[inline]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Expand Down Expand Up @@ -421,7 +418,6 @@ impl fmt::Display for ParseErrorInner {
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for ParseErrorInner {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand All @@ -447,7 +443,6 @@ impl fmt::Display for OverflowError {
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for OverflowError {}

/// Error returned when a conversion to satoshis fails due to the value not being round.
Expand All @@ -465,7 +460,6 @@ impl fmt::Display for FractionError {
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for FractionError {}

#[cfg(feature = "bitcoin-units")]
Expand Down Expand Up @@ -525,15 +519,13 @@ mod serde_impl {
}

/// The value is serialized as `u64` msats.
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl Serialize for Amount {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
serializer.serialize_u64(self.0)
}
}

/// The value is deserialized as `u64` msats.
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> Deserialize<'de> for Amount {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
deserializer.deserialize_u64(HRVisitor)
Expand All @@ -551,7 +543,6 @@ mod postgres_impl {
use core::convert::TryInto;

/// Stored as `i64` msats
#[cfg_attr(docsrs, doc(cfg(feature = "postgres-types")))]
impl ToSql for Amount {
fn to_sql(&self, ty: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Send + Sync + 'static>> {
// Amount guarantees to always be in bounds
Expand All @@ -566,7 +557,6 @@ mod postgres_impl {
}

/// Retrieved as `i64` msats with range check
#[cfg_attr(docsrs, doc(cfg(feature = "postgres-types")))]
impl<'a> FromSql<'a> for Amount {
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Send + Sync + 'static>> {
let msats = <i64>::from_sql(ty, raw)?
Expand All @@ -588,7 +578,6 @@ mod slog_impl {
use slog::{Key, Value, Record, Serializer};

/// Logs msats using `emit_u64`
#[cfg_attr(docsrs, doc(cfg(feature = "slog")))]
impl Value for Amount {
fn serialize(&self, _rec: &Record, key: Key, serializer: &mut dyn Serializer) -> slog::Result {
serializer.emit_u64(key, self.0)
Expand Down
12 changes: 1 addition & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,40 +96,33 @@
//! MIT

#![cfg_attr(docsrs, feature(doc_cfg))]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![deny(missing_docs)]

#![no_std]

#[cfg(any(feature = "std", test))]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
pub extern crate std;

#[cfg(any(feature = "alloc", test))]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub extern crate alloc;

#[cfg(feature = "bitcoin-units")]
#[cfg_attr(docsrs, doc(cfg(feature = "bitcoin")))]
pub extern crate bitcoin_units;

#[cfg(feature = "parse_arg")]
#[cfg_attr(docsrs, doc(cfg(feature = "parse_arg")))]
pub extern crate parse_arg;

#[cfg(feature = "postgres-types")]
#[cfg_attr(docsrs, doc(cfg(feature = "postgres-types")))]
pub extern crate postgres_types_real as postgres_types;

#[cfg(feature = "secp256k1")]
#[cfg_attr(docsrs, doc(cfg(feature = "secp256k1")))]
pub extern crate secp256k1;

#[cfg(feature = "serde")]
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
pub extern crate serde;

#[cfg(feature = "slog")]
#[cfg_attr(docsrs, doc(cfg(feature = "slog")))]
pub extern crate slog;

#[macro_use]
Expand All @@ -139,17 +132,14 @@ pub(crate) mod err_fmt;

pub mod node_id;
#[cfg(any(feature = "std", rust_v_1_77))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", version("1.77.0")))))]
pub mod p2p_address;
pub mod amount;
#[cfg(feature = "secp256k1")]
pub mod node_pubkey;

pub use node_id::NodeId;
#[cfg(any(feature = "std", rust_v_1_77))]
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", version("1.77.0")))))]
pub use p2p_address::P2PAddress;
pub use amount::Amount;
#[cfg(feature = "secp256k1")]
#[cfg_attr(docsrs, doc(cfg(feature = "secp256k1")))]
pub use node_pubkey::NodePubkey;
15 changes: 0 additions & 15 deletions src/node_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ impl NodeId {
/// This is meant for convenience around APIs that require `Vec<u8>`. Since it allocates it's
/// best to avoid it if possible.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn to_vec(self) -> Vec<u8> {
self.0.to_vec()
}
Expand Down Expand Up @@ -174,7 +173,6 @@ impl<'a> TryFrom<&'a str> for NodeId {

/// Expects hex representation
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<String> for NodeId {
type Error = ParseError;

Expand All @@ -186,7 +184,6 @@ impl TryFrom<String> for NodeId {

/// Expects hex representation
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<Box<str>> for NodeId {
type Error = ParseError;

Expand All @@ -209,7 +206,6 @@ impl<'a> TryFrom<&'a [u8]> for NodeId {
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<Vec<u8>> for NodeId {
type Error = DecodeError;

Expand All @@ -220,7 +216,6 @@ impl TryFrom<Vec<u8>> for NodeId {
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<Box<[u8]>> for NodeId {
type Error = DecodeError;

Expand Down Expand Up @@ -293,7 +288,6 @@ impl fmt::Display for DecodeError {
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for DecodeError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self.error {
Expand Down Expand Up @@ -322,7 +316,6 @@ impl fmt::Display for ParseError {
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for ParseError {
#[inline]
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Expand Down Expand Up @@ -352,7 +345,6 @@ impl fmt::Display for ParseErrorInner {
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for ParseErrorInner {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Expand Down Expand Up @@ -388,7 +380,6 @@ impl fmt::Display for InvalidNodeId {
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for InvalidNodeId {}

/// Implementation of `parse_arg::ParseArg` trait
Expand All @@ -397,7 +388,6 @@ mod parse_arg_impl {
use core::fmt;
use super::NodeId;

#[cfg_attr(docsrs, doc(cfg(feature = "parse_arg")))]
impl parse_arg::ParseArgFromStr for NodeId {
fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
writer.write_str("a hex-encoded LN node ID (66 hex digits/33 bytes)")
Expand Down Expand Up @@ -459,7 +449,6 @@ mod serde_impl {
}

/// `NodeId` is serialized as hex to human-readable formats and as bytes to non-human-readable.
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl Serialize for NodeId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> where S: Serializer {
if serializer.is_human_readable() {
Expand All @@ -471,7 +460,6 @@ mod serde_impl {
}

/// `NodeId` is deserialized as hex from human-readable formats and as bytes from non-human-readable.
#[cfg_attr(docsrs, doc(cfg(feature = "serde")))]
impl<'de> Deserialize<'de> for NodeId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> where D: Deserializer<'de> {
if deserializer.is_human_readable() {
Expand All @@ -496,7 +484,6 @@ mod postgres_impl {
/// Supports `BYTEA`, `TEXT`, and `VARCHAR`.
///
/// Stored as bytes if `BYTEA` is used, as hex string otherwise.
#[cfg_attr(docsrs, doc(cfg(feature = "postgres-types")))]
impl ToSql for NodeId {
fn to_sql(&self, ty: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Send + Sync + 'static>> {
use core::fmt::Write;
Expand All @@ -522,7 +509,6 @@ mod postgres_impl {
/// Supports `BYTEA`, `TEXT`, and `VARCHAR`.
///
/// Decoded as bytes if `BYTEA` is used, as hex string otherwise.
#[cfg_attr(docsrs, doc(cfg(feature = "postgres-types")))]
impl<'a> FromSql<'a> for NodeId {
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Send + Sync + 'static>> {
match *ty {
Expand All @@ -549,7 +535,6 @@ mod slog_impl {
use slog::{Key, Value, Record, Serializer};

/// Currently uses `Display` but may use `emit_bytes` if/when it's implemented.
#[cfg_attr(docsrs, doc(cfg(feature = "slog")))]
impl Value for NodeId {
fn serialize(&self, _rec: &Record, key: Key, serializer: &mut dyn Serializer) -> slog::Result {
serializer.emit_arguments(key, &format_args!("{}", self))
Expand Down
14 changes: 0 additions & 14 deletions src/node_pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
//! Further, it provides convenient parsing, serialization and signature verification methods
//! along with strong error types.

#![cfg_attr(docsrs, doc(cfg(feature = "secp256k1")))]

use core::convert::{TryFrom, TryInto};
use core::str::FromStr;
use core::fmt;
Expand Down Expand Up @@ -64,7 +62,6 @@ impl NodePubkey {
/// marvin.verify(&secp, message, signature).unwrap();
/// ```
#[cfg(feature = "node_pubkey_verify")]
#[cfg_attr(docsrs, doc(cfg(feature = "node_pubkey_verify")))]
pub fn verify<M: Into<secp256k1::Message>, C: secp256k1::Verification>(&self, secp: &Secp256k1<C>, message: M, signature: &[u8]) -> Result<(), secp256k1::Error> {
use secp256k1::ecdsa::Signature;

Expand Down Expand Up @@ -95,7 +92,6 @@ impl NodePubkey {
/// marvin.verify_lightning_message(&secp, message.as_bytes(), signature).unwrap();
/// ```
#[cfg(feature = "node_pubkey_recovery")]
#[cfg_attr(docsrs, doc(cfg(feature = "node_pubkey_recovery")))]
pub fn verify_lightning_message<C: secp256k1::Verification>(&self, secp: &Secp256k1<C>, message: &[u8], signature: &[u8]) -> Result<(), secp256k1::Error> {
use secp256k1::Message;
use secp256k1::ecdsa::{RecoverableSignature, RecoveryId};
Expand Down Expand Up @@ -267,7 +263,6 @@ impl<'a> TryFrom<&'a str> for NodePubkey {

/// Expects hex representation
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<String> for NodePubkey {
type Error = ParseError;

Expand All @@ -279,7 +274,6 @@ impl TryFrom<String> for NodePubkey {

/// Expects hex representation
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<Box<str>> for NodePubkey {
type Error = ParseError;

Expand All @@ -299,7 +293,6 @@ impl<'a> TryFrom<&'a [u8]> for NodePubkey {
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<Vec<u8>> for NodePubkey {
type Error = secp256k1::Error;

Expand All @@ -310,7 +303,6 @@ impl TryFrom<Vec<u8>> for NodePubkey {
}

#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl TryFrom<Box<[u8]>> for NodePubkey {
type Error = secp256k1::Error;

Expand Down Expand Up @@ -351,7 +343,6 @@ impl fmt::Display for ParseError {
///
/// Specifically pubkey error is displayed inline instead of as a source.
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for ParseError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
Some(&self.reason)
Expand All @@ -378,7 +369,6 @@ impl fmt::Display for ParseErrorInner {
}

#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for ParseErrorInner {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self {
Expand All @@ -397,7 +387,6 @@ mod parse_arg_impl {
use core::fmt;
use super::NodePubkey;

#[cfg_attr(docsrs, doc(cfg(feature = "parse_arg")))]
impl parse_arg::ParseArgFromStr for NodePubkey {
fn describe_type<W: fmt::Write>(mut writer: W) -> fmt::Result {
writer.write_str("a hex-encoded LN node ID (66 hex digits/33 bytes)")
Expand Down Expand Up @@ -442,7 +431,6 @@ mod postgres_impl {
/// Supports `BYTEA`, `TEXT`, and `VARCHAR`.
///
/// Stored as bytes if `BYTEA` is used, as hex string otherwise.
#[cfg_attr(docsrs, doc(cfg(feature = "postgres-types")))]
impl ToSql for NodePubkey {
fn to_sql(&self, ty: &Type, out: &mut BytesMut) -> Result<IsNull, Box<dyn Error + Send + Sync + 'static>> {
self.to_node_id().to_sql(ty, out)
Expand All @@ -458,7 +446,6 @@ mod postgres_impl {
/// Supports `BYTEA`, `TEXT`, and `VARCHAR`.
///
/// Decoded as bytes if `BYTEA` is used, as hex string otherwise.
#[cfg_attr(docsrs, doc(cfg(feature = "postgres-types")))]
impl<'a> FromSql<'a> for NodePubkey {
fn from_sql(ty: &Type, raw: &'a [u8]) -> Result<Self, Box<dyn Error + Send + Sync + 'static>> {
NodeId::from_sql(ty, raw)?.try_into().map_err(|error| Box::new(error) as _)
Expand All @@ -477,7 +464,6 @@ mod slog_impl {
use slog::{Key, Value, Record, Serializer};

/// Currently uses `Display` but may use `emit_bytes` if/when it's implemented.
#[cfg_attr(docsrs, doc(cfg(feature = "slog")))]
impl Value for NodePubkey {
fn serialize(&self, _rec: &Record, key: Key, serializer: &mut dyn Serializer) -> slog::Result {
serializer.emit_arguments(key, &format_args!("{}", self))
Expand Down
Loading

0 comments on commit 8d0224b

Please sign in to comment.