mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-06-12 19:21:13 +00:00
Run cargo fmt on the whole code base (#9394)
* Run cargo fmt on the whole code base * Second run * Add CI check * Fix compilation * More unnecessary braces * Handle weights * Use --all * Use correct attributes... * Fix UI tests * AHHHHHHHHH * 🤦 * Docs * Fix compilation * 🤷 * Please stop * 🤦 x 2 * More * make rustfmt.toml consistent with polkadot Co-authored-by: André Silva <andrerfosilva@gmail.com>
This commit is contained in:
@@ -48,17 +48,15 @@
|
||||
//! assert_eq!(body.error(), &None);
|
||||
//! ```
|
||||
|
||||
use sp_std::str;
|
||||
use sp_std::prelude::Vec;
|
||||
use sp_core::{
|
||||
offchain::{
|
||||
HttpError, HttpRequestId as RequestId, HttpRequestStatus as RequestStatus, Timestamp,
|
||||
},
|
||||
RuntimeDebug,
|
||||
};
|
||||
#[cfg(not(feature = "std"))]
|
||||
use sp_std::prelude::vec;
|
||||
use sp_core::RuntimeDebug;
|
||||
use sp_core::offchain::{
|
||||
Timestamp,
|
||||
HttpRequestId as RequestId,
|
||||
HttpRequestStatus as RequestStatus,
|
||||
HttpError,
|
||||
};
|
||||
use sp_std::{prelude::Vec, str};
|
||||
|
||||
/// Request method (HTTP verb)
|
||||
#[derive(Clone, PartialEq, Eq, RuntimeDebug)]
|
||||
@@ -103,10 +101,7 @@ mod header {
|
||||
impl Header {
|
||||
/// Creates new header given it's name and value.
|
||||
pub fn new(name: &str, value: &str) -> Self {
|
||||
Header {
|
||||
name: name.as_bytes().to_vec(),
|
||||
value: value.as_bytes().to_vec(),
|
||||
}
|
||||
Header { name: name.as_bytes().to_vec(), value: value.as_bytes().to_vec() }
|
||||
}
|
||||
|
||||
/// Returns the name of this header.
|
||||
@@ -166,13 +161,7 @@ impl<'a, T> Request<'a, T> {
|
||||
pub fn post(url: &'a str, body: T) -> Self {
|
||||
let req: Request = Request::default();
|
||||
|
||||
Request {
|
||||
url,
|
||||
body,
|
||||
method: Method::Post,
|
||||
headers: req.headers,
|
||||
deadline: req.deadline,
|
||||
}
|
||||
Request { url, body, method: Method::Post, headers: req.headers, deadline: req.deadline }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -213,7 +202,7 @@ impl<'a, T: Default> Request<'a, T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, I: AsRef<[u8]>, T: IntoIterator<Item=I>> Request<'a, T> {
|
||||
impl<'a, I: AsRef<[u8]>, T: IntoIterator<Item = I>> Request<'a, T> {
|
||||
/// Send the request and return a handle.
|
||||
///
|
||||
/// Err is returned in case the deadline is reached
|
||||
@@ -222,19 +211,13 @@ impl<'a, I: AsRef<[u8]>, T: IntoIterator<Item=I>> Request<'a, T> {
|
||||
let meta = &[];
|
||||
|
||||
// start an http request.
|
||||
let id = sp_io::offchain::http_request_start(
|
||||
self.method.as_ref(),
|
||||
self.url,
|
||||
meta,
|
||||
).map_err(|_| HttpError::IoError)?;
|
||||
let id = sp_io::offchain::http_request_start(self.method.as_ref(), self.url, meta)
|
||||
.map_err(|_| HttpError::IoError)?;
|
||||
|
||||
// add custom headers
|
||||
for header in &self.headers {
|
||||
sp_io::offchain::http_request_add_header(
|
||||
id,
|
||||
header.name(),
|
||||
header.value(),
|
||||
).map_err(|_| HttpError::IoError)?
|
||||
sp_io::offchain::http_request_add_header(id, header.name(), header.value())
|
||||
.map_err(|_| HttpError::IoError)?
|
||||
}
|
||||
|
||||
// write body
|
||||
@@ -245,9 +228,7 @@ impl<'a, I: AsRef<[u8]>, T: IntoIterator<Item=I>> Request<'a, T> {
|
||||
// finalize the request
|
||||
sp_io::offchain::http_request_write_body(id, &[], self.deadline)?;
|
||||
|
||||
Ok(PendingRequest {
|
||||
id,
|
||||
})
|
||||
Ok(PendingRequest { id })
|
||||
}
|
||||
}
|
||||
|
||||
@@ -285,8 +266,13 @@ impl PendingRequest {
|
||||
|
||||
/// Attempts to wait for the request to finish,
|
||||
/// but will return `Err` in case the deadline is reached.
|
||||
pub fn try_wait(self, deadline: impl Into<Option<Timestamp>>) -> Result<HttpResult, PendingRequest> {
|
||||
Self::try_wait_all(vec![self], deadline).pop().expect("One request passed, one status received; qed")
|
||||
pub fn try_wait(
|
||||
self,
|
||||
deadline: impl Into<Option<Timestamp>>,
|
||||
) -> Result<HttpResult, PendingRequest> {
|
||||
Self::try_wait_all(vec![self], deadline)
|
||||
.pop()
|
||||
.expect("One request passed, one status received; qed")
|
||||
}
|
||||
|
||||
/// Wait for all provided requests.
|
||||
@@ -305,7 +291,7 @@ impl PendingRequest {
|
||||
/// Requests that are complete will resolve to an `Ok` others will return a `DeadlineReached` error.
|
||||
pub fn try_wait_all(
|
||||
requests: Vec<PendingRequest>,
|
||||
deadline: impl Into<Option<Timestamp>>
|
||||
deadline: impl Into<Option<Timestamp>>,
|
||||
) -> Vec<Result<HttpResult, PendingRequest>> {
|
||||
let ids = requests.iter().map(|r| r.id).collect::<Vec<_>>();
|
||||
let statuses = sp_io::offchain::http_response_wait(&ids, deadline.into());
|
||||
@@ -336,19 +322,13 @@ pub struct Response {
|
||||
|
||||
impl Response {
|
||||
fn new(id: RequestId, code: u16) -> Self {
|
||||
Self {
|
||||
id,
|
||||
code,
|
||||
headers: None,
|
||||
}
|
||||
Self { id, code, headers: None }
|
||||
}
|
||||
|
||||
/// Retrieve the headers for this response.
|
||||
pub fn headers(&mut self) -> &Headers {
|
||||
if self.headers.is_none() {
|
||||
self.headers = Some(
|
||||
Headers { raw: sp_io::offchain::http_response_headers(self.id) },
|
||||
);
|
||||
self.headers = Some(Headers { raw: sp_io::offchain::http_response_headers(self.id) });
|
||||
}
|
||||
self.headers.as_ref().expect("Headers were just set; qed")
|
||||
}
|
||||
@@ -363,7 +343,7 @@ impl Response {
|
||||
///
|
||||
/// Note that reading the body may return `None` in following cases:
|
||||
/// 1. Either the deadline you've set is reached (check via `#error`;
|
||||
/// In such case you can resume the reader by setting a new deadline)
|
||||
/// In such case you can resume the reader by setting a new deadline)
|
||||
/// 2. Or because of IOError. In such case the reader is not resumable and will keep
|
||||
/// returning `None`.
|
||||
/// 3. The body has been returned. The reader will keep returning `None`.
|
||||
@@ -423,32 +403,28 @@ impl Iterator for ResponseBody {
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.error.is_some() {
|
||||
return None;
|
||||
return None
|
||||
}
|
||||
|
||||
if self.filled_up_to.is_none() {
|
||||
let result = sp_io::offchain::http_response_read_body(
|
||||
self.id,
|
||||
&mut self.buffer,
|
||||
self.deadline);
|
||||
let result =
|
||||
sp_io::offchain::http_response_read_body(self.id, &mut self.buffer, self.deadline);
|
||||
match result {
|
||||
Err(e) => {
|
||||
self.error = Some(e);
|
||||
return None;
|
||||
}
|
||||
Ok(0) => {
|
||||
return None;
|
||||
}
|
||||
return None
|
||||
},
|
||||
Ok(0) => return None,
|
||||
Ok(size) => {
|
||||
self.position = 0;
|
||||
self.filled_up_to = Some(size as usize);
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
if Some(self.position) == self.filled_up_to {
|
||||
self.filled_up_to = None;
|
||||
return self.next();
|
||||
return self.next()
|
||||
}
|
||||
|
||||
let result = self.buffer[self.position];
|
||||
@@ -508,7 +484,8 @@ impl<'a> HeadersIterator<'a> {
|
||||
///
|
||||
/// Note that you have to call `next` prior to calling this
|
||||
pub fn current(&self) -> Option<(&str, &str)> {
|
||||
self.collection.get(self.index?)
|
||||
self.collection
|
||||
.get(self.index?)
|
||||
.map(|val| (str::from_utf8(&val.0).unwrap_or(""), str::from_utf8(&val.1).unwrap_or("")))
|
||||
}
|
||||
}
|
||||
@@ -516,11 +493,8 @@ impl<'a> HeadersIterator<'a> {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use sp_core::offchain::{testing, OffchainWorkerExt};
|
||||
use sp_io::TestExternalities;
|
||||
use sp_core::offchain::{
|
||||
OffchainWorkerExt,
|
||||
testing,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn should_send_a_basic_request_and_get_response() {
|
||||
@@ -530,10 +504,7 @@ mod tests {
|
||||
|
||||
t.execute_with(|| {
|
||||
let request: Request = Request::get("http://localhost:1234");
|
||||
let pending = request
|
||||
.add_header("X-Auth", "hunter2")
|
||||
.send()
|
||||
.unwrap();
|
||||
let pending = request.add_header("X-Auth", "hunter2").send().unwrap();
|
||||
// make sure it's sent correctly
|
||||
state.write().fulfill_pending_request(
|
||||
0,
|
||||
|
||||
Reference in New Issue
Block a user