Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Commit

Permalink
Delete .rustfmt.toml
Browse files Browse the repository at this point in the history
In commit `14429b13` I introduced a new config file for rustfmt but
did not delete the hidden `.rustfmt.toml` so the new file had no effect.

- Delete the old confid `.rustfmt.toml`

- Make `rustfmt.toml` mirror the current one in `rust-bitcoin` with the
  exception of `edition = 2018`

- Run `cargo +nightly fmt`

Fix: #116
  • Loading branch information
tcharding committed Jun 12, 2024
1 parent 1a4278c commit 0a3e922
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 255 deletions.
1 change: 0 additions & 1 deletion .rustfmt.toml

This file was deleted.

7 changes: 2 additions & 5 deletions fuzz/fuzz_targets/minreq_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ fn do_test(data: &[u8]) {
{
use std::io;

use jsonrpc::minreq_http::MinreqHttpTransport;
use jsonrpc::minreq_http::FUZZ_TCP_SOCK;
use jsonrpc::minreq_http::{MinreqHttpTransport, FUZZ_TCP_SOCK};
use jsonrpc::Client;

*FUZZ_TCP_SOCK.lock().unwrap() = Some(io::Cursor::new(data.to_vec()));
Expand Down Expand Up @@ -56,7 +55,5 @@ mod tests {
}

#[test]
fn duplicate_crash() {
super::do_test(&extend_vec_from_hex("00"));
}
fn duplicate_crash() { super::do_test(&extend_vec_from_hex("00")); }
}
7 changes: 2 additions & 5 deletions fuzz/fuzz_targets/simple_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ fn do_test(data: &[u8]) {
{
use std::io;

use jsonrpc::simple_http::SimpleHttpTransport;
use jsonrpc::simple_http::FUZZ_TCP_SOCK;
use jsonrpc::simple_http::{SimpleHttpTransport, FUZZ_TCP_SOCK};
use jsonrpc::Client;

*FUZZ_TCP_SOCK.lock().unwrap() = Some(io::Cursor::new(data.to_vec()));
Expand Down Expand Up @@ -56,7 +55,5 @@ mod tests {
}

#[test]
fn duplicate_crash() {
super::do_test(&extend_vec_from_hex("00"));
}
fn duplicate_crash() { super::do_test(&extend_vec_from_hex("00")); }
}
1 change: 0 additions & 1 deletion integration_test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use std::time::Duration;
use std::{fs, panic};

use backtrace::Backtrace;

use jsonrpc::http::minreq_http;
use jsonrpc::{Client, Request};
use serde_json::json;
Expand Down
4 changes: 1 addition & 3 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
ignore = []

hard_tabs = false
tab_spaces = 4
newline_style = "Auto"
Expand Down Expand Up @@ -50,7 +49,7 @@ enum_discrim_align_threshold = 0
match_arm_blocks = false # Default true
match_arm_leading_pipes = "Never"
force_multiline_blocks = false
fn_args_layout = "Tall"
fn_params_layout = "Tall"
brace_style = "SameLineWhere"
control_brace_style = "AlwaysSameLine"
trailing_semicolon = true
Expand All @@ -68,7 +67,6 @@ use_field_init_shorthand = false
force_explicit_abi = true
condense_wildcard_suffixes = false
color = "Auto"
required_version = "1.5.1"
unstable_features = false
disable_all_formatting = false
skip_children = false
Expand Down
32 changes: 8 additions & 24 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ pub struct Client {
impl Client {
/// Creates a new client with the given transport.
pub fn with_transport<T: Transport>(transport: T) -> Client {
Client {
transport: Box::new(transport),
nonce: atomic::AtomicUsize::new(1),
}
Client { transport: Box::new(transport), nonce: atomic::AtomicUsize::new(1) }
}

/// Builds a request.
Expand All @@ -51,12 +48,7 @@ impl Client {
/// [`crate::arg`] or [`crate::try_arg`].
pub fn build_request<'a>(&self, method: &'a str, params: Option<&'a RawValue>) -> Request<'a> {
let nonce = self.nonce.fetch_add(1, atomic::Ordering::Relaxed);
Request {
method,
params,
id: serde_json::Value::from(nonce),
jsonrpc: Some("2.0"),
}
Request { method, params, id: serde_json::Value::from(nonce), jsonrpc: Some("2.0") }
}

/// Sends a request to a client.
Expand Down Expand Up @@ -141,9 +133,7 @@ impl fmt::Debug for crate::Client {
}

impl<T: Transport> From<T> for Client {
fn from(t: T) -> Client {
Client::with_transport(t)
}
fn from(t: T) -> Client { Client::with_transport(t) }
}

/// Newtype around `Value` which allows hashing for use as hashmap keys,
Expand Down Expand Up @@ -200,24 +190,18 @@ impl<'a> Hash for HashableValue<'a> {

#[cfg(test)]
mod tests {
use super::*;

use std::borrow::Cow;
use std::collections::HashSet;
use std::str::FromStr;
use std::sync;

use super::*;

struct DummyTransport;
impl Transport for DummyTransport {
fn send_request(&self, _: Request) -> Result<Response, Error> {
Err(Error::NonceMismatch)
}
fn send_batch(&self, _: &[Request]) -> Result<Vec<Response>, Error> {
Ok(vec![])
}
fn fmt_target(&self, _: &mut fmt::Formatter) -> fmt::Result {
Ok(())
}
fn send_request(&self, _: Request) -> Result<Response, Error> { Err(Error::NonceMismatch) }
fn send_batch(&self, _: &[Request]) -> Result<Vec<Response>, Error> { Ok(vec![]) }
fn fmt_target(&self, _: &mut fmt::Formatter) -> fmt::Result { Ok(()) }
}

#[test]
Expand Down
54 changes: 16 additions & 38 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,11 @@ pub enum Error {
}

impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Error {
Error::Json(e)
}
fn from(e: serde_json::Error) -> Error { Error::Json(e) }
}

impl From<RpcError> for Error {
fn from(e: RpcError) -> Error {
Error::Rpc(e)
}
fn from(e: RpcError) -> Error { Error::Rpc(e) }
}

impl fmt::Display for Error {
Expand Down Expand Up @@ -136,31 +132,16 @@ pub fn standard_error(
data: Option<Box<serde_json::value::RawValue>>,
) -> RpcError {
match code {
StandardError::ParseError => RpcError {
code: -32700,
message: "Parse error".to_string(),
data,
},
StandardError::InvalidRequest => RpcError {
code: -32600,
message: "Invalid Request".to_string(),
data,
},
StandardError::MethodNotFound => RpcError {
code: -32601,
message: "Method not found".to_string(),
data,
},
StandardError::InvalidParams => RpcError {
code: -32602,
message: "Invalid params".to_string(),
data,
},
StandardError::InternalError => RpcError {
code: -32603,
message: "Internal error".to_string(),
data,
},
StandardError::ParseError =>
RpcError { code: -32700, message: "Parse error".to_string(), data },
StandardError::InvalidRequest =>
RpcError { code: -32600, message: "Invalid Request".to_string(), data },
StandardError::MethodNotFound =>
RpcError { code: -32601, message: "Method not found".to_string(), data },
StandardError::InvalidParams =>
RpcError { code: -32602, message: "Invalid params".to_string(), data },
StandardError::InternalError =>
RpcError { code: -32603, message: "Internal error".to_string(), data },
}
}

Expand All @@ -179,22 +160,19 @@ pub fn result_to_response(
id,
jsonrpc: Some(String::from("2.0")),
},
Err(err) => Response {
result: None,
error: Some(err),
id,
jsonrpc: Some(String::from("2.0")),
},
Err(err) =>
Response { result: None, error: Some(err), id, jsonrpc: Some(String::from("2.0")) },
}
}

#[cfg(test)]
mod tests {
use serde_json;

use super::StandardError::{
InternalError, InvalidParams, InvalidRequest, MethodNotFound, ParseError,
};
use super::{result_to_response, standard_error};
use serde_json;

#[test]
fn test_parse_error() {
Expand Down
47 changes: 12 additions & 35 deletions src/http/minreq_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,10 @@ impl Default for MinreqHttpTransport {

impl MinreqHttpTransport {
/// Constructs a new [`MinreqHttpTransport`] with default parameters.
pub fn new() -> Self {
MinreqHttpTransport::default()
}
pub fn new() -> Self { MinreqHttpTransport::default() }

/// Returns a builder for [`MinreqHttpTransport`].
pub fn builder() -> Builder {
Builder::new()
}
pub fn builder() -> Builder { Builder::new() }

fn request<R>(&self, req: impl serde::Serialize) -> Result<R, Error>
where
Expand All @@ -72,16 +68,15 @@ impl MinreqHttpTransport {
let resp = req.send()?;
match resp.json() {
Ok(json) => Ok(json),
Err(minreq_err) => {
Err(minreq_err) =>
if resp.status_code != 200 {
Err(Error::Http(HttpError {
status_code: resp.status_code,
body: resp.as_str().unwrap_or("").to_string(),
}))
} else {
Err(Error::Minreq(minreq_err))
}
}
},
}
}
}
Expand All @@ -95,9 +90,7 @@ impl Transport for MinreqHttpTransport {
Ok(self.request(reqs)?)
}

fn fmt_target(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.url)
}
fn fmt_target(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}", self.url) }
}

/// Builder for simple bitcoind [`MinreqHttpTransport`].
Expand All @@ -108,11 +101,7 @@ pub struct Builder {

impl Builder {
/// Constructs a new [`Builder`] with default configuration and the URL to use.
pub fn new() -> Builder {
Builder {
tp: MinreqHttpTransport::new(),
}
}
pub fn new() -> Builder { Builder { tp: MinreqHttpTransport::new() } }

/// Sets the timeout after which requests will abort if they aren't finished.
pub fn timeout(mut self, timeout: Duration) -> Self {
Expand Down Expand Up @@ -160,15 +149,11 @@ impl Builder {
}

/// Builds the final [`MinreqHttpTransport`].
pub fn build(self) -> MinreqHttpTransport {
self.tp
}
pub fn build(self) -> MinreqHttpTransport { self.tp }
}

impl Default for Builder {
fn default() -> Self {
Builder::new()
}
fn default() -> Self { Builder::new() }
}

/// An HTTP error.
Expand Down Expand Up @@ -225,15 +210,11 @@ impl error::Error for Error {
}

impl From<serde_json::Error> for Error {
fn from(e: serde_json::Error) -> Self {
Error::Json(e)
}
fn from(e: serde_json::Error) -> Self { Error::Json(e) }
}

impl From<minreq::Error> for Error {
fn from(e: minreq::Error) -> Self {
Error::Minreq(e)
}
fn from(e: minreq::Error) -> Self { Error::Minreq(e) }
}

impl From<Error> for crate::Error {
Expand Down Expand Up @@ -266,12 +247,8 @@ mod impls {
}
}
impl Write for TcpStream {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
io::sink().write(buf)
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { io::sink().write(buf) }
fn flush(&mut self) -> io::Result<()> { Ok(()) }
}
}

Expand Down
Loading

0 comments on commit 0a3e922

Please sign in to comment.