Skip to content

Commit ef94e40

Browse files
authored
feat: replace Codec::from(u64) with TryFrom impl (#44)
BREAKING CHANGE: use `Codec::try_from()` instead of `Codec::from()`
1 parent f220c06 commit ef94e40

3 files changed

Lines changed: 8 additions & 4 deletions

File tree

src/cid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ impl TryFrom<&[u8]> for Cid {
207207
let version = Version::try_from(raw_version)?;
208208

209209
let (raw_codec, hash) = varint_decode::u64(&remain)?;
210-
let codec = Codec::from(raw_codec)?;
210+
let codec = Codec::try_from(raw_codec)?;
211211

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

src/codec.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::convert::TryFrom;
2+
13
use crate::error::{Error, Result};
24

35
macro_rules! build_codec_enum {
@@ -8,9 +10,11 @@ macro_rules! build_codec_enum {
810
$( #[$attr] $codec, )*
911
}
1012

11-
impl Codec {
13+
impl TryFrom<u64> for Codec {
14+
type Error = Error;
15+
1216
/// Convert a number to the matching codec, or `Error` if unknown codec is matching.
13-
pub fn from(raw: u64) -> Result<Codec> {
17+
fn try_from(raw: u64) -> Result<Codec> {
1418
match raw {
1519
$( $code => Ok(Self::$codec), )*
1620
_ => Err(Error::UnknownCodec),

src/prefix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ impl Prefix {
2626
let version = Version::try_from(raw_version)?;
2727

2828
let (raw_codec, remain) = varint_decode::u64(remain)?;
29-
let codec = Codec::from(raw_codec)?;
29+
let codec = Codec::try_from(raw_codec)?;
3030

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

0 commit comments

Comments
 (0)