Skip to content

Commit

Permalink
unify debug/display impl for Terminal
Browse files Browse the repository at this point in the history
  • Loading branch information
RCasatta committed Mar 6, 2024
1 parent 752acb3 commit 395aab8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
24 changes: 10 additions & 14 deletions src/miniscript/astelem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,18 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
if ch == 'c' {
if let Terminal::PkK(ref pk) = sub.node {
// alias: pk(K) = c:pk_k(K)
return write!(f, "pk({})", pk);
return fmt_1(f, "pk(", pk, is_debug);
} else if let Terminal::RawPkH(ref pkh) = sub.node {
// `RawPkH` is currently unsupported in the descriptor spec
// alias: pkh(K) = c:pk_h(K)
// We temporarily display there using raw_pkh, but these descriptors
// are not defined in the spec yet. These are prefixed with `expr`
// in the descriptor string.
// We do not support parsing these descriptors yet.
return write!(f, "expr_raw_pkh({})", pkh);
return fmt_1(f, "expr_raw_pkh(", pkh, is_debug);
} else if let Terminal::PkH(ref pk) = sub.node {
// alias: pkh(K) = c:pk_h(K)
return write!(f, "pkh({})", pk);
return fmt_1(f, "pkh(", pk, is_debug);
}
}

Expand All @@ -119,7 +119,11 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> Terminal<Pk, Ctx> {
},
_ => {}
};
write!(f, "{}", sub)
if is_debug {
write!(f, "{:?}", sub)
} else {
write!(f, "{}", sub)
}
} else {
unreachable!();
}
Expand Down Expand Up @@ -180,7 +184,6 @@ fn conditional_fmt<D: fmt::Debug + fmt::Display>(

impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Debug for Terminal<Pk, Ctx> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {

fn fmt_type_map(f: &mut fmt::Formatter<'_>, type_map: types::Type) -> fmt::Result {
f.write_str(match type_map.corr.base {
types::Base::B => "B",
Expand Down Expand Up @@ -223,15 +226,8 @@ impl<Pk: MiniscriptKey, Ctx: ScriptContext> fmt::Debug for Terminal<Pk, Ctx> {
f.write_str("TYPECHECK FAILED")?;
}
f.write_str("]")?;
if let Some((ch, sub)) = self.wrap_char() {
fmt::Write::write_char(f, ch)?;
if sub.node.wrap_char().is_none() {
f.write_str(":")?;
}
write!(f, "{:?}", sub)
} else {
self.conditional_fmt(f, true)
}

self.conditional_fmt(f, true)
}
}

Expand Down
16 changes: 8 additions & 8 deletions src/miniscript/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -898,7 +898,7 @@ mod tests {
phantom: PhantomData,
}));
let pkk_ms: Miniscript<String, Segwitv0> = Miniscript::from_ast(pk_node).unwrap();
dummy_string_rtt(pkk_ms, "[B/onduesm]c:[K/onduesm]pk_k(\"\")", "pk()");
dummy_string_rtt(pkk_ms, "[B/onduesm]pk(\"\")", "pk()");

let pkh_node = Terminal::Check(Arc::new(Miniscript {
node: Terminal::PkH(String::from("")),
Expand All @@ -908,7 +908,7 @@ mod tests {
}));
let pkh_ms: Miniscript<String, Segwitv0> = Miniscript::from_ast(pkh_node).unwrap();

let expected_debug = "[B/nduesm]c:[K/nduesm]pk_h(\"\")";
let expected_debug = "[B/nduesm]pkh(\"\")";
let expected_display = "pkh()";

assert_eq!(pkh_ms.ty.corr.base, types::Base::B);
Expand Down Expand Up @@ -988,47 +988,47 @@ mod tests {

string_rtt(
script,
"[B/onduesm]c:[K/onduesm]pk_k(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"[B/onduesm]pk(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"pk(028c28a97bf8298bc0d23d8c749452a32e694b65e30a9472a3954ab30fe5324caa)"
);

let script: Segwitv0Script = ms_str!("pk({})", pubkey.to_string());

string_rtt(
script,
"[B/onduesm]c:[K/onduesm]pk_k(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"[B/onduesm]pk(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"pk(028c28a97bf8298bc0d23d8c749452a32e694b65e30a9472a3954ab30fe5324caa)"
);

let script: Segwitv0Script = ms_str!("tv:pk({})", pubkey.to_string());

string_rtt(
script,
"[B/onufsm]t[V/onfsm]v[B/onduesm]c:[K/onduesm]pk_k(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"[B/onufsm]t[V/onfsm]v:[B/onduesm]pk(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"tv:pk(028c28a97bf8298bc0d23d8c749452a32e694b65e30a9472a3954ab30fe5324caa)"
);

let script: Segwitv0Script = ms_str!("c:pk_h({})", pubkey.to_string());

string_display_debug_test(
script,
"[B/nduesm]c:[K/nduesm]pk_h(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"[B/nduesm]pkh(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"pkh(028c28a97bf8298bc0d23d8c749452a32e694b65e30a9472a3954ab30fe5324caa)",
);

let script: Segwitv0Script = ms_str!("pkh({})", pubkey.to_string());

string_display_debug_test(
script,
"[B/nduesm]c:[K/nduesm]pk_h(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"[B/nduesm]pkh(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"pkh(028c28a97bf8298bc0d23d8c749452a32e694b65e30a9472a3954ab30fe5324caa)",
);

let script: Segwitv0Script = ms_str!("tv:pkh({})", pubkey.to_string());

string_display_debug_test(
script,
"[B/nufsm]t[V/nfsm]v[B/nduesm]c:[K/nduesm]pk_h(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"[B/nufsm]t[V/nfsm]v:[B/nduesm]pkh(PublicKey { compressed: true, inner: PublicKey(aa4c32e50fb34a95a372940ae3654b692ea35294748c3dd2c08b29f87ba9288c8294efcb73dc719e45b91c45f084e77aebc07c1ff3ed8f37935130a36304a340) })",
"tv:pkh(028c28a97bf8298bc0d23d8c749452a32e694b65e30a9472a3954ab30fe5324caa)",
);
}
Expand Down

0 comments on commit 395aab8

Please # to comment.