Skip to content

Commit

Permalink
core::address > impl Deserialize for Address
Browse files Browse the repository at this point in the history
  • Loading branch information
canewsin committed Jan 23, 2024
1 parent 82f3423 commit d0998f2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ bincode = "1.3.3"
bitcoin = "0.29.2"
clap = "4.0.19"
diff = "0.1.12"
derive_more = "0.99"
futures = "0.3.21"
itertools = "0.10.3"
lazy_static = "1.4.0"
Expand Down
36 changes: 23 additions & 13 deletions src/core/address.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use log::*;
use serde::{Serialize, Serializer};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use sha1::Sha1;
use sha2::{Digest, Sha256};
use std::{fmt::Display, str::FromStr};
Expand Down Expand Up @@ -52,6 +52,16 @@ impl Serialize for Address {
}
}

impl<'de> Deserialize<'de> for Address {
fn deserialize<D>(deserializer: D) -> Result<Address, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Address::from_str(&s).map_err(serde::de::Error::custom)
}
}

impl FromStr for Address {
type Err = Error;

Expand Down Expand Up @@ -132,16 +142,16 @@ mod tests {
assert_eq!(&address_hash, "1HELLo...2Ri9d");
}

// #[test]
// fn test_deserialization() {
// let result = serde_json::from_str("\"1HELLoE3sFD9569CLCbHEAVqvqV7U2Ri9d\"");
// assert_eq!(result.is_ok(), true, "Encountered error: {:?}", result);
// let address: Address = result.unwrap();
// assert_eq!(
// address,
// Address {
// address: String::from(ADDR)
// }
// );
// }
#[test]
fn test_deserialization() {
let result = serde_json::from_str("\"1HELLoE3sFD9569CLCbHEAVqvqV7U2Ri9d\"");
assert_eq!(result.is_ok(), true, "Encountered error: {:?}", result);
let address: Address = result.unwrap();
assert_eq!(
address,
Address {
address: String::from(ADDR)
}
);
}
}
4 changes: 2 additions & 2 deletions src/core/error.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// use actix_http::error::ResponseError;
// use derive_more::Display;
use derive_more::Display;

#[derive(Debug)]
#[derive(Debug, Display)]
pub enum Error {
AddressError(String),
SiteNotFound,
Expand Down

0 comments on commit d0998f2

Please sign in to comment.