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

replace Codec::from(u64) with TryFrom impl #44

Merged
merged 1 commit into from
Mar 9, 2020
Merged
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
2 changes: 1 addition & 1 deletion src/cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl TryFrom<&[u8]> for Cid {
let version = Version::from(raw_version)?;

let (raw_codec, hash) = varint_decode::u64(&remain)?;
let codec = Codec::from(raw_codec)?;
let codec = Codec::try_from(raw_codec)?;

let mh = MultihashRef::from_slice(hash)?.to_owned();

Expand Down
8 changes: 6 additions & 2 deletions src/codec.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

use crate::error::{Error, Result};

macro_rules! build_codec_enum {
Expand All @@ -8,9 +10,11 @@ macro_rules! build_codec_enum {
$( #[$attr] $codec, )*
}

impl Codec {
impl TryFrom<u64> for Codec {
type Error = Error;

/// Convert a number to the matching codec, or `Error` if unknown codec is matching.
pub fn from(raw: u64) -> Result<Codec> {
fn try_from(raw: u64) -> Result<Codec> {
match raw {
$( $code => Ok(Self::$codec), )*
_ => Err(Error::UnknownCodec),
Expand Down
4 changes: 3 additions & 1 deletion src/prefix.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

use unsigned_varint::{decode as varint_decode, encode as varint_encode};

use crate::codec::Codec;
Expand All @@ -24,7 +26,7 @@ impl Prefix {
let version = Version::from(raw_version)?;

let (raw_codec, remain) = varint_decode::u64(remain)?;
let codec = Codec::from(raw_codec)?;
let codec = Codec::try_from(raw_codec)?;

let (raw_mh_type, remain) = varint_decode::u64(remain)?;
let mh_type = match multihash::Code::from_u64(raw_mh_type) {
Expand Down