Skip to content

Commit

Permalink
ChannelId deserialization
Browse files Browse the repository at this point in the history
Signed-off-by: Lachezar Lechev <lachezar@ambire.com>
  • Loading branch information
elpiel committed Nov 25, 2022
1 parent 97467af commit 5702036
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ ethereum-types = "0.13"
parse-display = "0.5"
# CID & multihash / multibase
cid = "0.8"
hex = { version = "0.4", features = ["serde"] }
hex = "0.4"
merkletree = "0.10"
tiny-keccak = { version = "2", features = ["keccak"] }
url = { version = "2", features = ["serde"] }
Expand Down
15 changes: 12 additions & 3 deletions primitives/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use hex::{FromHex, FromHexError};

use crate::{Address, ToHex, Validator, ValidatorId};

#[derive(Deserialize, PartialEq, Eq, Copy, Clone, Hash)]
#[serde(transparent)]
pub struct ChannelId(#[serde(deserialize_with = "deserialize_channel_id")] [u8; 32]);
#[derive(PartialEq, Eq, Copy, Clone, Hash)]
pub struct ChannelId([u8; 32]);

impl ChannelId {
pub fn as_bytes(&self) -> &[u8; 32] {
Expand All @@ -32,6 +31,15 @@ where
validate_channel_id(&channel_id).map_err(serde::de::Error::custom)
}

impl<'de> Deserialize<'de> for ChannelId {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
deserialize_channel_id(deserializer).map(ChannelId)
}
}

impl Serialize for ChannelId {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down Expand Up @@ -334,6 +342,7 @@ mod postgres {
#[cfg(test)]
mod test {
use crate::{channel::Nonce, postgres::POSTGRES_POOL};

#[tokio::test]
async fn nonce_to_from_sql() {
let client = POSTGRES_POOL.get().await.unwrap();
Expand Down

0 comments on commit 5702036

Please sign in to comment.