bitcoin_rpc_json/
mining.rs

1// Copyright 2018 Jean Pierre Dudey <jeandudey@hotmail.com>
2//
3// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
5// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6// option. This file may not be copied, modified, or distributed
7// except according to those terms.
8
9//! Mining related RPC result types.
10
11use std::fmt::{self, Formatter};
12use std::str::FromStr;
13
14use serde::{de, ser};
15use strason::Json;
16
17/// Models the result of "estimatesmartfee"
18#[derive(Debug, Clone, Deserialize, Serialize)]
19pub struct EstimateSmartFee {
20    /// Estimate fee rate in BTC/kB.
21    pub feerate: Option<Json>,
22    /// Errors encountered during processing.
23    pub errors: Option<Vec<String>>,
24    /// Block number where estimate was found.
25    pub blocks: i64,
26}
27
28#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
29pub enum EstimateMode {
30    Unset,
31    Economical,
32    Conservative,
33}
34
35impl FromStr for EstimateMode {
36    type Err = ();
37
38    fn from_str(s: &str) -> Result<Self, Self::Err> {
39        match s {
40            "UNSET" => Ok(EstimateMode::Unset),
41            "ECONOMICAL" => Ok(EstimateMode::Economical),
42            "CONSERVATIVE" => Ok(EstimateMode::Conservative),
43            _ => Err(()),
44        }
45    }
46}
47
48impl<'de> de::Deserialize<'de> for EstimateMode {
49    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
50    where
51        D: de::Deserializer<'de>,
52    {
53        struct Visitor;
54
55        impl<'de> de::Visitor<'de> for Visitor {
56            type Value = EstimateMode;
57
58            fn expecting(&self, fmt: &mut Formatter) -> fmt::Result {
59                write!(fmt, "estimate mode")
60            }
61
62            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
63            where
64                E: de::Error,
65            {
66                EstimateMode::from_str(v)
67                    .map_err(|_e| de::Error::custom("invalid string"))
68            }
69
70            fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
71            where
72                E: de::Error,
73            {
74                EstimateMode::from_str(v)
75                    .map_err(|_e| de::Error::custom("invalid string"))
76            }
77
78            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
79            where
80                E: de::Error,
81            {
82                EstimateMode::from_str(&*v)
83                    .map_err(|_e| de::Error::custom("invalid string"))
84            }
85        }
86
87        deserializer.deserialize_str(Visitor)
88    }
89}
90
91impl ser::Serialize for EstimateMode {
92    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
93    where
94        S: ser::Serializer,
95    {
96        let s = match *self {
97            EstimateMode::Unset => "UNSET",
98            EstimateMode::Economical => "ECONOMICAL",
99            EstimateMode::Conservative => "CONSERVATIVE",
100        };
101
102        serializer.serialize_str(s)
103    }
104}
105