zecwallet_parser/zwl/
data.rs1use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt};
2use std::{
3 fmt::{self, Display},
4 io::{self, Read},
5};
6use zcash_encoding::Optional;
7
8#[derive(Clone, Debug)]
10pub struct WalletZecPriceInfo {
11 pub zec_price: Option<(u64, f64)>,
13
14 pub currency: String,
16
17 pub last_historical_prices_fetched_at: Option<u64>,
19
20 pub historical_prices_retry_count: u64,
22}
23
24impl Default for WalletZecPriceInfo {
25 fn default() -> Self {
26 Self::new()
27 }
28}
29
30impl WalletZecPriceInfo {
31 pub fn new() -> Self {
32 Self {
33 zec_price: None,
34 currency: "USD".to_string(), last_historical_prices_fetched_at: None,
36 historical_prices_retry_count: 0,
37 }
38 }
39
40 pub fn serialized_version() -> u64 {
41 20
42 }
43
44 pub fn read<R: Read>(mut reader: R) -> io::Result<Self> {
45 let version = reader.read_u64::<LittleEndian>()?;
46 if version > Self::serialized_version() {
47 return Err(io::Error::new(
48 io::ErrorKind::InvalidData,
49 "Can't read ZecPriceInfo because of incorrect version",
50 ));
51 }
52
53 let zec_price = None;
55
56 let currency = "USD".to_string();
58
59 let last_historical_prices_fetched_at =
60 Optional::read(&mut reader, |r| r.read_u64::<LittleEndian>())?;
61 let historical_prices_retry_count = reader.read_u64::<LittleEndian>()?;
62
63 Ok(Self {
64 zec_price,
65 currency,
66 last_historical_prices_fetched_at,
67 historical_prices_retry_count,
68 })
69 }
70
71 pub fn write<W: WriteBytesExt>(&self, mut writer: W) -> io::Result<()> {
72 writer.write_u64::<LittleEndian>(Self::serialized_version())?;
73
74 Optional::write(
76 &mut writer,
77 self.last_historical_prices_fetched_at,
78 |w, t| w.write_u64::<LittleEndian>(t),
79 )?;
80 writer.write_u64::<LittleEndian>(self.historical_prices_retry_count)?;
81
82 Ok(())
83 }
84}
85
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87pub enum MemoDownloadOption {
88 NoMemos = 0,
89 WalletMemos,
90 AllMemos,
91}
92
93impl fmt::Display for MemoDownloadOption {
94 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
95 match self {
96 MemoDownloadOption::NoMemos => write!(f, "NoMemos"),
97 MemoDownloadOption::WalletMemos => write!(f, "WalletMemos"),
98 MemoDownloadOption::AllMemos => write!(f, "AllMemos"),
99 }
100 }
101}
102
103#[derive(Debug, Clone, Copy)]
104pub struct WalletOptions {
105 pub(crate) download_memos: MemoDownloadOption,
106 pub(crate) spam_threshold: i64,
107}
108
109impl Default for WalletOptions {
110 fn default() -> Self {
111 WalletOptions {
112 download_memos: MemoDownloadOption::WalletMemos,
113 spam_threshold: -1,
114 }
115 }
116}
117
118impl WalletOptions {
119 pub fn serialized_version() -> u64 {
120 2
121 }
122
123 pub fn read<R: ReadBytesExt>(mut reader: R) -> io::Result<Self> {
124 let version = reader.read_u64::<LittleEndian>()?;
125
126 let download_memos = match reader.read_u8()? {
127 0 => MemoDownloadOption::NoMemos,
128 1 => MemoDownloadOption::WalletMemos,
129 2 => MemoDownloadOption::AllMemos,
130 v => {
131 return Err(io::Error::new(
132 io::ErrorKind::InvalidData,
133 format!("Bad download option {}", v),
134 ));
135 }
136 };
137
138 let spam_threshold = if version <= 1 {
139 -1
140 } else {
141 reader.read_i64::<LittleEndian>()?
142 };
143
144 Ok(Self {
145 download_memos,
146 spam_threshold,
147 })
148 }
149
150 pub fn write<W: WriteBytesExt>(&self, mut writer: W) -> io::Result<()> {
151 writer.write_u64::<LittleEndian>(Self::serialized_version())?;
153
154 writer.write_u8(self.download_memos as u8)?;
155
156 writer.write_i64::<LittleEndian>(self.spam_threshold)
157 }
158}
159
160impl fmt::Display for WalletOptions {
161 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
162 write!(
163 f,
164 "download_memos: {}, spam_threshold: {}",
165 self.download_memos, self.spam_threshold
166 )
167 }
168}
169
170#[derive(Debug, Clone, Copy)]
171pub enum ChainType {
172 Mainnet,
173 Testnet,
174 Regtest,
175 Unknown,
176}
177
178impl From<String> for ChainType {
179 fn from(s: String) -> Self {
180 match s.as_str() {
181 "mainnet" | "main" => ChainType::Mainnet,
182 "testnet" | "test" => ChainType::Testnet,
183 "regtest" => ChainType::Regtest,
184 _ => ChainType::Unknown,
185 }
186 }
187}
188
189impl Display for ChainType {
190 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
191 match self {
192 ChainType::Mainnet => write!(f, "Mainnet"),
193 ChainType::Testnet => write!(f, "Testnet"),
194 ChainType::Regtest => write!(f, "Regtest"),
195 ChainType::Unknown => write!(f, "Unkown"),
196 }
197 }
198}