use std::fmt;
use std::slice;
use std::vec;
use std::io;
use std::path::Path;
use buffered_reader::BufferedReader;
use Result;
use Error;
use Packet;
use packet::{Container, PacketIter};
use PacketPile;
use parse::PacketParserResult;
use parse::PacketParserBuilder;
use parse::Parse;
use parse::Cookie;
#[cfg(test)]
macro_rules! bytes {
( $x:expr ) => { include_bytes!(concat!("../tests/data/messages/", $x)) };
}
#[cfg(test)]
use std::path::PathBuf;
#[cfg(test)]
#[allow(dead_code)]
fn path_to(artifact: &str) -> PathBuf {
[env!("CARGO_MANIFEST_DIR"), "tests", "data", "messages", artifact]
.iter().collect()
}
impl fmt::Debug for PacketPile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PacketPile")
.field("packets", &self.top_level.packets)
.finish()
}
}
impl<'a> Parse<'a, PacketPile> for PacketPile {
/// Deserializes the OpenPGP message stored in a `std::io::Read`
/// object.
///
/// Although this method is easier to use to parse a sequence of
/// OpenPGP packets than a [`PacketParser`] or a
/// [`PacketPileParser`], this interface buffers the whole message
/// in memory. Thus, the caller must be certain that the
/// *deserialized* message is not too large.
///
/// Note: this interface *does* buffer the contents of packets.
///
/// [`PacketParser`]: parse/struct.PacketParser.html
/// [`PacketPileParser`]: parse/struct.PacketPileParser.html
fn from_reader<R: 'a + io::Read>(reader: R) -> Result<PacketPile> {
let bio = buffered_reader::Generic::with_cookie(
reader, None, Cookie::default());
PacketPile::from_buffered_reader(Box::new(bio))
}
/// Deserializes the OpenPGP message stored in the file named by
/// `path`.
///
/// See `from_reader` for more details and caveats.
fn from_file<P: AsRef<Path>>(path: P) -> Result<PacketPile> {
PacketPile::from_buffered_reader(
Box::new(buffered_reader::File::with_cookie(path, Cookie::default())?))
}
/// Deserializes the OpenPGP message stored in the provided buffer.
///
/// See `from_reader` for more details and caveats.
fn from_bytes(data: &'a [u8]) -> Result<PacketPile> {
let bio = buffered_reader::Memory::with_cookie(
data, Cookie::default());
PacketPile::from_buffered_reader(Box::new(bio))
}
}
impl From<Vec<Packet>> for PacketPile {
fn from(p: Vec<Packet>) -> Self {
PacketPile { top_level: Container { packets: p } }
}
}
impl From<Packet> for PacketPile {
fn from(p: Packet) -> Self {
Self::from(vec![p])
}
}
impl PacketPile {
/// Pretty prints the message to stderr.