From 63d41d356852d6b01cc96a167d0f7aaf04a35a78 Mon Sep 17 00:00:00 2001 From: mriise Date: Thu, 29 Oct 2020 16:08:50 -0700 Subject: [PATCH 01/15] build Codecs with macros --- src/impls.rs | 306 +++++++++++---------------------------------------- 1 file changed, 67 insertions(+), 239 deletions(-) diff --git a/src/impls.rs b/src/impls.rs index 69528a8..394702b 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -1,6 +1,48 @@ use crate::encoding; use crate::error::Result; +macro_rules! derrive_base_encoding { + ($type:ident, $encoding:expr) => { + #[derive(PartialEq, Eq, Clone, Copy, Debug)] + pub(crate) struct $type; + + impl BaseCodec for $type { + fn encode>(input: I) -> String { + $encoding.encode(input.as_ref()) + } + + fn decode>(input: I) -> Result> { + Ok($encoding.decode(input.as_ref().as_bytes())?) + } + } + }; + ($type:ident, $encoding:expr; $($type2:ident, $encoding2:expr);+) => { + derrive_base_encoding! ($type, $encoding); + derrive_base_encoding!($($type2, $encoding2);+); + }; +} + +macro_rules! derrive_base_x { + ($type:ident, $encoding:expr) => { + #[derive(PartialEq, Eq, Clone, Copy, Debug)] + pub(crate) struct $type; + + impl BaseCodec for $type { + fn encode>(input: I) -> String { + base_x::encode($encoding, input.as_ref()) + } + + fn decode>(input: I) -> Result> { + Ok(base_x::decode($encoding, input.as_ref())?) + } + } + }; + ($type:ident, $encoding:expr; $($type2:ident, $encoding2:expr);+) => { + derrive_base_x! ($type, $encoding); + derrive_base_x!($($type2, $encoding2);+); + }; +} + pub(crate) trait BaseCodec { /// Encode with the given byte slice. fn encode>(input: I) -> String; @@ -23,285 +65,71 @@ impl BaseCodec for Identity { } } -/// Base2 (alphabet: 01). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base2; - -impl BaseCodec for Base2 { - fn encode>(input: I) -> String { - encoding::BASE2.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE2.decode(input.as_ref().as_bytes())?) - } +derrive_base_encoding! { + Base2, encoding::BASE2; + Base8, encoding::BASE8; + Base16Lower, encoding::BASE16_LOWER; + Base16Upper, encoding::BASE16_UPPER; + Base32Lower, encoding::BASE32_NOPAD_LOWER; + Base32Upper, encoding::BASE32_NOPAD_UPPER; + Base32PadLower, encoding::BASE32_PAD_LOWER; + Base32PadUpper, encoding::BASE32_PAD_UPPER; + Base32HexLower, encoding::BASE32HEX_NOPAD_LOWER; + Base32HexUpper, encoding::BASE32HEX_NOPAD_UPPER; + Base32HexPadLower, encoding::BASE32HEX_PAD_LOWER; + Base32HexPadUpper, encoding::BASE32HEX_PAD_UPPER; + Base32Z, encoding::BASE32Z; + Base64, encoding::BASE64_NOPAD; + Base64Pad, encoding::BASE64_PAD; + Base64Url, encoding::BASE64URL_NOPAD; + Base64UrlPad, encoding::BASE64URL_PAD } -/// Base8 (alphabet: 01234567). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base8; - -impl BaseCodec for Base8 { - fn encode>(input: I) -> String { - encoding::BASE8.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE8.decode(input.as_ref().as_bytes())?) - } +derrive_base_x! { + Base10, encoding::BASE10; + Base58Flickr, encoding::BASE58_FLICKR; + Base58Btc, encoding::BASE58_BITCOIN } -/// Base10 (alphabet: 0123456789). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base10; +/// Base2 (alphabet: 01). -impl BaseCodec for Base10 { - fn encode>(input: I) -> String { - base_x::encode(encoding::BASE10, input.as_ref()) - } +/// Base8 (alphabet: 01234567). - fn decode>(input: I) -> Result> { - Ok(base_x::decode(encoding::BASE10, input.as_ref())?) - } -} +/// Base10 (alphabet: 0123456789). /// Base16 lower hexadecimal (alphabet: 0123456789abcdef). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base16Lower; - -impl BaseCodec for Base16Lower { - fn encode>(input: I) -> String { - encoding::BASE16_LOWER.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE16_LOWER.decode(input.as_ref().as_bytes())?) - } -} /// Base16 upper hexadecimal (alphabet: 0123456789ABCDEF). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base16Upper; - -impl BaseCodec for Base16Upper { - fn encode>(input: I) -> String { - encoding::BASE16_UPPER.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE16_UPPER.decode(input.as_ref().as_bytes())?) - } -} /// Base32, rfc4648 no padding (alphabet: abcdefghijklmnopqrstuvwxyz234567). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base32Lower; - -impl BaseCodec for Base32Lower { - fn encode>(input: I) -> String { - encoding::BASE32_NOPAD_LOWER.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE32_NOPAD_LOWER.decode(input.as_ref().as_bytes())?) - } -} /// Base32, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base32Upper; - -impl BaseCodec for Base32Upper { - fn encode>(input: I) -> String { - encoding::BASE32_NOPAD_UPPER.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE32_NOPAD_UPPER.decode(input.as_ref().as_bytes())?) - } -} /// Base32, rfc4648 with padding (alphabet: abcdefghijklmnopqrstuvwxyz234567). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base32PadLower; - -impl BaseCodec for Base32PadLower { - fn encode>(input: I) -> String { - encoding::BASE32_PAD_LOWER.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE32_PAD_LOWER.decode(input.as_ref().as_bytes())?) - } -} /// Base32, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base32PadUpper; - -impl BaseCodec for Base32PadUpper { - fn encode>(input: I) -> String { - encoding::BASE32_PAD_UPPER.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE32_PAD_UPPER.decode(input.as_ref().as_bytes())?) - } -} /// Base32hex, rfc4648 no padding (alphabet: 0123456789abcdefghijklmnopqrstuv). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base32HexLower; - -impl BaseCodec for Base32HexLower { - fn encode>(input: I) -> String { - encoding::BASE32HEX_NOPAD_LOWER.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE32HEX_NOPAD_LOWER.decode(input.as_ref().as_bytes())?) - } -} /// Base32hex, rfc4648 no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base32HexUpper; - -impl BaseCodec for Base32HexUpper { - fn encode>(input: I) -> String { - encoding::BASE32HEX_NOPAD_UPPER.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE32HEX_NOPAD_UPPER.decode(input.as_ref().as_bytes())?) - } -} /// Base32hex, rfc4648 with padding (alphabet: 0123456789abcdefghijklmnopqrstuv). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base32HexPadLower; - -impl BaseCodec for Base32HexPadLower { - fn encode>(input: I) -> String { - encoding::BASE32HEX_PAD_LOWER.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE32HEX_PAD_LOWER.decode(input.as_ref().as_bytes())?) - } -} /// Base32hex, rfc4648 with padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base32HexPadUpper; - -impl BaseCodec for Base32HexPadUpper { - fn encode>(input: I) -> String { - encoding::BASE32HEX_PAD_UPPER.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE32HEX_PAD_UPPER.decode(input.as_ref().as_bytes())?) - } -} /// z-base-32 (used by Tahoe-LAFS) (alphabet: ybndrfg8ejkmcpqxot1uwisza345h769). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base32Z; - -impl BaseCodec for Base32Z { - fn encode>(input: I) -> String { - encoding::BASE32Z.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE32Z.decode(input.as_ref().as_bytes())?) - } -} /// Base58 flicker (alphabet: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base58Flickr; - -impl BaseCodec for Base58Flickr { - fn encode>(input: I) -> String { - base_x::encode(encoding::BASE58_FLICKR, input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(base_x::decode(encoding::BASE58_FLICKR, input.as_ref())?) - } -} /// Base58 bitcoin (alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base58Btc; - -impl BaseCodec for Base58Btc { - fn encode>(input: I) -> String { - base_x::encode(encoding::BASE58_BITCOIN, input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(base_x::decode(encoding::BASE58_BITCOIN, input.as_ref())?) - } -} /// Base64, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base64; - -impl BaseCodec for Base64 { - fn encode>(input: I) -> String { - encoding::BASE64_NOPAD.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE64_NOPAD.decode(input.as_ref().as_bytes())?) - } -} /// Base64, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base64Pad; - -impl BaseCodec for Base64Pad { - fn encode>(input: I) -> String { - encoding::BASE64_PAD.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE64_PAD.decode(input.as_ref().as_bytes())?) - } -} /// Base64 url, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base64Url; - -impl BaseCodec for Base64Url { - fn encode>(input: I) -> String { - encoding::BASE64URL_NOPAD.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE64URL_NOPAD.decode(input.as_ref().as_bytes())?) - } -} /// Base64 url, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_). -#[derive(PartialEq, Eq, Clone, Copy, Debug)] -pub(crate) struct Base64UrlPad; - -impl BaseCodec for Base64UrlPad { - fn encode>(input: I) -> String { - encoding::BASE64URL_PAD.encode(input.as_ref()) - } - - fn decode>(input: I) -> Result> { - Ok(encoding::BASE64URL_PAD.decode(input.as_ref().as_bytes())?) - } -} #[cfg(test)] mod tests { From b752be6176b4c15ae6f8112bb93b78e4245bb389 Mon Sep 17 00:00:00 2001 From: mriise Date: Thu, 29 Oct 2020 16:58:52 -0700 Subject: [PATCH 02/15] fix docs in macros --- src/impls.rs | 84 +++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 50 deletions(-) diff --git a/src/impls.rs b/src/impls.rs index 394702b..d87ba6b 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -2,7 +2,9 @@ use crate::encoding; use crate::error::Result; macro_rules! derrive_base_encoding { - ($type:ident, $encoding:expr) => { + (#[$doc:meta] $type:ident, $encoding:expr;) => { + + #[$doc] #[derive(PartialEq, Eq, Clone, Copy, Debug)] pub(crate) struct $type; @@ -16,14 +18,16 @@ macro_rules! derrive_base_encoding { } } }; - ($type:ident, $encoding:expr; $($type2:ident, $encoding2:expr);+) => { - derrive_base_encoding! ($type, $encoding); - derrive_base_encoding!($($type2, $encoding2);+); + (#[$doc:meta] $type:ident, $encoding:expr; $(#[$doc2:meta] $type2:ident, $encoding2:expr;)+) => { + derrive_base_encoding! (#[$doc] $type, $encoding;); + derrive_base_encoding! ($(#[$doc2] $type2, $encoding2;)+); }; } macro_rules! derrive_base_x { - ($type:ident, $encoding:expr) => { + (#[$doc:meta] $type:ident, $encoding:expr;) => { + + #[$doc] #[derive(PartialEq, Eq, Clone, Copy, Debug)] pub(crate) struct $type; @@ -37,9 +41,9 @@ macro_rules! derrive_base_x { } } }; - ($type:ident, $encoding:expr; $($type2:ident, $encoding2:expr);+) => { - derrive_base_x! ($type, $encoding); - derrive_base_x!($($type2, $encoding2);+); + (#[$doc:meta] $type:ident, $encoding:expr; $(#[$doc2:meta] $type2:ident, $encoding2:expr;)+) => { + derrive_base_x! (#[$doc] $type, $encoding;); + derrive_base_x! ($(#[$doc2] $type2, $encoding2;)+); }; } @@ -66,71 +70,51 @@ impl BaseCodec for Identity { } derrive_base_encoding! { + /// Base2 (alphabet: 01). Base2, encoding::BASE2; + /// Base8 (alphabet: 01234567). Base8, encoding::BASE8; + /// Base16 lower hexadecimal (alphabet: 0123456789abcdef). Base16Lower, encoding::BASE16_LOWER; + /// Base16 upper hexadecimal (alphabet: 0123456789ABCDEF). Base16Upper, encoding::BASE16_UPPER; + /// Base32, rfc4648 no padding (alphabet: abcdefghijklmnopqrstuvwxyz234567). Base32Lower, encoding::BASE32_NOPAD_LOWER; + /// Base32, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567). Base32Upper, encoding::BASE32_NOPAD_UPPER; + /// Base32, rfc4648 with padding (alphabet: abcdefghijklmnopqrstuvwxyz234567). Base32PadLower, encoding::BASE32_PAD_LOWER; + /// Base32, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567). Base32PadUpper, encoding::BASE32_PAD_UPPER; + /// Base32hex, rfc4648 no padding (alphabet: 0123456789abcdefghijklmnopqrstuv). Base32HexLower, encoding::BASE32HEX_NOPAD_LOWER; + /// Base32hex, rfc4648 no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV). Base32HexUpper, encoding::BASE32HEX_NOPAD_UPPER; + /// Base32hex, rfc4648 with padding (alphabet: 0123456789abcdefghijklmnopqrstuv). Base32HexPadLower, encoding::BASE32HEX_PAD_LOWER; + /// Base32hex, rfc4648 with padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV). Base32HexPadUpper, encoding::BASE32HEX_PAD_UPPER; + /// z-base-32 (used by Tahoe-LAFS) (alphabet: ybndrfg8ejkmcpqxot1uwisza345h769). Base32Z, encoding::BASE32Z; + /// Base64, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/). Base64, encoding::BASE64_NOPAD; + /// Base64, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/). Base64Pad, encoding::BASE64_PAD; + /// Base64 url, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_). Base64Url, encoding::BASE64URL_NOPAD; - Base64UrlPad, encoding::BASE64URL_PAD + /// Base64 url, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_). + Base64UrlPad, encoding::BASE64URL_PAD; } derrive_base_x! { + /// Base10 (alphabet: 0123456789). Base10, encoding::BASE10; + /// Base58 flicker (alphabet: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ). Base58Flickr, encoding::BASE58_FLICKR; - Base58Btc, encoding::BASE58_BITCOIN + /// Base58 bitcoin (alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz). + Base58Btc, encoding::BASE58_BITCOIN; } -/// Base2 (alphabet: 01). - -/// Base8 (alphabet: 01234567). - -/// Base10 (alphabet: 0123456789). - -/// Base16 lower hexadecimal (alphabet: 0123456789abcdef). - -/// Base16 upper hexadecimal (alphabet: 0123456789ABCDEF). - -/// Base32, rfc4648 no padding (alphabet: abcdefghijklmnopqrstuvwxyz234567). - -/// Base32, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567). - -/// Base32, rfc4648 with padding (alphabet: abcdefghijklmnopqrstuvwxyz234567). - -/// Base32, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZ234567). - -/// Base32hex, rfc4648 no padding (alphabet: 0123456789abcdefghijklmnopqrstuv). - -/// Base32hex, rfc4648 no padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV). - -/// Base32hex, rfc4648 with padding (alphabet: 0123456789abcdefghijklmnopqrstuv). - -/// Base32hex, rfc4648 with padding (alphabet: 0123456789ABCDEFGHIJKLMNOPQRSTUV). - -/// z-base-32 (used by Tahoe-LAFS) (alphabet: ybndrfg8ejkmcpqxot1uwisza345h769). - -/// Base58 flicker (alphabet: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ). - -/// Base58 bitcoin (alphabet: 123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz). - -/// Base64, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/). - -/// Base64, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/). - -/// Base64 url, rfc4648 no padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_). - -/// Base64 url, rfc4648 with padding (alphabet: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_). - #[cfg(test)] mod tests { use super::*; From 2307044f3476d7eb610b8f8942538f7e74ab7811 Mon Sep 17 00:00:00 2001 From: mriise Date: Fri, 30 Oct 2020 17:30:37 -0700 Subject: [PATCH 03/15] remove unneeded macro steps, fix spelling --- src/impls.rs | 74 ++++++++++++++++++++++++---------------------------- 1 file changed, 34 insertions(+), 40 deletions(-) diff --git a/src/impls.rs b/src/impls.rs index d87ba6b..1461c6c 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -1,49 +1,43 @@ use crate::encoding; use crate::error::Result; -macro_rules! derrive_base_encoding { - (#[$doc:meta] $type:ident, $encoding:expr;) => { - - #[$doc] - #[derive(PartialEq, Eq, Clone, Copy, Debug)] - pub(crate) struct $type; - - impl BaseCodec for $type { - fn encode>(input: I) -> String { - $encoding.encode(input.as_ref()) +macro_rules! derive_base_encoding { + ( $(#[$doc:meta] $type:ident, $encoding:expr;)* ) => { + $( + #[$doc] + #[derive(PartialEq, Eq, Clone, Copy, Debug)] + pub(crate) struct $type; + + impl BaseCodec for $type { + fn encode>(input: I) -> String { + $encoding.encode(input.as_ref()) + } + + fn decode>(input: I) -> Result> { + Ok($encoding.decode(input.as_ref().as_bytes())?) + } } - - fn decode>(input: I) -> Result> { - Ok($encoding.decode(input.as_ref().as_bytes())?) - } - } - }; - (#[$doc:meta] $type:ident, $encoding:expr; $(#[$doc2:meta] $type2:ident, $encoding2:expr;)+) => { - derrive_base_encoding! (#[$doc] $type, $encoding;); - derrive_base_encoding! ($(#[$doc2] $type2, $encoding2;)+); + )* }; } -macro_rules! derrive_base_x { - (#[$doc:meta] $type:ident, $encoding:expr;) => { - - #[$doc] - #[derive(PartialEq, Eq, Clone, Copy, Debug)] - pub(crate) struct $type; - - impl BaseCodec for $type { - fn encode>(input: I) -> String { - base_x::encode($encoding, input.as_ref()) +macro_rules! derive_base_x { + ( $(#[$doc:meta] $type:ident, $encoding:expr;)* ) => { + $( + #[$doc] + #[derive(PartialEq, Eq, Clone, Copy, Debug)] + pub(crate) struct $type; + + impl BaseCodec for $type { + fn encode>(input: I) -> String { + base_x::encode($encoding, input.as_ref()) + } + + fn decode>(input: I) -> Result> { + Ok(base_x::decode($encoding, input.as_ref())?) + } } - - fn decode>(input: I) -> Result> { - Ok(base_x::decode($encoding, input.as_ref())?) - } - } - }; - (#[$doc:meta] $type:ident, $encoding:expr; $(#[$doc2:meta] $type2:ident, $encoding2:expr;)+) => { - derrive_base_x! (#[$doc] $type, $encoding;); - derrive_base_x! ($(#[$doc2] $type2, $encoding2;)+); + )* }; } @@ -69,7 +63,7 @@ impl BaseCodec for Identity { } } -derrive_base_encoding! { +derive_base_encoding! { /// Base2 (alphabet: 01). Base2, encoding::BASE2; /// Base8 (alphabet: 01234567). @@ -106,7 +100,7 @@ derrive_base_encoding! { Base64UrlPad, encoding::BASE64URL_PAD; } -derrive_base_x! { +derive_base_x! { /// Base10 (alphabet: 0123456789). Base10, encoding::BASE10; /// Base58 flicker (alphabet: 123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ). From 67902c79111b8cba9f63c0fb686514e50400fe74 Mon Sep 17 00:00:00 2001 From: mriise Date: Mon, 2 Nov 2020 06:55:31 -0800 Subject: [PATCH 04/15] implement no_std --- .cargo/config.toml | 2 ++ Cargo.toml | 13 ++++++++++--- src/base.rs | 3 +++ src/encoding.rs | 2 +- src/error.rs | 6 +++++- src/impls.rs | 3 +++ src/lib.rs | 7 +++++++ 7 files changed, 31 insertions(+), 5 deletions(-) create mode 100644 .cargo/config.toml diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 0000000..fa2f1bf --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,2 @@ +[unstable] +features = ["host_dep"] diff --git a/Cargo.toml b/Cargo.toml index 9f4c2bc..1f4c35d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,15 +10,22 @@ homepage = "https://github.com/multiformats/rust-multibase" repository = "https://github.com/multiformats/rust-multibase" keywords = ["ipld", "ipfs", "multihash", "multibase", "cid"] +[features] +default = ["std"] +std = [] + [dependencies] -base-x = "0.2" -data-encoding = "2.2" -data-encoding-macro = "0.1.8" +base-x = { git = "https://github.com/whalelephant/base-x-rs", branch = "no_std", default-features = false } +data-encoding = { version = "2.3.1", default-features = false, features = ["alloc"] } +data-encoding-macro = "0.1.9" [dev-dependencies] criterion = "0.3" rand = "0.7" +# [crates-io.patch] +# base-x = { git = "https://github.com/whalelephant/base-x-rs", branch = "no_std", default-features = false } + [[bench]] name = "multibase" harness = false diff --git a/src/base.rs b/src/base.rs index c783c8c..c6a734b 100644 --- a/src/base.rs +++ b/src/base.rs @@ -1,6 +1,9 @@ use crate::error::{Error, Result}; use crate::impls::*; +#[cfg(not(feature = "std"))] +use alloc::{string::String, vec::Vec}; + macro_rules! build_base_enum { ( $(#[$attr:meta] $code:expr => $base:ident,)* ) => { /// List of types currently supported in the multibase spec. diff --git a/src/encoding.rs b/src/encoding.rs index e92943f..9f57a6a 100644 --- a/src/encoding.rs +++ b/src/encoding.rs @@ -1,5 +1,5 @@ use data_encoding::Encoding; -use data_encoding_macro::{internal_new_encoding, new_encoding}; +use data_encoding_macro::new_encoding; // Base2 (alphabet: 01) pub const BASE2: Encoding = new_encoding! { diff --git a/src/error.rs b/src/error.rs index a108578..47582db 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,8 @@ -use std::{error, fmt}; +#[cfg(feature = "std")] +use std::error; +use core::fmt; +#[cfg(feature = "std")] /// Type alias to use this library's [`Error`] type in a `Result`. pub type Result = std::result::Result; @@ -21,6 +24,7 @@ impl fmt::Display for Error { } } +#[cfg(feature = "std")] impl error::Error for Error {} impl From for Error { diff --git a/src/impls.rs b/src/impls.rs index cd52a44..0b0e762 100644 --- a/src/impls.rs +++ b/src/impls.rs @@ -1,6 +1,9 @@ use crate::encoding; use crate::error::Result; +#[cfg(not(feature = "std"))] +use alloc::{string::String, vec::Vec}; + macro_rules! derive_base_encoding { ( $(#[$doc:meta] $type:ident, $encoding:expr;)* ) => { $( diff --git a/src/lib.rs b/src/lib.rs index 01ca939..3b7960d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,13 @@ //! Implementation of [multibase](https://github.com/multiformats/multibase) in Rust. #![deny(missing_docs)] +#![cfg_attr(not(feature = "std"), no_std)] + +#[cfg(not(feature = "std"))] +extern crate alloc; + +#[cfg(not(feature = "std"))] +use alloc::{string::String, vec::Vec}; mod base; mod encoding; From baf439922231e6a959dead176c8ed35682e453bb Mon Sep 17 00:00:00 2001 From: mriise Date: Mon, 2 Nov 2020 07:05:39 -0800 Subject: [PATCH 05/15] fix error types --- src/error.rs | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/error.rs b/src/error.rs index 47582db..0c18c1b 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,10 +1,7 @@ -#[cfg(feature = "std")] -use std::error; use core::fmt; -#[cfg(feature = "std")] /// Type alias to use this library's [`Error`] type in a `Result`. -pub type Result = std::result::Result; +pub type Result = core::result::Result; /// Error types #[derive(PartialEq, Eq, Clone, Debug)] @@ -25,7 +22,7 @@ impl fmt::Display for Error { } #[cfg(feature = "std")] -impl error::Error for Error {} +impl std::error::Error for Error {} impl From for Error { fn from(_: base_x::DecodeError) -> Self { From 781ee0de4793221fb27e1571bc022beafe5f3cfd Mon Sep 17 00:00:00 2001 From: mriise Date: Mon, 2 Nov 2020 07:09:59 -0800 Subject: [PATCH 06/15] remove commented text from Cargo.toml --- Cargo.toml | 3 --- 1 file changed, 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1f4c35d..828ea52 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,9 +23,6 @@ data-encoding-macro = "0.1.9" criterion = "0.3" rand = "0.7" -# [crates-io.patch] -# base-x = { git = "https://github.com/whalelephant/base-x-rs", branch = "no_std", default-features = false } - [[bench]] name = "multibase" harness = false From f21983a3ef14a65086a5852f046213fe1d4bac96 Mon Sep 17 00:00:00 2001 From: Mark Riise Date: Mon, 2 Nov 2020 10:06:43 -0800 Subject: [PATCH 07/15] Update build.yml --- .github/workflows/build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a56bc39..314115c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -57,7 +57,6 @@ jobs: - name: Run cargo-tarpaulin uses: actions-rs/tarpaulin@v0.1 with: - version: '0.11.0' args: '-- --test-threads 1' - name: Upload to codecov.io From 96edb64162c26319a7ba092fafebcda79a146f07 Mon Sep 17 00:00:00 2001 From: mriise Date: Fri, 6 Nov 2020 01:52:58 -0800 Subject: [PATCH 08/15] update README & base_x --- Cargo.toml | 6 +++--- README.md | 10 +++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 828ea52..6d0033a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,14 +8,14 @@ readme = "README.md" description = "multibase in rust" homepage = "https://github.com/multiformats/rust-multibase" repository = "https://github.com/multiformats/rust-multibase" -keywords = ["ipld", "ipfs", "multihash", "multibase", "cid"] +keywords = ["ipld", "ipfs", "multihash", "multibase", "cid", "no_std"] [features] default = ["std"] -std = [] +std = ["data-encoding/std"] [dependencies] -base-x = { git = "https://github.com/whalelephant/base-x-rs", branch = "no_std", default-features = false } +base-x = { version = "0.2.6", default-features = false } data-encoding = { version = "2.3.1", default-features = false, features = ["alloc"] } data-encoding-macro = "0.1.9" diff --git a/README.md b/README.md index a6b5748..aef2e9c 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,17 @@ First add this to your `Cargo.toml` ```toml [dependencies] -multibase = "0.8" +multibase = "0.9" ``` +For `no_std` +``` +[dependencies] +multibase = { version ="0.9", default-features = false } +``` + +**note**: This crate relies on the [currently unstable](rust-lang/cargo#7915) `host_dep` feature to [compile proc macros with the proper dependencies](https://docs.rs/data-encoding-macro/0.1.10/data_encoding_macro/). + Then run `cargo build`. ## Usage From c86b266ed1d9fdd6ee0b9c0dbcd512ee4d7e18b9 Mon Sep 17 00:00:00 2001 From: mriise Date: Fri, 6 Nov 2020 02:08:21 -0800 Subject: [PATCH 09/15] bump base-x --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 6d0033a..c7d1c66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ default = ["std"] std = ["data-encoding/std"] [dependencies] -base-x = { version = "0.2.6", default-features = false } +base-x = { version = "0.2.7", default-features = false } data-encoding = { version = "2.3.1", default-features = false, features = ["alloc"] } data-encoding-macro = "0.1.9" From 8c17c359ce1568e4bd4c635182c63fb91d6795b7 Mon Sep 17 00:00:00 2001 From: mriise Date: Fri, 6 Nov 2020 02:31:30 -0800 Subject: [PATCH 10/15] add no_std to CI --- .github/workflows/build.yml | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 314115c..43f1aed 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,6 +40,18 @@ jobs: with: command: test + ensure_no_std: + name: Ensure no_std + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + - name: Build thumbv6m-none-eabi + run: cargo build --target thumbv6m-none-eabi --no-default-feature + coverage: name: Code Coverage runs-on: ubuntu-latest From 89914dc27daf5b0b71210376e5f89a1586312157 Mon Sep 17 00:00:00 2001 From: mriise Date: Fri, 6 Nov 2020 02:55:45 -0800 Subject: [PATCH 11/15] fix Readme --- .github/workflows/build.yml | 22 +++++++++++----------- README.md | 2 +- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 43f1aed..a0730e9 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -40,17 +40,17 @@ jobs: with: command: test - ensure_no_std: - name: Ensure no_std - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v1 - - uses: actions-rs/toolchain@v1 - with: - toolchain: stable - override: true - - name: Build thumbv6m-none-eabi - run: cargo build --target thumbv6m-none-eabi --no-default-feature + ensure_no_std: + name: Ensure no_std + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v1 + - uses: actions-rs/toolchain@v1 + with: + toolchain: stable + override: true + - name: Build thumbv6m-none-eabi + run: cargo build --target thumbv6m-none-eabi --no-default-feature coverage: name: Code Coverage diff --git a/README.md b/README.md index aef2e9c..0c3aac7 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ For `no_std` multibase = { version ="0.9", default-features = false } ``` -**note**: This crate relies on the [currently unstable](rust-lang/cargo#7915) `host_dep` feature to [compile proc macros with the proper dependencies](https://docs.rs/data-encoding-macro/0.1.10/data_encoding_macro/). +**note**: This crate relies on the [currently unstable](https://github.com/rust-lang/cargo/issues/7915) `host_dep` feature to [compile proc macros with the proper dependencies](https://docs.rs/data-encoding-macro/0.1.10/data_encoding_macro/), thus **requiring nightly rustc** to use. Then run `cargo build`. From 9fcc9b53f785022493bbca93964395bb96a41d3d Mon Sep 17 00:00:00 2001 From: mriise Date: Fri, 6 Nov 2020 03:23:45 -0800 Subject: [PATCH 12/15] spelling --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index a0730e9..d726a6d 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,10 +47,10 @@ jobs: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: - toolchain: stable + toolchain: nightly override: true - name: Build thumbv6m-none-eabi - run: cargo build --target thumbv6m-none-eabi --no-default-feature + run: cargo build --target thumbv6m-none-eabi --no-default-features coverage: name: Code Coverage From 1a37f2cbaddd1619aee067760c13cec36b2a0bf3 Mon Sep 17 00:00:00 2001 From: mriise Date: Fri, 6 Nov 2020 03:27:08 -0800 Subject: [PATCH 13/15] test CI --- .github/workflows/build.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index d726a6d..8a03095 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -47,7 +47,7 @@ jobs: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: - toolchain: nightly + toolchain: thumbv6m-none-eabi-nightly override: true - name: Build thumbv6m-none-eabi run: cargo build --target thumbv6m-none-eabi --no-default-features From 403712fa744f86b4d5fdc9f0b384078d58e80003 Mon Sep 17 00:00:00 2001 From: mriise Date: Fri, 6 Nov 2020 03:28:48 -0800 Subject: [PATCH 14/15] test CI --- .github/workflows/build.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8a03095..20cef15 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -42,12 +42,12 @@ jobs: ensure_no_std: name: Ensure no_std - runs-on: ubuntu-latest + runs-on: thumbv6m-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: - toolchain: thumbv6m-none-eabi-nightly + toolchain: nightly override: true - name: Build thumbv6m-none-eabi run: cargo build --target thumbv6m-none-eabi --no-default-features From 393c2997c8528fe6cffdff04a6bfee93ff4eb150 Mon Sep 17 00:00:00 2001 From: mriise Date: Fri, 6 Nov 2020 03:43:19 -0800 Subject: [PATCH 15/15] add target to CI --- .github/workflows/build.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 20cef15..a8caaa6 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -42,13 +42,16 @@ jobs: ensure_no_std: name: Ensure no_std - runs-on: thumbv6m-latest + runs-on: ubuntu-latest steps: - uses: actions/checkout@v1 - uses: actions-rs/toolchain@v1 with: + profile: minimal toolchain: nightly override: true + - name: Install thumbv6m-none-eabi + run: rustup target add thumbv6m-none-eabi - name: Build thumbv6m-none-eabi run: cargo build --target thumbv6m-none-eabi --no-default-features