Skip to content

Commit

Permalink
Merge pull request #217 from 0xcregis/216-refactor-anychain-core-no_std
Browse files Browse the repository at this point in the history
refactor: anychain-core no_std
  • Loading branch information
shuimuliang authored May 2, 2024
2 parents d5fe018 + 27705b6 commit 7c5f8c2
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 52 deletions.
6 changes: 0 additions & 6 deletions anychain-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,6 @@ base58 = { workspace = true , optional = true }
rand_core = { workspace = true }
serde_json = { workspace = true }
sha2 = { workspace = true }
# rand = { workspace = true }
# ethereum-types = { workspace = true }
# blake2b_simd = { workspace = true }
# libsecp256k1 = { workspace = true }
# bls-signatures = { workspace = true }
# rlp = { workspace = true }

[features]
default = ["std"]
Expand Down
15 changes: 8 additions & 7 deletions anychain-core/src/address.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::format::Format;
use crate::no_std::*;
use crate::public_key::{PublicKey, PublicKeyError};
use core::{
fmt::{Debug, Display},
hash::Hash,
str::FromStr,
use crate::{
format::Format,
no_std::{
fmt::{Debug, Display},
hash::Hash,
FromStr, String,
},
public_key::{PublicKey, PublicKeyError},
};

/// The interface for a generic address.
Expand Down
11 changes: 7 additions & 4 deletions anychain-core/src/amount.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::no_std::*;
use core::{
fmt::{Debug, Display},
hash::Hash,
use {
crate::no_std::{
fmt::{Debug, Display},
hash::Hash,
String,
},
thiserror::Error,
};

/// The interface for a generic amount.
Expand Down
18 changes: 10 additions & 8 deletions anychain-core/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::AddressError;
use crate::AmountError;
use crate::FormatError;
use crate::PublicKeyError;
use crate::TransactionError;
use crate::{
no_std::{
fmt::Error as FmtError, io::Error as IoError, num::ParseIntError as NumParseIntError,
String,
},
AddressError, AmountError, FormatError, PublicKeyError, TransactionError,
};

#[derive(Debug, Error)]
pub enum Error {
Expand All @@ -25,14 +27,14 @@ pub enum Error {
InvalidFormat(#[from] FormatError),

#[error("io error: {0:}")]
Io(#[from] ::std::io::Error),
Io(#[from] IoError),

#[error("fmt error: {0:}")]
Fmt(#[from] ::std::fmt::Error),
Fmt(#[from] FmtError),

#[error("fromHex error: {0:}")]
FromHex(#[from] ::hex::FromHexError),

#[error("parsing error: {0:}")]
ParseInt(#[from] ::std::num::ParseIntError),
ParseInt(#[from] NumParseIntError),
}
11 changes: 7 additions & 4 deletions anychain-core/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use crate::no_std::*;
use core::{
fmt::{Debug, Display},
hash::Hash,
use {
crate::no_std::{
fmt::{Debug, Display},
hash::Hash,
String, Vec,
},
thiserror::Error,
};

/// The interface for a generic format.
Expand Down
14 changes: 8 additions & 6 deletions anychain-core/src/network.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use crate::no_std::*;
use core::{
fmt::{Debug, Display},
hash::Hash,
str::FromStr,
use {
crate::no_std::{
fmt::{Debug, Display},
hash::Hash,
FromStr, String,
},
thiserror::Error,
};

/// The interface for a generic network.
Expand All @@ -12,7 +14,7 @@ pub trait Network:
const NAME: &'static str;
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, Error)]
pub enum NetworkError {
#[error("invalid extended private key prefix: {0}")]
InvalidExtendedPrivateKeyPrefix(String),
Expand Down
10 changes: 9 additions & 1 deletion anychain-core/src/no_std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,18 @@ pub use alloc::{
borrow::ToOwned, format, string::FromUtf8Error, string::String, string::ToString, vec, vec::Vec,
};

#[cfg(not(feature = "std"))]
#[doc(hidden)]
pub use core::{
hash, num,
{fmt, str::FromStr},
};

#[cfg(feature = "std")]
#[doc(hidden)]
pub use std::{
borrow::ToOwned, format, string::FromUtf8Error, string::String, string::ToString, vec, vec::Vec,
borrow::ToOwned, fmt, format, hash, num, str::FromStr, string::FromUtf8Error, string::String,
string::ToString, vec, vec::Vec,
};

#[cfg(not(feature = "std"))]
Expand Down
16 changes: 10 additions & 6 deletions anychain-core/src/public_key.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use crate::address::{Address, AddressError};
use crate::format::Format;
use crate::no_std::*;
use core::{
fmt::{Debug, Display},
str::FromStr,
use {
crate::{
address::{Address, AddressError},
format::Format,
no_std::{
fmt::{Debug, Display},
FromStr, String,
},
},
thiserror::Error,
};

/// Generic public key.
Expand Down
25 changes: 15 additions & 10 deletions anychain-core/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
use crate::address::{Address, AddressError};
use crate::amount::AmountError;
use crate::format::Format;
use crate::no_std::*;
use crate::public_key::PublicKey;
use crate::utilities::crypto::keccak256;
use core::{
fmt::{Debug, Display},
hash::Hash,
use {
crate::{
address::{Address, AddressError},
amount::AmountError,
format::Format,
no_std::{
fmt::{Debug, Display},
hash::Hash,
String, Vec,
},
public_key::PublicKey,
utilities::crypto::keccak256,
},
thiserror::Error,
};

/**
Expand Down Expand Up @@ -48,7 +53,7 @@ pub trait Transaction: Clone + Send + Sync + 'static {
fn to_transaction_id(&self) -> Result<Self::TransactionId, TransactionError>;
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, Error)]
pub enum TransactionError {
#[error("{0}")]
AddressError(#[from] AddressError),
Expand Down

0 comments on commit 7c5f8c2

Please sign in to comment.