1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// SPDX-License-Identifier: CC0-1.0

//! Blanket Traits
//!
//! Because of this library's heavy use of generics, we often require complicated
//! trait bounds (especially when it comes to [`FromStr`] and its
//! associated error types). These blanket traits act as aliases, allowing easier
//! descriptions of them.
//!
//! While these traits are not sealed, they have blanket-impls which prevent you
//! from directly implementing them on your own types. The traits will be
//! automatically implemented if you satisfy all the bounds.
//!

use core::str::FromStr;
use core::{fmt, hash};

use crate::MiniscriptKey;

/// Blanket trait describing a key where all associated types implement `FromStr`,
/// and all `FromStr` errors can be displayed.
pub trait FromStrKey:
    MiniscriptKey<
        Sha256 = Self::_Sha256,
        Hash256 = Self::_Hash256,
        Ripemd160 = Self::_Ripemd160,
        Hash160 = Self::_Hash160,
    > + FromStr<Err = Self::_FromStrErr>
{
    /// Dummy type. Do not use.
    type _Sha256: FromStr<Err = Self::_Sha256FromStrErr>
        + Clone
        + Eq
        + Ord
        + fmt::Display
        + fmt::Debug
        + hash::Hash;
    /// Dummy type. Do not use.
    type _Sha256FromStrErr: fmt::Debug + fmt::Display;
    /// Dummy type. Do not use.
    type _Hash256: FromStr<Err = Self::_Hash256FromStrErr>
        + Clone
        + Eq
        + Ord
        + fmt::Display
        + fmt::Debug
        + hash::Hash;
    /// Dummy type. Do not use.
    type _Hash256FromStrErr: fmt::Debug + fmt::Display;
    /// Dummy type. Do not use.
    type _Ripemd160: FromStr<Err = Self::_Ripemd160FromStrErr>
        + Clone
        + Eq
        + Ord
        + fmt::Display
        + fmt::Debug
        + hash::Hash;
    /// Dummy type. Do not use.
    type _Ripemd160FromStrErr: fmt::Debug + fmt::Display;
    /// Dummy type. Do not use.
    type _Hash160: FromStr<Err = Self::_Hash160FromStrErr>
        + Clone
        + Eq
        + Ord
        + fmt::Display
        + fmt::Debug
        + hash::Hash;
    /// Dummy type. Do not use.
    type _Hash160FromStrErr: fmt::Debug + fmt::Display;
    /// Dummy type. Do not use.
    type _FromStrErr: fmt::Debug + fmt::Display;
}

impl<T> FromStrKey for T
where
    Self: MiniscriptKey + FromStr,
    <Self as MiniscriptKey>::Sha256: FromStr,
    Self::Hash256: FromStr,
    Self::Ripemd160: FromStr,
    Self::Hash160: FromStr,
    <Self as FromStr>::Err: fmt::Debug + fmt::Display,
    <<Self as MiniscriptKey>::Sha256 as FromStr>::Err: fmt::Debug + fmt::Display,
    <Self::Hash256 as FromStr>::Err: fmt::Debug + fmt::Display,
    <Self::Ripemd160 as FromStr>::Err: fmt::Debug + fmt::Display,
    <Self::Hash160 as FromStr>::Err: fmt::Debug + fmt::Display,
{
    type _Sha256 = <T as MiniscriptKey>::Sha256;
    type _Sha256FromStrErr = <<T as MiniscriptKey>::Sha256 as FromStr>::Err;
    type _Hash256 = <T as MiniscriptKey>::Hash256;
    type _Hash256FromStrErr = <<T as MiniscriptKey>::Hash256 as FromStr>::Err;
    type _Ripemd160 = <T as MiniscriptKey>::Ripemd160;
    type _Ripemd160FromStrErr = <<T as MiniscriptKey>::Ripemd160 as FromStr>::Err;
    type _Hash160 = <T as MiniscriptKey>::Hash160;
    type _Hash160FromStrErr = <<T as MiniscriptKey>::Hash160 as FromStr>::Err;
    type _FromStrErr = <T as FromStr>::Err;
}