use std::fmt;
use std::slice;
use std::vec;
use std::io;
use std::path::Path;
use std::fs::File;
use buffered_reader::BufferedReader;
use buffered_reader::BufferedReaderGeneric;
use buffered_reader::BufferedReaderMemory;
use Result;
use Error;
use Packet;
use packet::{Container, PacketIter};
use PacketPile;
use parse::PacketParserResult;
use parse::PacketParserBuilder;
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 PacketPile {
/// Turns a vector of [`Packet`s] into a `PacketPile`.
///
/// This is a simple wrapper function; it does not process the
/// packets in any way.
///
/// [`Packet`s]: enum.Packet.html
pub fn from_packets(p: Vec<Packet>) -> Self {
PacketPile { top_level: Container { packets: p } }
}
/// Turns a [`Packet`] into a `PacketPile`.
///
/// This is a simple wrapper function; it does not process the
/// packets in any way.
///
/// [`Packet`]: enum.Packet.html
pub fn from_packet(p: Packet) -> Self {
let mut top_level = Vec::with_capacity(1);
top_level.push(p);
Self::from_packets(top_level)
}
/// Pretty prints the message to stderr.
///
/// This function is primarily intended for debugging purposes.
pub fn pretty_print(&self) {
self.top_level.pretty_print(0);
}
/// Returns a reference to the packet at the location described by
/// `pathspec`.
///
/// `pathspec` is a slice of the form `[ 0, 1, 2 ]`. Each element
/// is the index of packet in a container. Thus, the previous
/// path specification means: return the third child of the second
/// child of the first top-level packet. In other words, the
/// starred packet in the following tree:
///
/// ```text
/// PacketPile
/// / | \
/// 0 1 2 ...
/// / \
/// / \
/// 0 1 ...
/// / | \ ...
/// 0 1 2
/// *
/// ```
///
/// And, `[ 10 ]` means return the 11th top-level packet.
///
/// Note: there is no packet at the root. Thus, the path `[]`
/// returns None.
pub fn path_ref(&self, pathspec: &[usize]) -> Option<&Packet> {
let mut packet : Option<&Packet> = None;
let mut cont = Some(&self.top_level);
for i in pathspec {
if let Some(ref c) = cont.take() {
if *i < c.packets.len() {
let