Skip to content
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
24 changes: 3 additions & 21 deletions .github/workflows/dusk_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,8 @@ on:
name: Continuous integration

jobs:
code-quality:
name: Code quality
ci:
name: CI
uses: dusk-network/.github/.github/workflows/run-make.yml@main
Comment thread
HDauven marked this conversation as resolved.
with:
make_target: cq

documentation:
name: Build documentation
uses: dusk-network/.github/.github/workflows/run-make.yml@main
with:
make_target: doc

test:
name: Run tests
uses: dusk-network/.github/.github/workflows/run-make.yml@main
with:
make_target: test

no-std:
name: Run no-std tests
uses: dusk-network/.github/.github/workflows/run-make.yml@main
with:
make_target: no-std
make_target: ci
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ cq: ## Run code quality checks (formatting + clippy)
@$(MAKE) fmt CHECK=1
@$(MAKE) clippy

ci: cq test no-std doc ## Run continuous integration checks

check: ## Type-check
@cargo check --all-features

Expand All @@ -31,4 +33,4 @@ doc: ## Generate docs
clean: ## Clean build artifacts
@cargo clean

.PHONY: help fmt clippy cq check test no-std doc clean
.PHONY: help fmt clippy cq ci check test no-std doc clean
3 changes: 3 additions & 0 deletions derive-hex/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Simplify generated formatting and remove the unused `proc-macro2` dependency
[#55]
- Update Syn to 3 [#53]
- Raise the MSRV to Rust 1.96.1 [#51]

Expand Down Expand Up @@ -44,6 +46,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
[#45]: https://github.com/dusk-network/dusk-bytes/issues/45
[#51]: https://github.com/dusk-network/dusk-bytes/issues/51
[#53]: https://github.com/dusk-network/dusk-bytes/issues/53
[#55]: https://github.com/dusk-network/dusk-bytes/issues/55
[unreleased]: https://github.com/dusk-network/dusk-bytes/compare/derive-hex-0.1.2...HEAD
[0.1.2]: https://github.com/dusk-network/dusk-bytes/releases/tag/derive-hex-0.1.2
[0.1.1]: https://github.com/dusk-network/dusk-bytes/releases/tag/derive-hex-0.1.1
Expand Down
1 change: 0 additions & 1 deletion derive-hex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@ license = "MPL-2.0"
proc-macro = true

[dependencies]
proc-macro2 = "1.0"
quote = "1.0"
syn = "3.0"
29 changes: 8 additions & 21 deletions derive-hex/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ pub fn derive_hex(item: TokenStream) -> TokenStream {
(quote! {
impl core::fmt::LowerHex for #ident {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let bytes = self.to_bytes();

if f.alternate() {
write!(f, "0x")?
}

for byte in &bytes[..] {
write!(f, "{:02x}", &byte)?
for byte in self.to_bytes() {
write!(f, "{byte:02x}")?
}
Comment thread
HDauven marked this conversation as resolved.

Ok(())
Expand All @@ -33,14 +31,12 @@ pub fn derive_hex(item: TokenStream) -> TokenStream {

impl core::fmt::UpperHex for #ident {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
let bytes = self.to_bytes();

if f.alternate() {
write!(f, "0x")?
}

for byte in &bytes[..] {
write!(f, "{:02X}", &byte)?
for byte in self.to_bytes() {
write!(f, "{byte:02X}")?
}

Ok(())
Expand All @@ -59,22 +55,13 @@ pub fn derive_hex_debug(item: TokenStream) -> TokenStream {
let dbg: TokenStream = (quote! {
impl core::fmt::Debug for #ident {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
// Once we format an object using the debug notation (e.g. `{:x?}`)
// there is absolutely NO WAY to detect the flag for the lowerhex
// or upperhex, and therefore forwarding to the relevant formatter.
// Two methods for this purpose exists, but they're not exposed
// because they didn't agree on a name yet, see:
// `Formatter` does not publicly expose the debug-hex case. Bit 5 is
// `FlagV1::DebugUpperHex` in `core`:
// <https://github.com/rust-lang/rust/blob/90442458ac46b1d5eed752c316da25450f67285b/library/core/src/fmt/mod.rs#L1817-L1825>
//
// Therefore the only way is using the deprecated method `flags`,
// implementing the same logic of the forementioned methods.

// We also do not have access to the `FlagV1` enum since it's
// private.
let FlagV1_DebugUpperHex = 5_u32;
const DEBUG_UPPER_HEX: u32 = 1 << 5;

#[allow(deprecated)]
if f.flags() & (1 << FlagV1_DebugUpperHex) !=0 {
if f.flags() & DEBUG_UPPER_HEX != 0 {
core::fmt::UpperHex::fmt(self, f)
} else { // LowerHex is always the default for debug
core::fmt::LowerHex::fmt(self, f)
Expand Down
74 changes: 12 additions & 62 deletions derive-hex/tests/hex_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,66 +20,16 @@ impl Beef {
}
}

mod display {
use super::*;

#[test]
fn lower_hex() {
let beef = Beef {};

assert_eq!(format!("{:x}", beef), "beef");
}

#[test]
fn lower_hex_alt() {
let beef = Beef {};

assert_eq!(format!("{:#x}", beef), "0xbeef");
}

#[test]
fn upper_hex() {
let beef = Beef {};

assert_eq!(format!("{:X}", beef), "BEEF");
}

#[test]
fn upper_hex_alt() {
let beef = Beef {};

assert_eq!(format!("{:#X}", beef), "0xBEEF");
}
}

mod debug {
use super::*;

#[test]
fn lower_hex() {
let beef = Beef {};

assert_eq!(format!("{:x?}", beef), "beef");
}

#[test]
fn lower_hex_alt() {
let beef = Beef {};

assert_eq!(format!("{:#x?}", beef), "0xbeef");
}

#[test]
fn upper_hex() {
let beef = Beef {};

assert_eq!(format!("{:X?}", beef), "BEEF");
}

#[test]
fn upper_hex_alt() {
let beef = Beef {};

assert_eq!(format!("{:#X?}", beef), "0xBEEF");
}
#[test]
fn formatting() {
let beef = Beef {};

assert_eq!(format!("{:x}", beef), "beef");
assert_eq!(format!("{:#x}", beef), "0xbeef");
assert_eq!(format!("{:X}", beef), "BEEF");
assert_eq!(format!("{:#X}", beef), "0xBEEF");
assert_eq!(format!("{:x?}", beef), "beef");
assert_eq!(format!("{:#x?}", beef), "0xbeef");
assert_eq!(format!("{:X?}", beef), "BEEF");
assert_eq!(format!("{:#X?}", beef), "0xBEEF");
}
34 changes: 33 additions & 1 deletion dusk-bytes/tests/parse_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

mod common;
use common::{Beef, BeefError};
use dusk_bytes::{ParseHexStr, Serializable};
use dusk_bytes::{Error, ParseHexStr, Serializable};

#[test]
fn parse_correct_chars() -> Result<(), BeefError> {
Expand Down Expand Up @@ -39,6 +39,26 @@ fn parse_wrong_chars() {
)
}

#[test]
fn parse_reports_length_and_low_nibble_errors() {
assert_eq!(
u16::from_hex_str("bee"),
Err(Error::BadLength {
found: 3,
expected: 4,
})
);
assert_eq!(
u8::from_hex_str("0g"),
Err(Error::InvalidChar { ch: 'g', index: 1 })
);
}

#[test]
fn parse_accepts_uppercase_hex() {
assert_eq!(u16::from_hex_str("BEEF"), Ok(0xefbe));
}

mod constant {
use dusk_bytes::hex;

Expand All @@ -61,6 +81,18 @@ mod constant {
assert_eq!(EMPTY_BIG, [0x0, 0x0]);
}

#[test]
fn runtime_matches_const_fixtures() {
let input = std::hint::black_box(*b"beef");
let exact: [u8; 2] = hex(&input);
let truncated: [u8; 1] = hex(&input);
let padded: [u8; 3] = hex(&input);

assert_eq!(exact, BEEF);
assert_eq!(truncated, BEEF_SMALL);
assert_eq!(padded, BEEF_BIG);
}

#[test]
#[should_panic(expected = "hex(): input length must be even")]
fn reject_odd_length() {
Expand Down
Loading