Skip to content

Commit 00fd9ec

Browse files
author
Michał Papierski
committed
NDRS-974: Get rid of failure
Currently thiserror crate does not support no_std so while wait for upstream PR[1] to get merged I've used "thiserror" for std case, and for no_std thiserror is hidden under cfg_attr. [1]: dtolnay/thiserror#64
1 parent ac41882 commit 00fd9ec

File tree

10 files changed

+223
-165
lines changed

10 files changed

+223
-165
lines changed

Cargo.lock

+13-22
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

types/Cargo.toml

+5-4
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ bitflags = "1"
1717
blake2 = { version = "0.9.0", default-features = false }
1818
datasize = { version = "0.2.4", default-features = false }
1919
ed25519-dalek = { version = "1.0.0", default-features = false, features = ["rand", "u64_backend"] }
20-
# TODO: Replace failure with thiserror once no_std support is landed https://github.com/dtolnay/thiserror/pull/64
21-
failure = { version = "0.1.8", default-features = false, features = ["failure_derive"] }
20+
thiserror = { version = "1.0.20", default-features = false, optional = true }
2221
hex = { version = "0.4.2", default-features = false }
2322
hex_fmt = "0.3.0"
2423
k256 = { version = "0.4.2", default-features = false, features = ["ecdsa", "zeroize"] }
@@ -33,6 +32,7 @@ serde = { version = "1", default-features = false, features = ["derive"] }
3332
serde_json = { version = "1.0.59", default-features = false }
3433
uint = { version = "0.8.3", default-features = false }
3534
once_cell = "1.5.2"
35+
displaydoc = { version = "0.1", default-features = false, optional = true }
3636

3737
[dev-dependencies]
3838
bincode = "1.3.1"
@@ -44,7 +44,7 @@ version-sync = "0.9"
4444
serde_test = "1.0.117"
4545

4646
[features]
47-
default = ["base16/alloc", "serde/alloc", "serde_json/alloc"]
47+
default = ["base16/alloc", "serde/alloc", "serde_json/alloc", "displaydoc"]
4848
std = [
4949
"base16/std",
5050
"base64/std",
@@ -54,7 +54,8 @@ std = [
5454
"k256/std",
5555
"serde/std",
5656
"serde_json/std",
57-
"schemars"
57+
"schemars",
58+
"thiserror"
5859
]
5960
gens = ["std", "proptest/std"]
6061

types/src/account.rs

+58-19
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,15 @@ use blake2::{
1515
VarBlake2b,
1616
};
1717
use datasize::DataSize;
18-
use failure::Fail;
1918
use rand::{
2019
distributions::{Distribution, Standard},
2120
Rng,
2221
};
2322
#[cfg(feature = "std")]
2423
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
2524
use serde::{de::Error as SerdeError, Deserialize, Deserializer, Serialize, Serializer};
25+
#[cfg(feature = "std")]
26+
use thiserror::Error;
2627

2728
use crate::{
2829
bytesrepr::{Error, FromBytes, ToBytes, U8_SERIALIZED_LENGTH},
@@ -106,22 +107,33 @@ impl TryFrom<u32> for ActionType {
106107
/// Errors that can occur while changing action thresholds (i.e. the total [`Weight`]s of signing
107108
/// [`AccountHash`]s required to perform various actions) on an account.
108109
#[repr(i32)]
109-
#[derive(Debug, Fail, PartialEq, Eq, Copy, Clone)]
110+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
111+
#[cfg_attr(feature = "std", derive(Error))]
110112
pub enum SetThresholdFailure {
111113
/// Setting the key-management threshold to a value lower than the deployment threshold is
112114
/// disallowed.
113-
#[fail(display = "New threshold should be greater than or equal to deployment threshold")]
115+
#[cfg_attr(
116+
feature = "std",
117+
error("New threshold should be greater than or equal to deployment threshold")
118+
)]
114119
KeyManagementThreshold = 1,
115120
/// Setting the deployment threshold to a value greater than any other threshold is disallowed.
116-
#[fail(display = "New threshold should be lower than or equal to key management threshold")]
121+
#[cfg_attr(
122+
feature = "std",
123+
error("New threshold should be lower than or equal to key management threshold")
124+
)]
117125
DeploymentThreshold = 2,
118126
/// Caller doesn't have sufficient permissions to set new thresholds.
119-
#[fail(display = "Unable to set action threshold due to insufficient permissions")]
127+
#[cfg_attr(
128+
feature = "std",
129+
error("Unable to set action threshold due to insufficient permissions")
130+
)]
120131
PermissionDeniedError = 3,
121132
/// Setting a threshold to a value greater than the total weight of associated keys is
122133
/// disallowed.
123-
#[fail(
124-
display = "New threshold should be lower or equal than total weight of associated keys"
134+
#[cfg_attr(
135+
feature = "std",
136+
error("New threshold should be lower or equal than total weight of associated keys")
125137
)]
126138
InsufficientTotalWeight = 4,
127139
}
@@ -396,19 +408,29 @@ impl Distribution<AccountHash> for Standard {
396408
}
397409

