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:
Bastian Köcher
2021-07-21 16:32:32 +02:00
committed by GitHub
parent d451c38c1c
commit 7b56ab15b4
1010 changed files with 53339 additions and 51208 deletions
@@ -16,14 +16,14 @@
// limitations under the License.
use codec::Encode;
use proc_macro2::{Span, TokenStream};
use quote::quote;
use syn::{
Expr, ExprLit, FieldValue, ItemConst, Lit,
parse::{Result, Error},
parse::{Error, Result},
parse_macro_input,
spanned::Spanned as _,
Expr, ExprLit, FieldValue, ItemConst, Lit,
};
use quote::quote;
use proc_macro2::{TokenStream, Span};
/// This macro accepts a `const` item that has a struct initializer expression of `RuntimeVersion`-like type.
/// The macro will pass through this declaration and append an item declaration that will
@@ -78,12 +78,8 @@ impl ParseRuntimeVersion {
fn parse_expr(init_expr: &Expr) -> Result<ParseRuntimeVersion> {
let init_expr = match init_expr {
Expr::Struct(ref e) => e,
_ => {
return Err(Error::new(
init_expr.span(),
"expected a struct initializer expression",
));
}
_ =>
return Err(Error::new(init_expr.span(), "expected a struct initializer expression")),
};
let mut parsed = ParseRuntimeVersion::default();
@@ -96,12 +92,8 @@ impl ParseRuntimeVersion {
fn parse_field_value(&mut self, field_value: &FieldValue) -> Result<()> {
let field_name = match field_value.member {
syn::Member::Named(ref ident) => ident,
syn::Member::Unnamed(_) => {
return Err(Error::new(
field_value.span(),
"only named members must be used",
));
}
syn::Member::Unnamed(_) =>
return Err(Error::new(field_value.span(), "only named members must be used")),
};
fn parse_once<T>(
@@ -110,10 +102,7 @@ impl ParseRuntimeVersion {
parser: impl FnOnce(&Expr) -> Result<T>,
) -> Result<()> {
if value.is_some() {
return Err(Error::new(
field.span(),
"field is already initialized before",
));
return Err(Error::new(field.span(), "field is already initialized before"))
} else {
*value = Some(parser(&field.expr)?);
Ok(())
@@ -125,21 +114,13 @@ impl ParseRuntimeVersion {
} else if field_name == "impl_name" {
parse_once(&mut self.impl_name, field_value, Self::parse_str_literal)?;
} else if field_name == "authoring_version" {
parse_once(
&mut self.authoring_version,
field_value,
Self::parse_num_literal,
)?;
parse_once(&mut self.authoring_version, field_value, Self::parse_num_literal)?;
} else if field_name == "spec_version" {
parse_once(&mut self.spec_version, field_value, Self::parse_num_literal)?;
} else if field_name == "impl_version" {
parse_once(&mut self.impl_version, field_value, Self::parse_num_literal)?;
} else if field_name == "transaction_version" {
parse_once(
&mut self.transaction_version,
field_value,
Self::parse_num_literal,
)?;
parse_once(&mut self.transaction_version, field_value, Self::parse_num_literal)?;
} else if field_name == "apis" {
// Intentionally ignored
//
@@ -147,7 +128,7 @@ impl ParseRuntimeVersion {
// the "runtime_version" custom section. `impl_runtime_apis` is responsible for generating
// a custom section with the supported runtime apis descriptor.
} else {
return Err(Error::new(field_name.span(), "unknown field"));
return Err(Error::new(field_name.span(), "unknown field"))
}
Ok(())
@@ -155,16 +136,12 @@ impl ParseRuntimeVersion {
fn parse_num_literal(expr: &Expr) -> Result<u32> {
let lit = match *expr {
Expr::Lit(ExprLit {
lit: Lit::Int(ref lit),
..
}) => lit,
_ => {
Expr::Lit(ExprLit { lit: Lit::Int(ref lit), .. }) => lit,
_ =>
return Err(Error::new(
expr.span(),
"only numeric literals (e.g. `10`) are supported here",
));
}
)),
};
lit.base10_parse::<u32>()
}
@@ -172,44 +149,28 @@ impl ParseRuntimeVersion {
fn parse_str_literal(expr: &Expr) -> Result<String> {
let mac = match *expr {
Expr::Macro(syn::ExprMacro { ref mac, .. }) => mac,
_ => {
return Err(Error::new(
expr.span(),
"a macro expression is expected here",
));
}
_ => return Err(Error::new(expr.span(), "a macro expression is expected here")),
};
let lit: ExprLit = mac.parse_body().map_err(|e| {
Error::new(
e.span(),
format!(
"a single literal argument is expected, but parsing is failed: {}",
e
),
format!("a single literal argument is expected, but parsing is failed: {}", e),
)
})?;
match lit.lit {
Lit::Str(ref lit) => Ok(lit.value()),
_ => Err(Error::new(
lit.span(),
"only string literals are supported here",
)),
_ => Err(Error::new(lit.span(), "only string literals are supported here")),
}
}
fn build(self, span: Span) -> Result<RuntimeVersion> {
macro_rules! required {
($e:expr) => {
$e.ok_or_else(||
{
Error::new(
span,
format!("required field '{}' is missing", stringify!($e)),
)
}
)?
$e.ok_or_else(|| {
Error::new(span, format!("required field '{}' is missing", stringify!($e)))
})?
};
}
+3 -2
View File
@@ -19,7 +19,7 @@
//! into a WASM file.
use codec::Encode;
use parity_wasm::elements::{Module, deserialize_buffer, serialize};
use parity_wasm::elements::{deserialize_buffer, serialize, Module};
#[derive(Clone, Copy, Eq, PartialEq, Debug, thiserror::Error)]
pub enum Error {
@@ -40,7 +40,8 @@ pub fn embed_runtime_version(
) -> Result<Vec<u8>, Error> {
let mut module: Module = deserialize_buffer(wasm).map_err(|_| Error::Deserialize)?;
let apis = version.apis
let apis = version
.apis
.iter()
.map(Encode::encode)
.map(|v| v.into_iter())
+32 -35
View File
@@ -20,20 +20,20 @@
#![cfg_attr(not(feature = "std"), no_std)]
#[cfg(feature = "std")]
use serde::{Serialize, Deserialize};
#[cfg(feature = "std")]
use std::fmt;
use serde::{Deserialize, Serialize};
#[cfg(feature = "std")]
use std::collections::HashSet;
#[cfg(feature = "std")]
use std::fmt;
use codec::{Encode, Decode};
use sp_runtime::RuntimeString;
use codec::{Decode, Encode};
pub use sp_runtime::create_runtime_str;
use sp_runtime::RuntimeString;
#[doc(hidden)]
pub use sp_std;
#[cfg(feature = "std")]
use sp_runtime::{traits::Block as BlockT, generic::BlockId};
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
#[cfg(feature = "std")]
pub mod embed;
@@ -106,7 +106,9 @@ pub type ApisVec = sp_std::borrow::Cow<'static, [(ApiId, u32)]>;
/// Create a vector of Api declarations.
#[macro_export]
macro_rules! create_apis_vec {
( $y:expr ) => { $crate::sp_std::borrow::Cow::Borrowed(& $y) }
( $y:expr ) => {
$crate::sp_std::borrow::Cow::Borrowed(&$y)
};
}
/// Runtime version.
@@ -172,7 +174,9 @@ pub struct RuntimeVersion {
#[cfg(feature = "std")]
impl fmt::Display for RuntimeVersion {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}-{} ({}-{}.tx{}.au{})",
write!(
f,
"{}-{} ({}-{}.tx{}.au{})",
self.spec_name,
self.spec_version,
self.impl_name,
@@ -188,17 +192,13 @@ impl RuntimeVersion {
/// Check if this version matches other version for calling into runtime.
pub fn can_call_with(&self, other: &RuntimeVersion) -> bool {
self.spec_version == other.spec_version &&
self.spec_name == other.spec_name &&
self.authoring_version == other.authoring_version
self.spec_name == other.spec_name &&
self.authoring_version == other.authoring_version
}
/// Check if the given api with `api_id` is implemented and the version passes the given
/// `predicate`.
pub fn has_api_with<P: Fn(u32) -> bool>(
&self,
id: &ApiId,
predicate: P,
) -> bool {
pub fn has_api_with<P: Fn(u32) -> bool>(&self, id: &ApiId, predicate: P) -> bool {
self.apis.iter().any(|(s, v)| s == id && predicate(*v))
}
@@ -229,11 +229,10 @@ impl NativeVersion {
if self.runtime_version.spec_name != other.spec_name {
Err(format!(
"`spec_name` does not match `{}` vs `{}`",
self.runtime_version.spec_name,
other.spec_name,
self.runtime_version.spec_name, other.spec_name,
))
} else if self.runtime_version.authoring_version != other.authoring_version
&& !self.can_author_with.contains(&other.authoring_version)
} else if self.runtime_version.authoring_version != other.authoring_version &&
!self.can_author_with.contains(&other.authoring_version)
{
Err(format!(
"`authoring_version` does not match `{version}` vs `{other_version}` and \
@@ -272,15 +271,13 @@ impl<T: GetRuntimeVersion<Block>, Block: BlockT> GetRuntimeVersion<Block> for st
mod apis_serialize {
use super::*;
use impl_serde::serialize as bytes;
use serde::{Serializer, de, ser::SerializeTuple};
use serde::{de, ser::SerializeTuple, Serializer};
#[derive(Serialize)]
struct ApiId<'a>(
#[serde(serialize_with="serialize_bytesref")] &'a super::ApiId,
&'a u32,
);
struct ApiId<'a>(#[serde(serialize_with = "serialize_bytesref")] &'a super::ApiId, &'a u32);
pub fn serialize<S>(apis: &ApisVec, ser: S) -> Result<S::Ok, S::Error> where
pub fn serialize<S>(apis: &ApisVec, ser: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let len = apis.len();
@@ -291,20 +288,18 @@ mod apis_serialize {
seq.end()
}
pub fn serialize_bytesref<S>(&apis: &&super::ApiId, ser: S) -> Result<S::Ok, S::Error> where
pub fn serialize_bytesref<S>(&apis: &&super::ApiId, ser: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
bytes::serialize(apis, ser)
}
#[derive(Deserialize)]
struct ApiIdOwned(
#[serde(deserialize_with="deserialize_bytes")]
super::ApiId,
u32,
);
struct ApiIdOwned(#[serde(deserialize_with = "deserialize_bytes")] super::ApiId, u32);
pub fn deserialize<'de, D>(deserializer: D) -> Result<ApisVec, D::Error> where
pub fn deserialize<'de, D>(deserializer: D) -> Result<ApisVec, D::Error>
where
D: de::Deserializer<'de>,
{
struct Visitor;
@@ -315,7 +310,8 @@ mod apis_serialize {
formatter.write_str("a sequence of api id and version tuples")
}
fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error> where
fn visit_seq<V>(self, mut visitor: V) -> Result<Self::Value, V::Error>
where
V: de::SeqAccess<'de>,
{
let mut apis = Vec::new();
@@ -328,8 +324,9 @@ mod apis_serialize {
deserializer.deserialize_seq(Visitor)
}
pub fn deserialize_bytes<'de, D>(d: D) -> Result<super::ApiId, D::Error> where
D: de::Deserializer<'de>
pub fn deserialize_bytes<'de, D>(d: D) -> Result<super::ApiId, D::Error>
where
D: de::Deserializer<'de>,
{
let mut arr = [0; 8];
bytes::deserialize_check_len(d, bytes::ExpectedLen::Exact(&mut arr[..]))?;