bp/
bp.rs

1// Bitcoin protocol core library.
2//
3// SPDX-License-Identifier: Apache-2.0
4//
5// Written in 2019-2024 by
6//     Dr Maxim Orlovsky <orlovsky@lnp-bp.org>
7//
8// Copyright (C) 2019-2024 LNP/BP Standards Association. All rights reserved.
9//
10// Licensed under the Apache License, Version 2.0 (the "License");
11// you may not use this file except in compliance with the License.
12// You may obtain a copy of the License at
13//
14//     http://www.apache.org/licenses/LICENSE-2.0
15//
16// Unless required by applicable law or agreed to in writing, software
17// distributed under the License is distributed on an "AS IS" BASIS,
18// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19// See the License for the specific language governing permissions and
20// limitations under the License.
21
22use strict_encoding::{StrictDecode, StrictDumb, StrictEncode};
23
24/// Enumeration over types related to bitcoin protocol-compatible chains.
25#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
26#[derive(StrictType, StrictDumb, StrictEncode, StrictDecode)]
27#[strict_type(lib = dbc::LIB_NAME_BPCORE, tags = custom, dumb = Self::Bitcoin(strict_dumb!()))]
28#[cfg_attr(feature = "serde", derive(Serialize, Deserialize), serde(rename_all = "camelCase"))]
29pub enum Bp<T>
30where T: StrictDumb + StrictEncode + StrictDecode
31{
32    /// Bitcoin blockchain-based.
33    ///
34    /// NB: The type does not distinguish mainnet from testnets.
35    #[strict_type(tag = 0x00)]
36    Bitcoin(T),
37
38    /// Liquid blockchain-based
39    ///
40    /// NB: The type does not distinguish mainnet from testnets.
41    #[strict_type(tag = 0x01)]
42    Liquid(T),
43}
44
45impl<T: StrictDumb + StrictEncode + StrictDecode> Bp<T> {
46    /// Detects if the variant matches bitcoin blockchain.
47    pub fn is_bitcoin(&self) -> bool { matches!(self, Bp::Bitcoin(_)) }
48    /// Detects if the variant matches liquid blockchain.
49    pub fn is_liquid(&self) -> bool { matches!(self, Bp::Liquid(_)) }
50    /// Returns bitcoin blockchain variant as an optional.
51    pub fn as_bitcoin(&self) -> Option<&T> {
52        match self {
53            Bp::Bitcoin(t) => Some(t),
54            Bp::Liquid(_) => None,
55        }
56    }
57    /// Returns liquid blockchain variant as an optional.
58    pub fn as_liquid(&self) -> Option<&T> {
59        match self {
60            Bp::Bitcoin(_) => None,
61            Bp::Liquid(t) => Some(t),
62        }
63    }
64    /// Converts into bitcoin blockchain optional.
65    pub fn into_bitcoin(self) -> Option<T> {
66        match self {
67            Bp::Bitcoin(t) => Some(t),
68            Bp::Liquid(_) => None,
69        }
70    }
71    /// Converts into liquid blockchain optional.
72    pub fn into_liquid(self) -> Option<T> {
73        match self {
74            Bp::Bitcoin(_) => None,
75            Bp::Liquid(t) => Some(t),
76        }
77    }
78
79    /// Maps the value from one internal type into another.
80    pub fn map<U: StrictDumb + StrictEncode + StrictDecode>(self, f: impl FnOnce(T) -> U) -> Bp<U> {
81        match self {
82            Bp::Bitcoin(t) => Bp::Bitcoin(f(t)),
83            Bp::Liquid(t) => Bp::Liquid(f(t)),
84        }
85    }
86
87    /// Maps the value from one internal type into another, covering cases which
88    /// may error.
89    pub fn try_map<U: StrictDumb + StrictEncode + StrictDecode, E>(
90        self,
91        f: impl FnOnce(T) -> Result<U, E>,
92    ) -> Result<Bp<U>, E> {
93        match self {
94            Bp::Bitcoin(t) => f(t).map(Bp::Bitcoin),
95            Bp::Liquid(t) => f(t).map(Bp::Liquid),
96        }
97    }
98
99    /// Maps the value from one internal type into another, covering cases which
100    /// may result in an optional value.
101    pub fn maybe_map<U: StrictDumb + StrictEncode + StrictDecode>(
102        self,
103        f: impl FnOnce(T) -> Option<U>,
104    ) -> Option<Bp<U>> {
105        match self {
106            Bp::Bitcoin(t) => f(t).map(Bp::Bitcoin),
107            Bp::Liquid(t) => f(t).map(Bp::Liquid),
108        }
109    }
110}