398410
/// Errors that can occur while adding a new [`AccountHash`] to an account's associated keys map.
399-
#[derive(PartialEq, Eq, Fail, Debug, Copy, Clone)]
411+
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
412+
#[cfg_attr(feature = "std", derive(Error))]
400413
#[repr(i32)]
401414
pub enum AddKeyFailure {
402415
/// There are already [`MAX_ASSOCIATED_KEYS`] [`AccountHash`]s associated with the given
403416
/// account.
404-
#[fail(display = "Unable to add new associated key because maximum amount of keys is reached")]
417+
#[cfg_attr(
418+
feature = "std",
419+
error("Unable to add new associated key because maximum amount of keys is reached")
420+
)]
405421
MaxKeysLimit = 1,
406422
/// The given [`AccountHash`] is already associated with the given account.
407-
#[fail(display = "Unable to add new associated key because given key already exists")]
423+
#[cfg_attr(
424+
feature = "std",
425+
error("Unable to add new associated key because given key already exists")
426+
)]
408427
DuplicateKey = 2,
409428
/// Caller doesn't have sufficient permissions to associate a new [`AccountHash`] with the
410429
/// given account.
411-
#[fail(display = "Unable to add new associated key due to insufficient permissions")]
430+
#[cfg_attr(
431+
feature = "std",
432+
error("Unable to add new associated key due to insufficient permissions")
433+
)]
412434
PermissionDenied = 3,
413435
}
414436

@@ -428,19 +450,26 @@ impl TryFrom<i32> for AddKeyFailure {
428450
}
429451

430452
/// Errors that can occur while removing a [`AccountHash`] from an account's associated keys map.
431-
#[derive(Fail, Debug, Eq, PartialEq, Copy, Clone)]
453+
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
454+
#[cfg_attr(feature = "std", derive(Error))]
432455
#[repr(i32)]
433456
pub enum RemoveKeyFailure {
434457
/// The given [`AccountHash`] is not associated with the given account.
435-
#[fail(display = "Unable to remove a key that does not exist")]
458+
#[cfg_attr(feature = "std", error("Unable to remove a key that does not exist"))]
436459
MissingKey = 1,
437460
/// Caller doesn't have sufficient permissions to remove an associated [`AccountHash`] from the
438461
/// given account.
439-
#[fail(display = "Unable to remove associated key due to insufficient permissions")]
462+
#[cfg_attr(
463+
feature = "std",
464+
error("Unable to remove associated key due to insufficient permissions")
465+
)]
440466
PermissionDenied = 2,
441467
/// Removing the given associated [`AccountHash`] would cause the total weight of all remaining
442468
/// `AccountHash`s to fall below one of the action thresholds for the given account.
443-
#[fail(display = "Unable to remove a key which would violate action threshold constraints")]
469+
#[cfg_attr(
470+
feature = "std",
471+
error("Unable to remove a key which would violate action threshold constraints")
472+
)]
444473
ThresholdViolation = 3,
445474
}
446475

@@ -465,20 +494,30 @@ impl TryFrom<i32> for RemoveKeyFailure {
465494

466495
/// Errors that can occur while updating the [`Weight`] of a [`AccountHash`] in an account's
467496
/// associated keys map.
468-
#[derive(PartialEq, Eq, Fail, Debug, Copy, Clone)]
497+
#[derive(PartialEq, Eq, Debug, Copy, Clone)]
498+
#[cfg_attr(feature = "std", derive(Error))]
469499
#[repr(i32)]
470500
pub enum UpdateKeyFailure {
471501
/// The given [`AccountHash`] is not associated with the given account.
472-
#[fail(display = "Unable to update the value under an associated key that does not exist")]
502+
#[cfg_attr(
503+
feature = "std",
504+
error("Unable to update the value under an associated key that does not exist")
505+
)]
473506
MissingKey = 1,
474507
/// Caller doesn't have sufficient permissions to update an associated [`AccountHash`] from the
475508
/// given account.
476-
#[fail(display = "Unable to update associated key due to insufficient permissions")]
509+
#[cfg_attr(
510+
feature = "std",
511+
error("Unable to update associated key due to insufficient permissions")
512+
)]
477513
PermissionDenied = 2,
478514
/// Updating the [`Weight`] of the given associated [`AccountHash`] would cause the total
479515
/// weight of all `AccountHash`s to fall below one of the action thresholds for the given
480516
/// account.
481-
#[fail(display = "Unable to update weight that would fall below any of action thresholds")]
517+
#[cfg_attr(
518+
feature = "std",
519+
error("Unable to update weight that would fall below any of action thresholds")
520+
)]
482521
ThresholdViolation = 3,
483522
}
484523

