Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix abci::Code parser to accept a string or an int #13

Merged
merged 1 commit into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 39 additions & 11 deletions src/abci/code.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
use serde::de::{Deserialize, Deserializer, Visitor};
use serde::{Serialize, Serializer};
use std::fmt;

/// ABCI application response codes.
///
Expand Down Expand Up @@ -54,18 +56,44 @@ impl From<Code> for u32 {
}
}

impl<'de> Deserialize<'de> for Code {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
Ok(Code::from(
String::deserialize(deserializer)?
.parse::<u32>()
.map_err(|e| D::Error::custom(format!("{}", e)))?,
))
}
}

impl Serialize for Code {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
self.value().serialize(serializer)
}
}

impl<'de> Deserialize<'de> for Code {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct CodeVisitor;

impl<'de> Visitor<'de> for CodeVisitor {
type Value = Code;

fn expecting(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.write_str("integer or string")
}

fn visit_u64<E>(self, val: u64) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
Ok(Code::from(val as u32))
}

fn visit_str<E>(self, val: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
match val.parse::<u32>() {
Ok(val) => self.visit_u32(val),
Err(_) => Err(E::custom("failed to parse integer")),
}
}
}

deserializer.deserialize_any(CodeVisitor)
}
}
18 changes: 18 additions & 0 deletions tests/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#[cfg(feature = "rpc")]
mod endpoints {
use std::{fs, path::PathBuf};
use tendermint::abci::Code;
use tendermint::rpc::{self, endpoint, Response};

const EXAMPLE_APP: &str = "GaiaApp";
Expand Down Expand Up @@ -122,6 +123,23 @@ mod endpoints {
))
.unwrap();

assert_eq!(response.code, Code::Ok);

assert_eq!(
&response.hash.to_string(),
"0D33F2F03A5234F38706E43004489E061AC40A2E"
);
}

#[test]
fn broadcast_tx_sync_int() {
let response = endpoint::broadcast::tx_sync::Response::from_json(&read_json_fixture(
"broadcast_tx_sync_int",
))
.unwrap();

assert_eq!(response.code, Code::Ok);

assert_eq!(
&response.hash.to_string(),
"0D33F2F03A5234F38706E43004489E061AC40A2E"
Expand Down
10 changes: 10 additions & 0 deletions tests/support/rpc/broadcast_tx_sync_int.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"jsonrpc": "2.0",
"id": "",
"result": {
"code": 0,
"data": "",
"log": "",
"hash": "0D33F2F03A5234F38706E43004489E061AC40A2E"
}
}