Conversion of Haskell CBOR decoding code into Rust I was interested in converting the following Haskell code, that does decoding of CBOR into Rust:

module Main where

import Data.Word
import Data.Bits
import Control.Monad.Trans.Except

data Value = I Int deriving (Show)

read_int xs size =
    f xs size 0
    where
        f _  0 acc     = return (I acc)
        f [] _ acc     = throwE ()
        f (x:xs) n acc = f xs (n - 1) (acc `shiftL` 8 .|. fromIntegral x)

cbor_decode (x:xs)
    | x &gt;= 0x00 &amp;&amp; x &lt;= 0x17 = return (I (fromIntegral x))
    | x &gt;= 0x18 &amp;&amp; x &lt;= 0x1b = let v = fromIntegral x - 0x17 in read_int xs v
    | otherwise = throwE ()

main :: IO ()
main = do
    let x = [0x1a, 1, 0, 0] :: [Word8]
    let y = runExcept (cbor_decode x) :: Either () Value
    putStrLn $ show $ y


I ended up with the following:

use Value::*;

#[derive(Debug)]
enum Value {
    I(i64),
}

fn read_int(buf: &amp;[u8], size: usize) -&gt; Result&lt;Value, ()&gt; {
    fn f(buf: &amp;[u8], size: usize, acc: i64) -&gt; Result&lt;Value, ()&gt; {
        if size == 0 {
            Ok(I(acc))
        } else if buf.len() == 0 {
            Err(())
        } else {
            f(&amp;buf[1..], size - 1, acc &lt;&lt; 8 | buf[0] as i64)
        }
    }
    f(buf, size, 0)
}

fn cbor_decode(buf: &amp;[u8]) -&gt; Result&lt;Value, ()&gt; {
    match buf[0] {
        0x00 ... 0x17 =&gt; Ok(I(buf[0] as i64)),
        0x18 ... 0x1b =&gt; read_int(&amp;buf[1..], (buf[0] - 0x17) as usize),
        _             =&gt; Err(()),
    }
}

fn main() {
    let x = vec!(0x1a, 1, 0, 0);
    let y = cbor_decode(&amp;x);
    println!("{:?}", y);

}


Is this considered good Rust?
I'd expect some kind of error type to describe what the problem was. There's at least two types of error I see, but the user cannot distinguish between them.
Rust does not perform tail-call optimization, so it's usually better to write things iteratively, especially when there's no fixed bound.
Using is_empty is more immediately obvious as to the desired behavior.
It seems very strange to read arbitrary integer lengths. Since the size is a usize, that means you could read an integer that takes 4 billion bytes or more! This is especially strange considering you can only return an i64.
Would encode the unit of size somehow. The easiest is to add _in_bytes to the argument name.
The vec! macro idiomatically uses square brackets to look like arrays.
There's no reason to allocate a vector here anyway; an array works fine.




use Value::*;

#[derive(Debug)]
enum Value {
    I(i64),
}

#[derive(Debug)]
enum Error {
    NotEnoughData,
    InvalidInteger,
}

fn read_int(mut buf: &amp;[u8], size_in_bytes: usize) -&gt; Result&lt;Value, Error&gt; {
    let mut acc = 0;

    for _ in 0..size_in_bytes {
        if buf.is_empty() { return Err(Error::NotEnoughData) }
        acc &lt;&lt;= 8;
        acc |= buf[0] as i64;
        buf = &amp;buf[1..];
    }

    Ok(I(acc))
}

fn cbor_decode(buf: &amp;[u8]) -&gt; Result&lt;Value, Error&gt; {
    match buf[0] {
        0x00 ... 0x17 =&gt; Ok(I(buf[0] as i64)),
        0x18 ... 0x1b =&gt; read_int(&amp;buf[1..], (buf[0] - 0x17) as usize),
        _             =&gt; Err(Error::InvalidInteger),
    }
}

fn main() {
    let x = [0x1a, 1, 0, 0];
    let y = cbor_decode(&amp;x);
    println!("{:?}", y);
}


A good programming practice is to not reimplement things for no reason. To that end, there's already crates for reading numbers out of byte slices:

extern crate byteorder;

use byteorder::{ByteOrder, BigEndian};

fn read_int(buf: &amp;[u8], size_in_bytes: usize) -&gt; Result&lt;Value, Error&gt; {
    if buf.len() &lt; size_in_bytes {
        Err(Error::NotEnoughData)
    } else {
        Ok(I(BigEndian::read_int(buf, size_in_bytes)))
    }
}


Even better, there's already crates for reading and writing CBOR data, such as serde_cbor. This plugs in to the de facto Rust serialization library, allowing you to operate at a higher level, defining structs that can be transformed to and from CBOR automatically.