Skip to content

Commit 3360545

Browse files
committed
Deduplicate Definitor/Derivator into shared enum
Address review feedback: unify the duplicated `Definitor` and `Derivator` translator structs into a single `enum Definitor { FromPk, AtIndex(u32) }` shared by both `into_definite()` and `at_derivation_index()`.
1 parent f4599d6 commit 3360545

1 file changed

Lines changed: 24 additions & 26 deletions

File tree

src/descriptor/mod.rs

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,28 @@ impl<Pk: MiniscriptKey> ForEachKey<Pk> for Descriptor<Pk> {
650650
}
651651
}
652652

653+
/// Translates [`DescriptorPublicKey`]s into [`DefiniteDescriptorKey`]s.
654+
enum Definitor {
655+
/// Convert a non-wildcard key directly via [`DefiniteDescriptorKey::new`].
656+
FromPk,
657+
/// Replace the wildcard with a specific derivation index.
658+
AtIndex(u32),
659+
}
660+
661+
impl Translator<DescriptorPublicKey> for Definitor {
662+
type TargetPk = DefiniteDescriptorKey;
663+
type Error = NonDefiniteKeyError;
664+
665+
fn pk(&mut self, pk: &DescriptorPublicKey) -> Result<Self::TargetPk, Self::Error> {
666+
match self {
667+
Definitor::FromPk => DefiniteDescriptorKey::new(pk.clone()),
668+
Definitor::AtIndex(idx) => pk.clone().at_derivation_index(*idx),
669+
}
670+
}
671+
672+
translate_hash_clone!(DescriptorPublicKey);
673+
}
674+
653675
impl Descriptor<DescriptorPublicKey> {
654676
/// Whether or not the descriptor has any wildcards i.e. `/*`.
655677
pub fn has_wildcard(&self) -> bool { self.for_any_key(|key| key.has_wildcard()) }
@@ -670,19 +692,7 @@ impl Descriptor<DescriptorPublicKey> {
670692
if self.has_wildcard() {
671693
return Err(NonDefiniteKeyError::Wildcard);
672694
}
673-
struct Definitor;
674-
675-
impl Translator<DescriptorPublicKey> for Definitor {
676-
type TargetPk = DefiniteDescriptorKey;
677-
type Error = NonDefiniteKeyError;
678-
679-
fn pk(&mut self, pk: &DescriptorPublicKey) -> Result<Self::TargetPk, Self::Error> {
680-
DefiniteDescriptorKey::new(pk.clone())
681-
}
682-
683-
translate_hash_clone!(DescriptorPublicKey);
684-
}
685-
self.translate_pk(&mut Definitor)
695+
self.translate_pk(&mut Definitor::FromPk)
686696
.map_err(|e| e.expect_translator_err("No Context errors while translating"))
687697
}
688698

@@ -700,19 +710,7 @@ impl Descriptor<DescriptorPublicKey> {
700710
if !self.has_wildcard() {
701711
return Err(NonDefiniteKeyError::NoWildcard);
702712
}
703-
struct Derivator(u32);
704-
705-
impl Translator<DescriptorPublicKey> for Derivator {
706-
type TargetPk = DefiniteDescriptorKey;
707-
type Error = NonDefiniteKeyError;
708-
709-
fn pk(&mut self, pk: &DescriptorPublicKey) -> Result<Self::TargetPk, Self::Error> {
710-
pk.clone().at_derivation_index(self.0)
711-
}
712-
713-
translate_hash_clone!(DescriptorPublicKey);
714-
}
715-
self.translate_pk(&mut Derivator(index))
713+
self.translate_pk(&mut Definitor::AtIndex(index))
716714
.map_err(|e| e.expect_translator_err("No Context errors while translating"))
717715
}
718716

0 commit comments

Comments
 (0)