Differentiate std and core
This commit is contained in:
parent
d41e0722dc
commit
ca454327b2
|
@ -21,6 +21,8 @@ script:
|
||||||
- cargo test --verbose
|
- cargo test --verbose
|
||||||
- cargo build --verbose --no-default-features
|
- cargo build --verbose --no-default-features
|
||||||
- cargo test --verbose --no-default-features
|
- cargo test --verbose --no-default-features
|
||||||
|
- cargo build --verbose --no-default-features --features core,all-languages
|
||||||
|
- cargo test --verbose --no-default-features --features core,all-languages
|
||||||
- cargo build --verbose --features rand,all-languages
|
- cargo build --verbose --features rand,all-languages
|
||||||
- cargo test --verbose --features rand,all-languages
|
- cargo test --verbose --features rand,all-languages
|
||||||
# benchmarks
|
# benchmarks
|
||||||
|
|
|
@ -16,7 +16,8 @@ path = "src/lib.rs"
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
default = [ "std" ]
|
default = [ "std" ]
|
||||||
std = [ "serde/std" ]
|
core = []
|
||||||
|
std = [ "core", "unicode-normalization", "serde/std" ]
|
||||||
|
|
||||||
# Note: English is the standard for bip39 so always included
|
# Note: English is the standard for bip39 so always included
|
||||||
chinese-simplified = []
|
chinese-simplified = []
|
||||||
|
@ -41,8 +42,8 @@ all-languages = [
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bitcoin_hashes = "0.9.4"
|
bitcoin_hashes = "0.9.4"
|
||||||
unicode-normalization = "=0.1.9"
|
|
||||||
|
|
||||||
|
unicode-normalization = { version = "=0.1.9", optional = true }
|
||||||
rand = { version = "0.6.0", optional = true }
|
rand = { version = "0.6.0", optional = true }
|
||||||
serde = { version = "1.0", default-features = false, optional = true }
|
serde = { version = "1.0", default-features = false, optional = true }
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "core")]
|
||||||
use std::fmt;
|
use core::fmt;
|
||||||
|
|
||||||
mod english;
|
mod english;
|
||||||
#[cfg(feature = "chinese-simplified")]
|
#[cfg(feature = "chinese-simplified")]
|
||||||
|
@ -153,7 +153,7 @@ impl Language {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "core")]
|
||||||
impl fmt::Display for Language {
|
impl fmt::Display for Language {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
fmt::Debug::fmt(self, f)
|
fmt::Debug::fmt(self, f)
|
||||||
|
|
36
src/lib.rs
36
src/lib.rs
|
@ -27,7 +27,9 @@
|
||||||
#![deny(missing_docs)]
|
#![deny(missing_docs)]
|
||||||
|
|
||||||
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
|
#![cfg_attr(all(not(test), not(feature = "std")), no_std)]
|
||||||
#[cfg(any(test, feature = "std"))] pub extern crate core;
|
|
||||||
|
#[cfg(any(test, feature = "std"))]
|
||||||
|
pub extern crate core;
|
||||||
|
|
||||||
extern crate bitcoin_hashes;
|
extern crate bitcoin_hashes;
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
|
@ -38,8 +40,11 @@ extern crate rand;
|
||||||
#[cfg(feature = "serde")]
|
#[cfg(feature = "serde")]
|
||||||
pub extern crate serde;
|
pub extern crate serde;
|
||||||
|
|
||||||
|
#[cfg(feature = "core")]
|
||||||
|
use core::{fmt, str};
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::{error, fmt, str};
|
use std::error;
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
|
|
||||||
|
@ -75,7 +80,7 @@ impl AmbiguousLanguages {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An iterator over the possible languages.
|
/// An iterator over the possible languages.
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "core")]
|
||||||
pub fn iter(&self) -> impl Iterator<Item = Language> + '_ {
|
pub fn iter(&self) -> impl Iterator<Item = Language> + '_ {
|
||||||
Language::all().iter().enumerate().filter(move |(i, _)| self.0[*i]).map(|(_, l)| *l)
|
Language::all().iter().enumerate().filter(move |(i, _)| self.0[*i]).map(|(_, l)| *l)
|
||||||
}
|
}
|
||||||
|
@ -106,7 +111,7 @@ pub enum Error {
|
||||||
AmbiguousLanguages(AmbiguousLanguages),
|
AmbiguousLanguages(AmbiguousLanguages),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "core")]
|
||||||
impl fmt::Display for Error {
|
impl fmt::Display for Error {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match *self {
|
match *self {
|
||||||
|
@ -120,7 +125,17 @@ impl fmt::Display for Error {
|
||||||
"entropy was not between 128-256 bits or not a multiple of 32 bits: {} bits", c,
|
"entropy was not between 128-256 bits or not a multiple of 32 bits: {} bits", c,
|
||||||
),
|
),
|
||||||
Error::InvalidChecksum => write!(f, "the mnemonic has an invalid checksum"),
|
Error::InvalidChecksum => write!(f, "the mnemonic has an invalid checksum"),
|
||||||
Error::AmbiguousLanguages(a) => write!(f, "ambiguous word list: {:?}", a.to_vec()),
|
Error::AmbiguousLanguages(a) => {
|
||||||
|
write!(f, "ambiguous word list: ")?;
|
||||||
|
for (i, lang) in a.iter().enumerate() {
|
||||||
|
if i == 0 {
|
||||||
|
write!(f, "{}", lang)?;
|
||||||
|
} else {
|
||||||
|
write!(f, ", {}", lang)?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -130,7 +145,7 @@ impl error::Error for Error {}
|
||||||
|
|
||||||
/// A mnemonic code.
|
/// A mnemonic code.
|
||||||
///
|
///
|
||||||
/// The [std::str::FromStr] implementation will try to determine the language of the
|
/// The [core::str::FromStr] implementation will try to determine the language of the
|
||||||
/// mnemonic from all the supported languages. (Languages have to be explicitly enabled using
|
/// mnemonic from all the supported languages. (Languages have to be explicitly enabled using
|
||||||
/// the Cargo features.)
|
/// the Cargo features.)
|
||||||
///
|
///
|
||||||
|
@ -443,7 +458,7 @@ impl Mnemonic {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "core")]
|
||||||
impl fmt::Display for Mnemonic {
|
impl fmt::Display for Mnemonic {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
for i in 0..self.0.len() {
|
for i in 0..self.0.len() {
|
||||||
|
@ -460,12 +475,15 @@ impl fmt::Display for Mnemonic {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "core")]
|
||||||
impl str::FromStr for Mnemonic {
|
impl str::FromStr for Mnemonic {
|
||||||
type Err = Error;
|
type Err = Error;
|
||||||
|
|
||||||
fn from_str(s: &str) -> Result<Mnemonic, Error> {
|
fn from_str(s: &str) -> Result<Mnemonic, Error> {
|
||||||
Mnemonic::parse(s)
|
#[cfg(feature = "std")]
|
||||||
|
{ Mnemonic::parse(s) }
|
||||||
|
#[cfg(not(feature = "std"))]
|
||||||
|
{ Mnemonic::parse_normalized(s) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue