diff --git a/Cargo.lock b/Cargo.lock index 018cbb9bb..0ce3ad4e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5188,7 +5188,6 @@ dependencies = [ "kitchensink-runtime", "log", "parity-scale-codec", - "primitive-types", "serde", "serde_json", "sp-core", @@ -5197,7 +5196,7 @@ dependencies = [ "sp-runtime-interface", "sp-std", "sp-version", - "thiserror", + "thiserror-core", "tungstenite", "url", "ws", @@ -5333,6 +5332,26 @@ dependencies = [ "thiserror-impl", ] +[[package]] +name = "thiserror-core" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "808e769db82353a456db04af0e76a4e211c5428516cead2b420dfa3d82cb83d3" +dependencies = [ + "thiserror-core-impl", +] + +[[package]] +name = "thiserror-core-impl" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7905b4dad6617f9e81544469da6bb1c658c02c6c7e420ee953d03687db1e1cfe" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "thiserror-impl" version = "1.0.37" diff --git a/Cargo.toml b/Cargo.toml index df1349489..82ba7584d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,9 +25,10 @@ log = { version = "0.4.14", default-features = false } serde = { version = "1.0.136", default-features = false, features = ["derive"] } serde_json = { version = "1.0.79", default-features = false } +#FIXME: convert back to thiserror once possible #317 +thiserror = { version = "1.0", package = "thiserror-core", default-features = false } + # crates.io std only -primitive-types = { version = "0.12.1", optional = true, features = ["codec"] } -thiserror = { version = "1.0.30", optional = true } url = { version = "2.0.0", optional = true } # websocket dependent features @@ -70,9 +71,8 @@ std = [ "log/std", "serde/std", "serde_json/std", + "thiserror/std", # crates.io std only - "primitive-types", - "thiserror", "url", # substrate no_std "frame-metadata/std", diff --git a/compose-macros/src/lib.rs b/compose-macros/src/lib.rs index 4b650af8a..34f00c3ad 100644 --- a/compose-macros/src/lib.rs +++ b/compose-macros/src/lib.rs @@ -21,9 +21,7 @@ // re-export for macro resolution pub use ac_primitives as primitives; -#[cfg(feature = "std")] pub use codec; -#[cfg(feature = "std")] pub use log; pub use rpc::*; pub use sp_core; @@ -98,7 +96,6 @@ macro_rules! compose_extrinsic_offline { /// * 'args' - Optional sequence of arguments of the call. They are not checked against the metadata. /// As of now the user needs to check himself that the correct arguments are supplied. #[macro_export] -#[cfg(feature = "std")] macro_rules! compose_extrinsic { ($api: expr, $module: expr, diff --git a/primitives/src/rpc_params.rs b/primitives/src/rpc_params.rs index 55b8ed30a..a2a2a7565 100644 --- a/primitives/src/rpc_params.rs +++ b/primitives/src/rpc_params.rs @@ -42,7 +42,6 @@ impl RpcParams { } /// Insert a plain value into the builder. - #[cfg(feature = "std")] pub fn insert(&mut self, value: P) -> Result<()> { self.0.insert(value) } @@ -145,6 +144,13 @@ impl ParamsBuilder { Ok(()) } + /// Insert a plain value into the builder with heap allocation. If available, + /// use the more efficient std version. + #[cfg(not(feature = "std"))] + pub(crate) fn insert(&mut self, value: P) -> Result<()> { + self.insert_with_allocation(value) + } + /// Insert a plain value into the builder with heap allocation. For better performance, /// use the std version, if possible. pub(crate) fn insert_with_allocation(&mut self, value: P) -> Result<()> { diff --git a/src/lib.rs b/src/lib.rs index ce849fbf0..c97fea2af 100755 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,12 +16,16 @@ */ #![cfg_attr(not(feature = "std"), no_std)] #![feature(assert_matches)] +#![feature(error_in_core)] + +extern crate alloc; pub use ac_compose_macros::*; pub use ac_node_api::*; pub use ac_primitives::*; pub use utils::*; +pub mod rpc; pub mod utils; // std only features: @@ -33,5 +37,3 @@ pub use api::*; pub mod api; #[cfg(feature = "std")] pub mod extrinsic; -#[cfg(feature = "std")] -pub mod rpc; diff --git a/src/rpc/error.rs b/src/rpc/error.rs index e238637cb..a35c5deae 100644 --- a/src/rpc/error.rs +++ b/src/rpc/error.rs @@ -15,33 +15,33 @@ */ -use std::sync::mpsc::SendError; +use alloc::{boxed::Box, string::String}; pub type Result = core::result::Result; #[derive(Debug, thiserror::Error)] pub enum Error { #[error("Serde json error: {0}")] - Serde(#[from] serde_json::error::Error), + Serde(serde_json::error::Error), #[error("mpsc send Error: {0}")] Send(String), #[error("Could not convert to valid Url: {0}")] - Url(#[from] url::ParseError), + Url(String), #[error("ChannelReceiveError, sender is disconnected: {0}")] - ChannelDisconnected(#[from] sp_std::sync::mpsc::RecvError), + ChannelDisconnected(String), #[error("Failure during thread creation: {0}")] - Io(#[from] std::io::Error), + Io(String), #[error("Exceeded maximum amount of connections")] ConnectionAttemptsExceeded, #[error("Websocket Connection was closed unexpectedly")] ConnectionClosed, #[error(transparent)] - Client(#[from] Box), + Client(#[from] Box), } -impl From> for Error { - fn from(error: SendError) -> Self { - Self::Send(error.0) +impl From for Error { + fn from(error: serde_json::error::Error) -> Self { + Self::Serde(error) } } @@ -58,3 +58,35 @@ impl From for Error { Self::Client(Box::new(error)) } } + +#[cfg(feature = "std")] +pub use std_only::*; +#[cfg(feature = "std")] +mod std_only { + use super::*; + use std::sync::mpsc::{RecvError, SendError}; + + impl From> for Error { + fn from(error: SendError) -> Self { + Self::Send(error.0) + } + } + + impl From for Error { + fn from(error: RecvError) -> Self { + Self::ChannelDisconnected(format!("{:?}", error)) + } + } + + impl From for Error { + fn from(error: std::io::Error) -> Self { + Self::Io(format!("{:?}", error)) + } + } + + impl From for Error { + fn from(error: url::ParseError) -> Self { + Self::Io(format!("{:?}", error)) + } + } +} diff --git a/src/rpc/mod.rs b/src/rpc/mod.rs index bee5159fe..206d7cbde 100644 --- a/src/rpc/mod.rs +++ b/src/rpc/mod.rs @@ -35,6 +35,7 @@ pub mod error; pub use error::*; use ac_primitives::RpcParams; +use alloc::string::{String, ToString}; use serde::de::DeserializeOwned; /// Trait to be implemented by the ws-client for sending rpc requests and extrinsic. diff --git a/src/utils.rs b/src/utils.rs index dcd62ceef..351484738 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -15,11 +15,7 @@ */ -#[cfg(not(feature = "std"))] -extern crate alloc; -#[cfg(not(feature = "std"))] use alloc::{string::String, vec::Vec}; - use hex::FromHexError; use sp_core::{storage::StorageKey, twox_128, H256};