Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement stack-based display and multibase encoding #120

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ readme = "README.md"
edition = "2018"

[features]
default = ["std", "multihash/default"]
default = [ "multibase"]
std = ["multihash/std", "unsigned-varint/std", "alloc", "multibase/std"]
alloc = ["multibase", "multihash/alloc", "core2/alloc", "serde/alloc"]
alloc = ["multibase/alloc", "multihash/alloc", "core2/alloc", "serde/alloc"]
arb = ["quickcheck", "rand", "multihash/arb", "multihash/sha2", "arbitrary"]
scale-codec = ["parity-scale-codec", "multihash/scale-codec"]
serde-codec = ["alloc", "serde", "multihash/serde-codec", "serde_bytes"]
Expand All @@ -21,7 +21,7 @@ serde-codec = ["alloc", "serde", "multihash/serde-codec", "serde_bytes"]
multihash = { version = "0.16.2", default-features = false }
unsigned-varint = { version = "0.7.0", default-features = false }

multibase = { version = "0.9.1", optional = true, default-features = false }
multibase = { version = "0.9.1", optional = true, default-features = false, git = "https://github.com/mriise/rust-multibase", branch = "stack" }
parity-scale-codec = { version = "3.0.0", default-features = false, features = ["derive"], optional = true }
quickcheck = { version = "0.9.2", optional = true }
rand = { version = "0.7.3", optional = true }
Expand Down
70 changes: 70 additions & 0 deletions src/cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! import the `Cid` type from this module.
use core::convert::TryFrom;

use multibase::{StackBase, StackVec, StackString};
#[cfg(feature = "alloc")]
use multibase::{encode as base_encode, Base};

Expand Down Expand Up @@ -182,6 +183,26 @@ impl<const S: usize> Cid<S> {
multibase::encode(Base::Base32Lower, self.to_bytes().as_slice())
}

fn to_stack_string_v0<const STR: usize>(&self) -> StackString<STR> {
let mut vec = StackVec::<u8, STR>::default();
{
let buf: &mut [u8] = &mut vec;
// unwraps because it assumes const was written to be generic over base + CID and thus should be correctly sized
self.hash.write(buf).unwrap();
}
StackBase::<0, STR>::Base58BtcS.encode(vec).unwrap()
}

fn to_stack_string_v1<const STR: usize>(&self) -> StackString<STR> {
let mut vec = StackVec::<u8, STR>::default();
{
let buf: &mut [u8] = &mut vec;
// unwraps because it assumes const was written to be generic over base + CID and thus should be correctly sized
self.write_bytes_v1(buf).unwrap();
}
StackBase::<0, STR>::Base32Lower.encode(vec).unwrap()
}

/// Convert CID into a multibase encoded string
///
/// # Example
Expand Down Expand Up @@ -222,6 +243,55 @@ impl<const S: usize> Default for Cid<S> {
}
}

#[cfg(not(feature = "alloc"))]
/// takes a numerator of a fraction (magic number!)
/// and the input size to calculate the expected output size
///
/// adds the buffer sizes of varint
///
/// 58: 136566
/// 10: 240824
const fn encoded_size(size: usize, num: usize) -> usize {
let version_size = 1;
let codec_size = 4;
(size * num / 100000) + 1 + version_size + codec_size
}

#[cfg(not(feature = "alloc"))]
macro_rules! stack_display {
($($size:literal,)*) => {
$(
impl core::fmt::Display for Cid<$size> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {

match self.version {
Version::V0 => write!(f, "{}", self.to_stack_string_v0::<{encoded_size($size, 136566)}>()),
Version::V1 => write!(f, "{}", self.to_stack_string_v1::<{encoded_size($size, 240824)}>()),
}
}
}

impl core::fmt::Debug for Cid<$size> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if f.alternate() {
f.debug_struct("Cid")
.field("version", &self.version())
.field("codec", &self.codec())
.field("hash", self.hash())
.finish()
} else {
write!(f, "Cid({})", self)
}
}
}

)*
};
}

#[cfg(not(feature = "alloc"))]
stack_display!(32, 64, 128, 256, 512, 1024, 2048,);

// TODO: remove the dependency on alloc by fixing
// https://github.com/multiformats/rust-multibase/issues/33
#[cfg(feature = "alloc")]
Expand Down
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub use self::cid::Cid as CidGeneric;
pub use self::error::{Error, Result};
pub use self::version::Version;

#[cfg(feature = "alloc")]
pub use multibase;
pub use multihash;

Expand Down