Skip to content

Commit

Permalink
use core::error::Error and make the crate no-std (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasad1 authored Nov 11, 2024
1 parent a574dde commit 82bb039
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 57 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = "https://github.com/paritytech/scale-encode"
homepage = "https://www.parity.io/"
keywords = ["parity", "scale", "encoding"]
include = ["Cargo.toml", "src/**/*.rs", "README.md", "LICENSE"]
rust-version = "1.81.0"

[workspace.dependencies]
scale-encode = { version = "0.8.0", path = "scale-encode" }
Expand Down
1 change: 1 addition & 0 deletions scale-encode-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ repository.workspace = true
homepage.workspace = true
keywords.workspace = true
include.workspace = true
rust-version.workspace = true

[lib]
proc-macro = true
Expand Down
8 changes: 3 additions & 5 deletions scale-encode/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ repository.workspace = true
homepage.workspace = true
keywords.workspace = true
include.workspace = true
rust-version.workspace = true

[features]
default = ["std", "derive", "primitive-types", "bits"]

# Activates std feature.
std = []
default = ["derive", "primitive-types", "bits"]

# Include the derive proc macro.
derive = ["dep:scale-encode-derive"]
Expand All @@ -35,7 +33,7 @@ scale-bits = { version = "0.6.0", default-features = false, optional = true }
scale-encode-derive = { workspace = true, optional = true }
primitive-types = { version = "0.13.1", optional = true, default-features = false }
smallvec = "1.10.0"
derive_more = { version = "1.0.0", default-features = false, features = ["from", "display"] }
thiserror = { version = "2.0.0", default-features = false }

[dev-dependencies]
bitvec = { version = "1.0.1", default-features = false }
Expand Down
67 changes: 16 additions & 51 deletions scale-encode/src/error/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ pub struct Error {
kind: ErrorKind,
}

#[cfg(feature = "std")]
impl std::error::Error for Error {}
impl core::error::Error for Error {}

impl Error {
/// Construct a new error given an error kind.
Expand All @@ -40,24 +39,22 @@ impl Error {
}
}
/// Construct a new, custom error.
pub fn custom(error: impl CustomError) -> Error {
pub fn custom(error: impl core::error::Error + Send + Sync + 'static) -> Error {
Error::new(ErrorKind::Custom(Box::new(error)))
}
/// Construct a custom error from a static string.
pub fn custom_str(error: &'static str) -> Error {
#[derive(derive_more::Display, Debug)]
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct StrError(pub &'static str);
#[cfg(feature = "std")]
impl std::error::Error for StrError {}

Error::new(ErrorKind::Custom(Box::new(StrError(error))))
}
/// Construct a custom error from an owned string.
pub fn custom_string(error: String) -> Error {
#[derive(derive_more::Display, Debug)]
#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct StringError(String);
#[cfg(feature = "std")]
impl std::error::Error for StringError {}

Error::new(ErrorKind::Custom(Box::new(StringError(error))))
}
Expand Down Expand Up @@ -112,70 +109,57 @@ impl Display for Error {
}

/// The underlying nature of the error.
#[derive(Debug, derive_more::From, derive_more::Display)]
#[derive(Debug, thiserror::Error)]
pub enum ErrorKind {
/// There was an error resolving the type via the given [`crate::TypeResolver`].
#[display("Failed to resolve type: {_0}")]
#[error("Failed to resolve type: {0}")]
TypeResolvingError(String),
/// Cannot find a given type.
#[display("Cannot find type with identifier {_0}")]
#[error("Cannot find type with identifier {0}")]
TypeNotFound(String),
/// Cannot encode the actual type given into the target type ID.
#[display("Cannot encode {actual:?} into type with ID {expected_id}")]
#[error("Cannot encode {actual:?} into type with ID {expected_id}")]
WrongShape {
/// The actual kind we have to encode
actual: Kind,
/// Identifier for the expected type
expected_id: String,
},
/// The types line up, but the expected length of the target type is different from the length of the input value.
#[display("Cannot encode to type; expected length {expected_len} but got length {actual_len}")]
#[error("Cannot encode to type; expected length {expected_len} but got length {actual_len}")]
WrongLength {
/// Length we have
actual_len: usize,
/// Length expected for type.
expected_len: usize,
},
/// We cannot encode the number given into the target type; it's out of range.
#[display("Number {value} is out of range for target type with identifier {expected_id}")]
#[error("Number {value} is out of range for target type with identifier {expected_id}")]
NumberOutOfRange {
/// A string represenatation of the numeric value that was out of range.
value: String,
/// Identifier for the expected numeric type that we tried to encode it to.
expected_id: String,
},
/// Cannot find a variant with a matching name on the target type.
#[display("Variant {name} does not exist on type with identifier {expected_id}")]
#[error("Variant {name} does not exist on type with identifier {expected_id}")]
CannotFindVariant {
/// Variant name we can't find in the expected type.
name: String,
/// Identifier for the expected type.
expected_id: String,
},
/// Cannot find a field on our source type that's needed for the target type.
#[display("Field {name} does not exist in our source struct")]
#[error("Field {name} does not exist in our source struct")]
CannotFindField {
/// Name of the field which was not provided.
name: String,
},
/// A custom error.
#[from]
#[display("Custom error: {_0}")]
Custom(Box<dyn CustomError>),
#[error("Custom error: {0}")]
Custom(Box<dyn core::error::Error + Send + Sync + 'static>),
}

/// Anything implementing this trait can be used in [`ErrorKind::Custom`].
#[cfg(feature = "std")]
pub trait CustomError: std::error::Error + Send + Sync + 'static {}
#[cfg(feature = "std")]
impl<T: std::error::Error + Send + Sync + 'static> CustomError for T {}

/// Anything implementing this trait can be used in [`ErrorKind::Custom`].
#[cfg(not(feature = "std"))]
pub trait CustomError: core::fmt::Debug + core::fmt::Display + Send + Sync + 'static {}
#[cfg(not(feature = "std"))]
impl<T: core::fmt::Debug + core::fmt::Display + Send + Sync + 'static> CustomError for T {}

/// The kind of type that we're trying to encode.
#[allow(missing_docs)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
Expand All @@ -190,22 +174,3 @@ pub enum Kind {
Str,
Number,
}

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

#[derive(Debug, derive_more::Display)]
enum MyError {
Foo,
}

#[cfg(feature = "std")]
impl std::error::Error for MyError {}

#[test]
fn custom_error() {
// Just a compile-time check that we can ergonomically provide an arbitrary custom error:
Error::custom(MyError::Foo);
}
}
2 changes: 1 addition & 1 deletion scale-encode/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#![cfg_attr(not(feature = "std"), no_std)]
#![no_std]

/*!
`parity-scale-codec` provides an `Encode` trait which allows types to SCALE encode themselves based on their shape.
Expand Down

0 comments on commit 82bb039

Please sign in to comment.