use std::convert::From;
use std::convert::TryFrom;
use std::convert::TryInto;
+use std::io;
use std::str::FromStr;
use bitcoin::hashes::Hash;
impl TryInto<serde_json::Value> for JsonResponse {
- type Error = std::io::Error;
- fn try_into(self) -> Result<serde_json::Value, std::io::Error> { Ok(self.0) }
+ type Error = io::Error;
+ fn try_into(self) -> Result<serde_json::Value, io::Error> { Ok(self.0) }
}
-/// Conversion from `std::io::Error` into `BlockSourceError`.
-impl From<std::io::Error> for BlockSourceError {
- fn from(e: std::io::Error) -> BlockSourceError {
+/// Conversion from `io::Error` into `BlockSourceError`.
+impl From<io::Error> for BlockSourceError {
+ fn from(e: io::Error) -> BlockSourceError {
match e.kind() {
- std::io::ErrorKind::InvalidData => BlockSourceError::persistent(e),
- std::io::ErrorKind::InvalidInput => BlockSourceError::persistent(e),
+ io::ErrorKind::InvalidData => BlockSourceError::persistent(e),
+ io::ErrorKind::InvalidInput => BlockSourceError::persistent(e),
_ => BlockSourceError::transient(e),
}
}
/// Parses binary data as a block.
impl TryInto<Block> for BinaryResponse {
- type Error = std::io::Error;
+ type Error = io::Error;
- fn try_into(self) -> std::io::Result<Block> {
+ fn try_into(self) -> io::Result<Block> {
match encode::deserialize(&self.0) {
- Err(_) => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid block data")),
+ Err(_) => return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid block data")),
Ok(block) => Ok(block),
}
}
/// Parses binary data as a block hash.
impl TryInto<BlockHash> for BinaryResponse {
- type Error = std::io::Error;
+ type Error = io::Error;
- fn try_into(self) -> std::io::Result<BlockHash> {
+ fn try_into(self) -> io::Result<BlockHash> {
BlockHash::from_slice(&self.0).map_err(|_|
- std::io::Error::new(std::io::ErrorKind::InvalidData, "bad block hash length")
+ io::Error::new(io::ErrorKind::InvalidData, "bad block hash length")
)
}
}
/// Converts a JSON value into block header data. The JSON value may be an object representing a
/// block header or an array of such objects. In the latter case, the first object is converted.
impl TryInto<BlockHeaderData> for JsonResponse {
- type Error = std::io::Error;
+ type Error = io::Error;
- fn try_into(self) -> std::io::Result<BlockHeaderData> {
+ fn try_into(self) -> io::Result<BlockHeaderData> {
let header = match self.0 {
serde_json::Value::Array(mut array) if !array.is_empty() => array.drain(..).next().unwrap(),
serde_json::Value::Object(_) => self.0,
- _ => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "unexpected JSON type")),
+ _ => return Err(io::Error::new(io::ErrorKind::InvalidData, "unexpected JSON type")),
};
if !header.is_object() {
- return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON object"));
+ return Err(io::Error::new(io::ErrorKind::InvalidData, "expected JSON object"));
}
// Add an empty previousblockhash for the genesis block.
match header.try_into() {
- Err(_) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid header data")),
+ Err(_) => Err(io::Error::new(io::ErrorKind::InvalidData, "invalid header data")),
Ok(header) => Ok(header),
}
}
/// Converts a JSON value into a block. Assumes the block is hex-encoded in a JSON string.
impl TryInto<Block> for JsonResponse {
- type Error = std::io::Error;
+ type Error = io::Error;
- fn try_into(self) -> std::io::Result<Block> {
+ fn try_into(self) -> io::Result<Block> {
match self.0.as_str() {
- None => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON string")),
+ None => Err(io::Error::new(io::ErrorKind::InvalidData, "expected JSON string")),
Some(hex_data) => match Vec::<u8>::from_hex(hex_data) {
- Err(_) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid hex data")),
+ Err(_) => Err(io::Error::new(io::ErrorKind::InvalidData, "invalid hex data")),
Ok(block_data) => match encode::deserialize(&block_data) {
- Err(_) => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid block data")),
+ Err(_) => Err(io::Error::new(io::ErrorKind::InvalidData, "invalid block data")),
Ok(block) => Ok(block),
},
},
/// Converts a JSON value into the best block hash and optional height.
impl TryInto<(BlockHash, Option<u32>)> for JsonResponse {
- type Error = std::io::Error;
+ type Error = io::Error;
- fn try_into(self) -> std::io::Result<(BlockHash, Option<u32>)> {
+ fn try_into(self) -> io::Result<(BlockHash, Option<u32>)> {
if !self.0.is_object() {
- return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON object"));
+ return Err(io::Error::new(io::ErrorKind::InvalidData, "expected JSON object"));
}
let hash = match &self.0["bestblockhash"] {
serde_json::Value::String(hex_data) => match BlockHash::from_str(&hex_data) {
- Err(_) => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid hex data")),
+ Err(_) => return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid hex data")),
Ok(block_hash) => block_hash,
},
- _ => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON string")),
+ _ => return Err(io::Error::new(io::ErrorKind::InvalidData, "expected JSON string")),
};
let height = match &self.0["blocks"] {
serde_json::Value::Null => None,
serde_json::Value::Number(height) => match height.as_u64() {
- None => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid height")),
+ None => return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid height")),
Some(height) => match height.try_into() {
- Err(_) => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid height")),
+ Err(_) => return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid height")),
Ok(height) => Some(height),
}
},
- _ => return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON number")),
+ _ => return Err(io::Error::new(io::ErrorKind::InvalidData, "expected JSON number")),
};
Ok((hash, height))
}
impl TryInto<Txid> for JsonResponse {
- type Error = std::io::Error;
- fn try_into(self) -> std::io::Result<Txid> {
- let hex_data = self.0.as_str().ok_or(Self::Error::new(std::io::ErrorKind::InvalidData, "expected JSON string" ))?;
- Txid::from_str(hex_data).map_err(|err|Self::Error::new(std::io::ErrorKind::InvalidData, err.to_string() ))
+ type Error = io::Error;
+ fn try_into(self) -> io::Result<Txid> {
+ let hex_data = self.0.as_str().ok_or(io::Error::new(io::ErrorKind::InvalidData, "expected JSON string" ))?;
+ Txid::from_str(hex_data).map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err.to_string() ))
}
}
/// Converts a JSON value into a transaction. WATCH OUT! this cannot be used for zero-input transactions
/// (e.g. createrawtransaction). See <https://github.com/rust-bitcoin/rust-bitcoincore-rpc/issues/197>
impl TryInto<Transaction> for JsonResponse {
- type Error = std::io::Error;
- fn try_into(self) -> std::io::Result<Transaction> {
+ type Error = io::Error;
+ fn try_into(self) -> io::Result<Transaction> {
let hex_tx = if self.0.is_object() {
// result is json encoded
match &self.0["hex"] {
_ => "Unknown error",
};
- return Err(std::io::Error::new(
- std::io::ErrorKind::InvalidData,
+ return Err(io::Error::new(
+ io::ErrorKind::InvalidData,
format!("transaction couldn't be signed. {}", reason),
));
} else {
// result is a complete transaction (e.g. getrawtranaction verbose)
_ => hex_data,
},
- _ => return Err(std::io::Error::new(
- std::io::ErrorKind::InvalidData,
- "expected JSON string",
- )),
+ _ => {
+ return Err(io::Error::new(
+ io::ErrorKind::InvalidData,
+ "expected JSON string",
+ ));
+ }
}
} else {
// result is plain text (e.g. getrawtransaction no verbose)
match self.0.as_str() {
Some(hex_tx) => hex_tx,
None => {
- return Err(std::io::Error::new(
- std::io::ErrorKind::InvalidData,
+ return Err(io::Error::new(
+ io::ErrorKind::InvalidData,
"expected JSON string",
- ))
+ ));
}
}
};
match Vec::<u8>::from_hex(hex_tx) {
- Err(_) => Err(std::io::Error::new(
- std::io::ErrorKind::InvalidData,
- "invalid hex data",
- )),
+ Err(_) => {
+ Err(io::Error::new(
+ io::ErrorKind::InvalidData,
+ "invalid hex data",
+ ))
+ }
Ok(tx_data) => match encode::deserialize(&tx_data) {
- Err(_) => Err(std::io::Error::new(
- std::io::ErrorKind::InvalidData,
- "invalid transaction",
- )),
+ Err(_) => {
+ Err(io::Error::new(
+ io::ErrorKind::InvalidData,
+ "invalid transaction",
+ ))
+ }
Ok(tx) => Ok(tx),
},
}
}
impl TryInto<BlockHash> for JsonResponse {
- type Error = std::io::Error;
+ type Error = io::Error;
- fn try_into(self) -> std::io::Result<BlockHash> {
+ fn try_into(self) -> io::Result<BlockHash> {
match self.0.as_str() {
- None => Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected JSON string")),
+ None => Err(io::Error::new(io::ErrorKind::InvalidData, "expected JSON string")),
Some(hex_data) if hex_data.len() != 64 =>
- Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid hash length")),
+ Err(io::Error::new(io::ErrorKind::InvalidData, "invalid hash length")),
Some(hex_data) => BlockHash::from_str(hex_data)
- .map_err(|_| std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid hex data")),
+ .map_err(|_| io::Error::new(io::ErrorKind::InvalidData, "invalid hex data")),
}
}
}
#[cfg(feature = "rest-client")]
impl TryInto<GetUtxosResponse> for JsonResponse {
- type Error = std::io::Error;
+ type Error = io::Error;
- fn try_into(self) -> std::io::Result<GetUtxosResponse> {
+ fn try_into(self) -> io::Result<GetUtxosResponse> {
+ let obj_err = || io::Error::new(io::ErrorKind::InvalidData, "expected an object");
+ let bitmap_err = || io::Error::new(io::ErrorKind::InvalidData, "missing bitmap field");
+ let bitstr_err = || io::Error::new(io::ErrorKind::InvalidData, "bitmap should be an str");
let bitmap_str =
- self.0.as_object().ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "expected an object"))?
- .get("bitmap").ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "missing bitmap field"))?
- .as_str().ok_or(std::io::Error::new(std::io::ErrorKind::InvalidData, "bitmap should be an str"))?;
+ self.0.as_object().ok_or_else(obj_err)?
+ .get("bitmap").ok_or_else(bitmap_err)?
+ .as_str().ok_or_else(bitstr_err)?;
let mut hit_bitmap_nonempty = false;
for c in bitmap_str.chars() {
if c < '0' || c > '9' {
- return Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "invalid byte"));
+ return Err(io::Error::new(io::ErrorKind::InvalidData, "invalid byte"));
}
if c > '0' { hit_bitmap_nonempty = true; }
}
let response = JsonResponse(serde_json::json!(42));
match TryInto::<BlockHeaderData>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "unexpected JSON type");
},
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!([42]));
match TryInto::<BlockHeaderData>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON object");
},
Ok(_) => panic!("Expected error"),
match TryInto::<BlockHeaderData>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "invalid header data");
},
Ok(_) => panic!("Expected error"),
match TryInto::<BlockHeaderData>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "invalid header data");
},
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!({ "result": "foo" }));
match TryInto::<Block>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON string");
},
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!("foobar"));
match TryInto::<Block>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "invalid hex data");
},
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!("abcd"));
match TryInto::<Block>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "invalid block data");
},
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!("foo"));
match TryInto::<(BlockHash, Option<u32>)>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON object");
},
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!({ "bestblockhash": 42 }));
match TryInto::<(BlockHash, Option<u32>)>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON string");
},
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!({ "bestblockhash": "foobar"} ));
match TryInto::<(BlockHash, Option<u32>)>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "invalid hex data");
},
Ok(_) => panic!("Expected error"),
}));
match TryInto::<(BlockHash, Option<u32>)>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON number");
},
Ok(_) => panic!("Expected error"),
}));
match TryInto::<(BlockHash, Option<u32>)>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "invalid height");
},
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!({ "result": "foo" }));
match TryInto::<Txid>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON string");
}
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!("foobar"));
match TryInto::<Txid>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "bad hex string length 6 (expected 64)");
}
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!("abcd"));
match TryInto::<Txid>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "bad hex string length 4 (expected 64)");
}
Ok(_) => panic!("Expected error"),
{"error": "", "id": "770", "result": "7934f775149929a8b742487129a7c3a535dfb612f0b726cc67bc10bc2628f906"}
);
- let r: std::io::Result<Txid> = JsonResponse(rpc_response.get_mut("result").unwrap().take())
+ let r: io::Result<Txid> = JsonResponse(rpc_response.get_mut("result").unwrap().take())
.try_into();
assert_eq!(
r.unwrap().to_string(),
let response = JsonResponse(serde_json::json!("foobar"));
match TryInto::<Transaction>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "invalid hex data");
}
Ok(_) => panic!("Expected error"),
let response = JsonResponse(Value::Number(Number::from_f64(1.0).unwrap()));
match TryInto::<Transaction>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "expected JSON string");
}
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!("abcd"));
match TryInto::<Transaction>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(e.get_ref().unwrap().to_string(), "invalid transaction");
}
Ok(_) => panic!("Expected error"),
let response = JsonResponse(serde_json::json!({ "error": "foo" }));
match TryInto::<Transaction>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert_eq!(
e.get_ref().unwrap().to_string(),
"expected JSON string"
let response = JsonResponse(serde_json::json!({ "hex": "foo", "complete": false }));
match TryInto::<Transaction>::try_into(response) {
Err(e) => {
- assert_eq!(e.kind(), std::io::ErrorKind::InvalidData);
+ assert_eq!(e.kind(), io::ErrorKind::InvalidData);
assert!(
e.get_ref().unwrap().to_string().contains(
"transaction couldn't be signed")