Skip to content

Commit

Permalink
feat: replace Codec::from(u64) with TryFrom impl (#44)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: use `Codec::try_from()` instead of `Codec::from()`
  • Loading branch information
sunny-g committed Mar 9, 2020
1 parent f220c06 commit ef94e40
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/cid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl TryFrom<&[u8]> for Cid {
let version = Version::try_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
2 changes: 1 addition & 1 deletion src/prefix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Prefix {
let version = Version::try_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

0 comments on commit ef94e40

Please sign in to comment.