types/src/bytesrepr.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ use alloc::{
1515
use core::any;
1616
use core::{mem, ptr::NonNull};
1717

18-
use failure::Fail;
1918
use num_integer::Integer;
2019
use num_rational::Ratio;
2120
use serde::{Deserialize, Serialize};
21+
#[cfg(feature = "std")]
22+
use thiserror::Error;
2223

2324
pub use bytes::Bytes;
2425

@@ -98,20 +99,21 @@ pub fn allocate_buffer<T: ToBytes>(to_be_serialized: &T) -> Result<Vec<u8>, Erro
9899
}
99100

100101
/// Serialization and deserialization errors.
101-
#[derive(Debug, Fail, PartialEq, Eq, Clone, Serialize, Deserialize)]
102+
#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize)]
103+
#[cfg_attr(feature = "std", derive(Error))]
102104
#[repr(u8)]
103105
pub enum Error {
104106
/// Early end of stream while deserializing.
105-
#[fail(display = "Deserialization error: early end of stream")]
107+
#[cfg_attr(feature = "std", error("Deserialization error: early end of stream"))]
106108
EarlyEndOfStream = 0,
107109
/// Formatting error while deserializing.
108-
#[fail(display = "Deserialization error: formatting")]
110+
#[cfg_attr(feature = "std", error("Deserialization error: formatting"))]
109111
Formatting,
110112
/// Not all input bytes were consumed in [`deserialize`].
111-
#[fail(display = "Deserialization error: left-over bytes")]
113+
#[cfg_attr(feature = "std", error("Deserialization error: left-over bytes"))]
112114
LeftOverBytes,
113115
/// Out of memory error.
114-
#[fail(display = "Serialization error: out of memory")]
116+
#[cfg_attr(feature = "std", error("Serialization error: out of memory"))]
115117
OutOfMemory,
116118
}
117119

types/src/cl_value.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ use alloc::{string::String, vec::Vec};
55
use core::fmt;
66

77
use datasize::DataSize;
8-
use failure::Fail;
98
#[cfg(feature = "std")]
109
use schemars::{gen::SchemaGenerator, schema::Schema, JsonSchema};
1110
use serde::{de::Error as SerdeError, Deserialize, Deserializer, Serialize, Serializer};
1211
use serde_json::Value;
12+
#[cfg(feature = "std")]
13+
use thiserror::Error;
1314

1415
use crate::{
1516
bytesrepr::{self, Bytes, FromBytes, ToBytes, U32_SERIALIZED_LENGTH},
@@ -39,13 +40,14 @@ impl fmt::Display for CLTypeMismatch {
3940
}
4041

4142
/// Error relating to [`CLValue`] operations.
42-
#[derive(Fail, PartialEq, Eq, Clone, Debug)]
43+
#[derive(PartialEq, Eq, Clone, Debug)]
44+
#[cfg_attr(feature = "std", derive(Error))]
4345
pub enum CLValueError {
4446
/// An error while serializing or deserializing the underlying data.
45-
#[fail(display = "CLValue error: {}", _0)]
47+
#[cfg_attr(feature = "std", error("CLValue error: {}", _0))]
4648
Serialization(bytesrepr::Error),
4749
/// A type mismatch while trying to convert a [`CLValue`] into a given type.
48-
#[fail(display = "Type mismatch: {}", _0)]
50+
#[cfg_attr(feature = "std", error("Type mismatch: {}", _0))]
4951
Type(CLTypeMismatch),
5052
}
5153

types/src/semver.rs

+12-4
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,13 @@ use alloc::vec::Vec;
22
use core::{convert::TryFrom, fmt, num::ParseIntError};
33

44
use datasize::DataSize;
5-
use failure::Fail;
65
use serde::{Deserialize, Serialize};
76

7+
#[cfg(not(feature = "std"))]
8+
use displaydoc::Display;
9+
#[cfg(feature = "std")]
10+
use thiserror::Error;
11+
812
use crate::bytesrepr::{self, Error, FromBytes, ToBytes, U32_SERIALIZED_LENGTH};
913

1014
/// Length of SemVer when serialized
@@ -81,11 +85,15 @@ impl fmt::Display for SemVer {
8185
}
8286
}
8387

84-
#[derive(Fail, Debug, Clone, PartialEq, Eq)]
88+
#[derive(Debug, Clone, PartialEq, Eq)]
89+
#[cfg_attr(feature = "std", derive(Error))]
90+
#[cfg_attr(not(feature = "std"), derive(Display))]
8591
pub enum ParseSemVerError {
86-
#[fail(display = "Invalid version format")]
92+
/// Invalid version format
93+
#[cfg_attr(feature = "std", error("Invalid version format"))]
8794
InvalidVersionFormat,
88-
#[fail(display = "{}", _0)]
95+
/// {0}
96+
#[cfg_attr(feature = "std", error("{}", _0))]
8997
ParseIntError(ParseIntError),
9098
}
9199

0 commit comments

Comments
 (0)