Skip to content
This repository was archived by the owner on Oct 22, 2026. It is now read-only.

Pretty print a policy - #222

Open
sanket1729 wants to merge 2 commits into
rust-bitcoin:masterfrom
sanket1729:policy_diff
Open

sanket1729 wants to merge 2 commits into
rust-bitcoin:masterfrom
sanket1729:policy_diff

Conversation

@sanket1729

Copy link
Copy Markdown
Member

Adds methods to pretty print policies similar to directory tree printing in Linux. Prints diff in colors which highlights the differences between the two policies.

Our representation of Semantic policies in only in Thresh, so we combine or(pk,or(pk,pk)) into thresh(1,pk,pk,pk). And so we use the latter in comparing two policies that complicate some of the implementation but is still fairly small.

Comment thread src/policy/semantic.rs
(x, y) => {
let red = "\x1b[0;31m";
let nc = "\x1b[0m";
let green = "\x1b[0;32m";

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.

I think we need to come up with a different encoding scheme. It doesn't have to be pretty, we could provide a shellscript or something that converted it into a colorized version.

But using shell escape sequences means that we won't work on windows and are likely to confuse other command line tools.

sanket1729 added a commit to sanket1729/rust-miniscript that referenced this pull request Oct 1, 2021
e829434 Implemented Policy Diff (sanket1729)
4d23b79 Pretty print a policy (sanket1729)

Pull request description:

  Adds methods to pretty print policies similar to directory tree printing in Linux. Prints diff in colors which highlights the differences between the two policies.

  Our representation of Semantic policies in only in Thresh, so we combine `or(pk,or(pk,pk))` into `thresh(1,pk,pk,pk)`. And so we use the latter in comparing two policies that complicate some of the implementation but is still fairly small.

Top commit has no ACKs.

Tree-SHA512: 8efd11b2c2582a4e26455ccb718d1ad2016ead3507746bd98a16240c7c72cf411df7fa01d817aee3a42c138b8ff7b887e7104d29d15f7a57a61620eee20a9d2b
Comment thread src/policy/semantic.rs
}

/// Pretty Print a tree
pub fn pprint_tree(&self) {

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.

In 4d23b79:

Can we make this pub(crate)? I think we can come up with a much better API here and I'd rather not expose this one.

portlandhodl added a commit to portlandhodl/rust-miniscript that referenced this pull request Aug 31, 2026
Modernized port of rust-bitcoin#222. Adds:

- PolicyDiff::new(), which computes the syntactic difference between
  two normalized policies: children of same-shape thresholds that
  compare equal are matched up (position-independently) and the
  unmatched children of each side are returned.
- Policy::to_tree_string(), which renders a policy in the style of
  the UNIX tree command.
- Policy::to_diff_string(), which renders the difference between two
  policies, marking differing sub-policies with `- `/`+ ` instead
  of ANSI escape sequences.

All functions are no_std-compatible and return Strings rather than
printing to stdout.

Co-authored-by: Sanket Kanjalkar <sanket1729@gmail.com>
@portlandhodl

Copy link
Copy Markdown
Collaborator

My shot at this PR if anyone is interested. Though I'm having a hard time seeing a solid use case.

master...portlandhodl:rust-miniscript:policy-diff

@apoelstra

Copy link
Copy Markdown
Member

e829434 needs rebase

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants