From f7cf90bcaad9f8f9f9b7a34ffb2eaaac3a8262cc Mon Sep 17 00:00:00 2001 From: Denys Zadorozhnyi Date: Tue, 14 Mar 2023 13:17:01 +0200 Subject: [PATCH] parse change address in wallet status as None if empty string; --- Cargo.toml | 1 + src/local_config.rs | 5 +---- src/node_interface.rs | 26 ++++++++++++++++++++++---- src/scanning.rs | 2 +- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index da45566..bb21231 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,3 +24,4 @@ thiserror = "1.0.22" blake2b_simd = "0.5.11" base16 = "0.2.1" yaml-rust = "0.4.4" +serde_with = { version = "1.14", features = ["json"] } diff --git a/src/local_config.rs b/src/local_config.rs index 5715377..b457ba9 100644 --- a/src/local_config.rs +++ b/src/local_config.rs @@ -30,10 +30,7 @@ pub fn acquire_node_interface_from_local_config() -> NodeInterface { } // Error checking reading the local node interface yaml if let Err(e) = new_interface_from_local_config() { - println!( - "Could not parse local `node-interface.yaml` file.\nError: {:?}", - e - ); + println!("Could not parse local `node-interface.yaml` file.\nError: {e:?}"); std::process::exit(0); } // Create `NodeInterface` diff --git a/src/node_interface.rs b/src/node_interface.rs index 9b56c71..ecf2cb9 100644 --- a/src/node_interface.rs +++ b/src/node_interface.rs @@ -1,9 +1,11 @@ -/// The `NodeInterface` struct is defined which allows for interacting with an -/// Ergo Node via Rust. +//! The `NodeInterface` struct is defined which allows for interacting with an Ergo Node via Rust. + use crate::{BlockHeight, NanoErg, P2PKAddressString, P2SAddressString}; use ergo_lib::ergotree_ir::chain::ergo_box::ErgoBox; use reqwest::Url; use serde_json::from_str; +use serde_with::serde_as; +use serde_with::NoneAsEmptyString; use thiserror::Error; pub type Result = std::result::Result; @@ -105,7 +107,7 @@ impl NodeInterface { let mut n = 0; for address in &address_list { n += 1; - println!("{}. {}", n, address); + println!("{n}. {address}"); } println!("Which address would you like to select?"); let mut input = String::new(); @@ -365,6 +367,7 @@ impl NodeInterface { } } +#[serde_as] #[derive(serde::Deserialize, serde::Serialize)] pub struct WalletStatus { #[serde(rename = "isInitialized")] @@ -372,6 +375,7 @@ pub struct WalletStatus { #[serde(rename = "isUnlocked")] pub unlocked: bool, #[serde(rename = "changeAddress")] + #[serde_as(as = "NoneAsEmptyString")] pub change_address: Option, #[serde(rename = "walletHeight")] pub height: BlockHeight, @@ -384,7 +388,7 @@ mod tests { use super::*; #[test] - fn test_name() { + fn test_parsing_wallet_status_unlocked() { let node_response_json_str = r#"{ "isInitialized": true, "isUnlocked": true, @@ -395,4 +399,18 @@ mod tests { let t: WalletStatus = serde_json::from_str(node_response_json_str).unwrap(); assert_eq!(t.height, 251965); } + + #[test] + fn test_parsing_wallet_status_locked() { + let node_response_json_str = r#"{ + "isInitialized": true, + "isUnlocked": false, + "changeAddress": "", + "walletHeight": 251965, + "error": "" + }"#; + let t: WalletStatus = serde_json::from_str(node_response_json_str).unwrap(); + assert_eq!(t.change_address, None); + assert_eq!(t.height, 251965); + } } diff --git a/src/scanning.rs b/src/scanning.rs index ae73b91..7f1c166 100644 --- a/src/scanning.rs +++ b/src/scanning.rs @@ -157,7 +157,7 @@ impl NodeInterface { if let Ok(ergo_box) = res_ergo_box { box_list.push(ergo_box); } else if let Err(e) = res_ergo_box { - let mess = format!("Box Json: {}\nError: {:?}", box_json, e); + let mess = format!("Box Json: {box_json}\nError: {e:?}"); return Err(NodeError::FailedParsingBox(mess)); } }