Skip to content

macro for result-domain PSBT structs - #68

Merged
arminsabouri merged 1 commit into
mainfrom
struct-macro
Nov 12, 2026
Merged

arminsabouri merged 1 commit into
mainfrom
struct-macro

Conversation

@yuval-block

Copy link
Copy Markdown
Collaborator

Add joinable_struct!, a macro_rules helper that generates the result-domain wrapper for PSBT record types (Global, Input, Output)

@yuval-block
yuval-block added this pull request to stack #69 November 11, 2026 17:35
@github-code-quality

github-code-quality Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

Code coverage overview

Languages: Rust

Rust / code-coverage/cargo-llvm-cov

main 9db606e struct-macro 94c1abc +/-
100% 100% 0%

ℹ️ Calculations represent line coverage. Results may change as calculations are updated.


Updated November 12, 2026 18:53 UTC

@github-actions

Copy link
Copy Markdown
range-diff for force push 5a9b58f9eb → 5ded1676b9 November 11, 2026 18:17 UTC: 1 unchanged; 1 commit; base 1bd370a5e5 → 9594379096 (diverged, 3 ahead and 1 behind)

reproduce: git range-diff 1bd370a..5a9b58f 9594379..5ded167

1:  5a9b58f9eb +245 -0 3f = 1:  5ded1676b9 +245 -0 3f macro for result-domain PSBT structs

@github-actions

Copy link
Copy Markdown
range-diff for force push 5a9b58f9eb → 5ded1676b9 November 11, 2026 18:17 UTC: 1 unchanged; 1 commit; base 1bd370a5e5 → 9594379096 (diverged, 3 ahead and 1 behind)

reproduce: git range-diff 1bd370a..5a9b58f 9594379..5ded167

1:  5a9b58f9eb +245 -0 3f = 1:  5ded1676b9 +245 -0 3f macro for result-domain PSBT structs

@github-actions

Copy link
Copy Markdown
range-diff for force push 5ded1676b9 → bb670a7c05 November 11, 2026 18:30 UTC: 1 modified; 1 commit; same base 9594379096

reproduce: git range-diff 9594379..5ded167 9594379..bb670a7

1:  5ded1676b9 +245 -0 3f ! 1:  bb670a7c05 +245 -0 3f macro for result-domain PSBT structs
1: 5ded1676b9 ! 1: bb670a7c05 macro for result-domain PSBT structs
@@ crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs (new)
 +/// 3. `Join for $Result`
 +/// 4. `$Result::is_ok`
 +/// 5. `$Result::try_unwrap`
-+#[allow(unused_macros)]
++#[expect(unused_macros, reason = "expanded by the PSBT field types")]
 +macro_rules! joinable_struct {
 +    // ── Entry point ──────────────────────────────────────────
 +    (

@github-actions

Copy link
Copy Markdown
range-diff for force push bb670a7c05 → 9889d0b565 November 11, 2026 18:42 UTC: 1 unchanged; 1 commit; base 9594379096 → 895e63a301 (diverged, 2 ahead and 3 behind)

reproduce: git range-diff 9594379..bb670a7 895e63a..9889d0b

1:  bb670a7c05 +245 -0 3f = 1:  9889d0b565 +245 -0 3f macro for result-domain PSBT structs

@github-actions

Copy link
Copy Markdown
range-diff for force push bb670a7c05 → 9889d0b565 November 11, 2026 18:42 UTC: 1 unchanged; 1 commit; base 9594379096 → 895e63a301 (diverged, 2 ahead and 3 behind)

reproduce: git range-diff 9594379..bb670a7 895e63a..9889d0b

1:  bb670a7c05 +245 -0 3f = 1:  9889d0b565 +245 -0 3f macro for result-domain PSBT structs

@yuval-block
yuval-block removed this pull request from stack #69 November 11, 2026 18:44
@yuval-block
yuval-block added this pull request to stack #71 November 11, 2026 18:44
Base automatically changed from psbt-deps to main November 11, 2026 19:43
@github-actions

Copy link
Copy Markdown
range-diff for force push 9889d0b565 → 5a2d70eae9 November 11, 2026 19:43 UTC: 1 unchanged; 1 commit; base 895e63a301 → 5346f2b34e (descendant, 4 commits ahead)

reproduce: git range-diff 895e63a..9889d0b 5346f2b..5a2d70e

1:  9889d0b565 +245 -0 3f = 1:  5a2d70eae9 +245 -0 3f macro for result-domain PSBT structs

@github-actions

Copy link
Copy Markdown
range-diff for force push 5a2d70eae9 → 9d3ba507d1 November 11, 2026 22:43 UTC: 1 modified; 1 commit; same base 5346f2b34e

reproduce: git range-diff 5346f2b..5a2d70e 5346f2b..9d3ba50

1:  5a2d70eae9 +245 -0 3f ! 1:  9d3ba507d1 +307 -0 3f macro for result-domain PSBT structs
1: 5a2d70eae9 ! 1: 9d3ba507d1 macro for result-domain PSBT structs
@@ crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs (new)
 +/// 3. `Join for $Result`
 +/// 4. `$Result::is_ok`
 +/// 5. `$Result::try_unwrap`
-+#[expect(unused_macros, reason = "expanded by the PSBT field types")]
++///
++/// The field list must name at least one field: `is_ok` is generated as the
++/// conjunction of the per-field checks, which has no expansion for an empty
++/// list.
++#[cfg_attr(
++    not(all(test, feature = "unit-tests")),
++    expect(unused_macros, reason = "expanded by the PSBT field types")
++)]
 +macro_rules! joinable_struct {
 +    // ── Entry point ──────────────────────────────────────────
 +    (
@@ crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs (new)
 +            ///
 +            /// Only visits fields that contain conflicts. Clean fields are skipped.
 +            /// For map fields, visits each conflicted entry with the field name.
-+            #[allow(dead_code)]
++            #[cfg_attr(
++                not(test),
++                expect(dead_code, reason = "only tests enumerate conflicted fields so far")
++            )]
 +            pub(crate) fn for_each_conflict(
 +                &self,
 +                mut f: impl FnMut(&str, &dyn std::fmt::Debug),
@@ crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs (new)
 +        $crate::collections::btreemap::ResultBTreeMapExt::try_unwrap($self.$field)
 +            .expect("verified all fields are Ok")
 +    };
