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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
use buffered_reader::{
    BufferedReader, BufferedReaderGeneric, BufferedReaderMemory,
    BufferedReaderFile,
};
use std::io;
use std::path::Path;

use CTB;
use Result;
use armor;

/// Transparently strips ASCII Armor.
///
/// This reader detects and strips ASCII Armor.
pub struct Reader<'a>(Box<'a + BufferedReader<()>>);

impl<'a> Reader<'a> {
    fn from_buffered_reader(mut br: Box<'a + BufferedReader<()>>)
                            -> Result<Self> {
        let ptag = br.data_hard(1)?[0];
        if let Ok(_) = CTB::from_ptag(ptag) {
            Ok(Reader(br))
        } else {
            Ok(Reader(Box::new(BufferedReaderGeneric::new(
                armor::Reader::from_buffered_reader(br, None),
                None))))
        }
    }

    /// Creates a `Reader` from an `io::Read`er.
    pub fn from_reader<R>(reader: R) -> Result<Self>
        where R: io::Read + 'a
    {
        Self::from_buffered_reader(
            Box::new(BufferedReaderGeneric::new(reader, None)))
    }

    /// Creates a `Reader` from a file.
    pub fn from_file<P>(path: P) -> Result<Self>
        where P: AsRef<Path>
    {
        Self::from_buffered_reader(Box::new(BufferedReaderFile::open(path)?))
    }

    /// Creates a `Reader` from a buffer.
    pub fn from_bytes(bytes: &'a [u8]) -> Result<Self> {
        Self::from_buffered_reader(
            Box::new(BufferedReaderMemory::new(bytes)))
    }
}

impl<'a> io::Read for Reader<'a> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        self.0.read(buf)
    }
}