Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the docs #160

Merged
merged 6 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ There are a bunch of breaking changes in this release, including:
- Hide error internal [#44](https://github.com/rust-bitcoin/hex-conservative/pull/44)
- Return specific error from `HexToByesIter::new` [#62](https://github.com/rust-bitcoin/hex-conservative/pull/62)

# 0.1.2 - 2024-05-14

- Fix bug in output of `InvalidError` [#88](https://github.com/rust-bitcoin/hex-conservative/pull/88).

# 0.1.1 - 2023-07-19

- [Add `test_hex_unwrap`](https://github.com/rust-bitcoin/hex-conservative/pull/24) hex parsing macro for test usage.
Expand Down
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ version = "0.3.0"
authors = ["Martin Habovštiak <martin.habovstiak@gmail.com>", "Andrew Poelstra <apoelstra@wpsoftware.net>"]
license = "CC0-1.0"
repository = "https://github.com/rust-bitcoin/hex-conservative"
documentation = "https://docs.rs/hex-conservative/"
description = "A hex encoding and decoding crate with a conservative MSRV and dependency policy."
categories = ["encoding"]
keywords = ["encoding", "hex", "hexadecimal"]
Expand Down Expand Up @@ -43,3 +42,7 @@ name = "wrap_array"
[[example]]
name = "serde"
required-features = ["std", "serde"]


[lints.clippy]
missing_errors_doc = "warn"
4 changes: 3 additions & 1 deletion examples/hexy.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// SPDX-License-Identifier: CC0-1.0

//! Demonstrate hexadecimal encoding and decoding for a type with a natural hex representation.
//!
//! For a type where hex is supported but is not the natural representation see `./custom.rs`.
//! To wrap an array see the `./wrap_array_*` examples.
//! To wrap an array see the `./wrap_array.rs` example.

use std::fmt;
use std::str::FromStr;
Expand Down
2 changes: 2 additions & 0 deletions examples/serde.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// SPDX-License-Identifier: CC0-1.0

//! Demonstrate how to use the serde module with struct fields.

#![allow(clippy::disallowed_names)] // Foo is a valid name.
Expand Down
14 changes: 9 additions & 5 deletions examples/wrap_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ fn main() {
println!("Debug: {:?}", wrap);
println!("Debug pretty: {:#?}", wrap);

// We cannot call `to_lower_hex_string` on the wrapped type to allocate a string, if you wish to
// use that trait method see `./wrap_array_display_hex_trait.rs`.
let array_hex = array.as_hex().to_string();
let wrap_hex = wrap.to_string();
assert_eq!(array_hex, wrap_hex);
#[cfg(feature = "alloc")]
{
let array_hex = array.to_lower_hex_string();
let other = array.as_hex().to_string();
assert_eq!(array_hex, other);

let wrap_hex = wrap.to_string();
assert_eq!(array_hex, wrap_hex);
}
}

pub struct Wrap([u8; 32]);
Expand Down
8 changes: 8 additions & 0 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,8 @@ impl<T> std::io::Write for HexWriter<T>
where
T: core::fmt::Write,
{
/// Writes `buf` into [`HexWriter`].
///
/// # Errors
///
/// If no bytes could be written to this `HexWriter`, and the provided buffer is not empty,
Expand All @@ -641,6 +643,12 @@ where
Ok(n)
}
}

/// `flush` is a no-op for [`HexWriter`].
///
/// # Errors
///
/// [`HexWriter`] never errors when flushing.
fn flush(&mut self) -> Result<(), std::io::Error> { Ok(()) }
}

Expand Down
4 changes: 4 additions & 0 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ pub trait FromHex: Sized + sealed::Sealed {
type Error: Sized + fmt::Debug + fmt::Display;

/// Produces an object from a hex string.
///
/// # Errors
///
/// Errors if parsing of hex string fails for any reason.
fn from_hex(s: &str) -> Result<Self, Self::Error>;
}

Expand Down
16 changes: 16 additions & 0 deletions src/serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ use crate::prelude::*;
///
/// We only serialize as hex if the serializer is human readable, if not we call through to the
/// `Serialize` implementation for `data`.
///
/// # Errors
///
/// Returns the serializer error if one occurs.
pub fn serialize<S, T>(data: T, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand All @@ -41,6 +45,10 @@ where
///
/// We only serialize as hex if the serializer is human readable, if not we call through to the
/// `Serialize` implementation for `data`.
///
/// # Errors
///
/// Returns the serializer error if one occurs.
pub fn serialize_lower<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand All @@ -58,6 +66,10 @@ where
///
/// We only serialize as hex if the serializer is human readable, if not we call through to the
/// `Serialize` implementation for `data`.
///
/// # Errors
///
/// Returns the serializer error if one occurs.
pub fn serialize_upper<S, T>(data: T, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down Expand Up @@ -116,6 +128,10 @@ impl serde::Serialize for SerializeBytesAsHexUpper<'_> {
///
/// We only deserialize from hex if the serializer is human readable, if not we call through to the
/// `Deserialize` implementation for `T`.
///
/// # Errors
///
/// Returns the deserializer error if one occurs.
pub fn deserialize<'de, D, T>(d: D) -> Result<T, D::Error>
where
D: Deserializer<'de>,
Expand Down