1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::Path;

use crate::errors::BencodeResult;
use crate::protocol::decode;
use crate::types::Type;

pub struct Decoder;

impl Decoder {
    pub fn decode<I>(it: &mut I) -> BencodeResult<Type>
    where
        I: Iterator<Item = u8>
    {
        match decode(it) {
            Err(err) => Err(err.into()),
            Ok(t) => Ok(t),
        }
    }

    pub fn decode_from<P>(path: P) -> BencodeResult<Type>
    where
        P: AsRef<Path>
    {
        let file = File::open(path)?;
        let mut reader = BufReader::new(file).bytes().map(|c| c.unwrap());
        let decoded = decode(&mut reader)?;

        Ok(decoded)
    }
}