++}
++
++#[cfg(all(test, feature = "unit-tests"))]
++#[cfg_attr(coverage_nightly, coverage(off))]
++mod tests {
++    use std::collections::BTreeMap;
++
++    use crate::lattice::join::Join;
++
++    #[derive(Debug, PartialEq)]
++    pub struct Sample {
++        scalar: u8,
++        option: Option<u8>,
++        map: BTreeMap<u8, u8>,
++    }
++
++    joinable_struct! {
++        /// Result-domain form of [`Sample`], one field per category.
++        source: Sample,
++        result: ResultSample,
++        ext: SampleExt,
++        fields: {
++            scalar: u8,
++            option: Option<u8>,
++            map: BTreeMap<u8, u8>,
++        }
++    }
++
++    fn sample(scalar: u8, option: u8, value: u8) -> Sample {
++        Sample {
++            scalar,
++            option: Some(option),
++            map: BTreeMap::from([(0, value)]),
++        }
++    }
++
++    #[test]
++    fn equal_structs_join_and_unwrap() {
++        let joined = sample(1, 2, 3).wrap().join(sample(1, 2, 3).wrap());
++        assert!(joined.is_ok());
++        assert_eq!(joined.try_unwrap().ok(), Some(sample(1, 2, 3)));
++    }
++
++    #[test]
++    fn conflicting_fields_are_reported_by_name() {
++        let joined = sample(1, 2, 3).wrap().join(sample(4, 5, 6).wrap());
++        assert!(!joined.is_ok());
++        let mut fields = Vec::new();
++        joined.for_each_conflict(|field, _| fields.push(field.to_owned()));
++        assert_eq!(fields, ["scalar", "option", "map"]);
++        assert!(joined.try_unwrap().is_err());
++    }
 +}
 
  ## crates/concurrent-psbt/src/psbt/mod.rs ##

Add joinable_struct!, a macro_rules helper that generates the
result-domain wrapper for PSBT record types (Global, Input, Output)
@github-actions

Copy link
Copy Markdown
range-diff for force push c5e82a0f1a → 94c1abc38e November 12, 2026 18:47 UTC: 1 modified, 2 dropped; 3 → 1 commit; base 5346f2b34e → 9db606ef81 (descendant, 2 commits ahead)

reproduce: git range-diff 5346f2b..c5e82a0 9db606e..94c1abc

1:  9d3ba507d1 +307  -0 3f ! 1:  94c1abc38e +328  -0 3f macro for result-domain PSBT structs
2:  e99ad1e9f9 +172 -32 6f < -:  ----------             Introduce joinable Output struct type
3:  0df3dfe4bc +152  -1 4f < -:  ----------             introduce output.UniqueId
1: 9d3ba507d1 ! 1: 94c1abc38e macro for result-domain PSBT structs
@@ crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs (new)
 +/// - `name: Option<Type>`          → option → `name: Option<JoinResult<Type>>`
 +/// - `name: BTreeMap<K, V>`        → map    → `name: BTreeMap<K, JoinResult<V>>`
 +///
++/// Classification is syntactic: only the bare identifiers `Option` and
++/// `BTreeMap` select the option and map rules. A `macro_rules!` macro cannot
++/// resolve paths or aliases, so `std::option::Option<T>`,
++/// `std::collections::BTreeMap<K, V>` or a type alias for either is classified
++/// as a scalar, which only compiles if the whole type implements
++/// `PartialJoin`. Import the types and write them bare.
++///
 +/// Generates:
 +/// 1. `$Result` struct with transformed field types
 +/// 2. `$Ext` trait with `wrap` method on the source type
@@ crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs (new)
 +                )&&*
 +            }
 +
-+            /// Visit each conflicted field, calling `f(field_name, &conflict)`.
++            /// Visit each conflicted field, calling `f(field_name, key, &conflict)`.
 +            ///
 +            /// Only visits fields that contain conflicts. Clean fields are skipped.
-+            /// For map fields, visits each conflicted entry with the field name.
++            /// For map fields, visits each conflicted entry with `Some(&key)`, so
++            /// callers can tell which entry conflicts. For scalar and option
++            /// fields `key` is `None`.
 +            #[cfg_attr(
 +                not(test),
 +                expect(dead_code, reason = "only tests enumerate conflicted fields so far")
 +            )]
 +            pub(crate) fn for_each_conflict(
 +                &self,
-+                mut f: impl FnMut(&str, &dyn std::fmt::Debug),
++                mut f: impl FnMut(&str, Option<&dyn std::fmt::Debug>, &dyn std::fmt::Debug),
 +            ) {
 +                $(
 +                    joinable_struct!(@visit_conflict $category self . $field, f);
 +                )*
 +            }
 +
-+            #[allow(clippy::result_large_err)]
 +            pub fn try_unwrap(self) -> Result<$Source, Self> {
 +                if !self.is_ok() {
 +                    return Err(self);
@@ crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs (new)
 +
 +    (@visit_conflict scalar $self:ident . $field:ident , $f:expr) => {
 +        if let Err(c) = &$self.$field {
-+            ($f)(stringify!($field), c as &dyn std::fmt::Debug);
++            ($f)(stringify!($field), None, c as &dyn std::fmt::Debug);
 +        }
 +    };
 +    (@visit_conflict option $self:ident . $field:ident , $f:expr) => {
 +        if let Some(Err(c)) = &$self.$field {
-+            ($f)(stringify!($field), c as &dyn std::fmt::Debug);
++            ($f)(stringify!($field), None, c as &dyn std::fmt::Debug);
 +        }
 +    };
 +    (@visit_conflict map $self:ident . $field:ident , $f:expr) => {
-+        for (_k, v) in &$self.$field {
++        for (k, v) in &$self.$field {
 +            if let Err(c) = v {
-+                ($f)(stringify!($field), c as &dyn std::fmt::Debug);
++                ($f)(
++                    stringify!($field),
++                    Some(k as &dyn std::fmt::Debug),
++                    c as &dyn std::fmt::Debug,
++                );
 +            }
 +        }
 +    };
@@ crates/concurrent-psbt/src/psbt/joinable_struct_macro.rs (new)
 +    }
 +
 +    #[test]
-+    fn conflicting_fields_are_reported_by_name() {
++    fn conflicting_fields_are_reported_by_name_and_key() {
 +        let joined = sample(1, 2, 3).wrap().join(sample(4, 5, 6).wrap());
 +        assert!(!joined.is_ok());
 +        let mut fields = Vec::new();
-+        joined.for_each_conflict(|field, _| fields.push(field.to_owned()));
-+        assert_eq!(fields, ["scalar", "option", "map"]);
++        joined.for_each_conflict(|field, key, _| {
++            fields.push((field.to_owned(), key.map(|k| format!("{k:?}"))));
++        });
++        assert_eq!(
++            fields,
++            [
++                ("scalar".to_owned(), None),
++                ("option".to_owned(), None),
++                ("map".to_owned(), Some("0".to_owned())),
++            ]
++        );
 +        assert!(joined.try_unwrap().is_err());
 +    }
 +}

@yuval-block

Copy link
Copy Markdown
Collaborator Author

I think all outstanding feedback is addressed, we should merge this before its dependents which i pulled out of this (#74 and #77)

@arminsabouri arminsabouri left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CodeACK

@arminsabouri
arminsabouri added this pull request to the merge queue Nov 12, 2026

@bc1cindy bc1cindy left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK 94c1abc

Merged via the queue into main with commit 6fa4646 Nov 12, 2026
6 checks passed
@arminsabouri
arminsabouri deleted the struct-macro branch November 12, 2026 19:36
Mshehu5 pushed a commit to Mshehu5/concurrent-psbt that referenced this pull request Nov 14, 2026
Reopens fungi-protocol#74, which was merged into \`struct-macro\` before fungi-protocol#68 landed.
Same commit, now stacked on fungi-protocol#68.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Mshehu5 pushed a commit to Mshehu5/concurrent-psbt that referenced this pull request Nov 14, 2026
Introduce the PSBT_OUT_UNIQUE_ID field (currently proprietary field)
which serves as a stable key for outputs in unordered PSBTS.

Reopens fungi-protocol#77, which was merged into \`struct-macro\` before fungi-protocol#68 landed.
Same commit, now stacked on fungi-protocol#90.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants