use std::path;
use std::vec;
use error;
use p4;
#[derive(Debug, Clone)]
pub struct WhereCommand<'p, 'f> {
connection: &'p p4::P4,
file: Vec<&'f str>,
}
impl<'p, 'f> WhereCommand<'p, 'f> {
pub fn new(connection: &'p p4::P4) -> Self {
Self {
connection,
file: vec![],
}
}
pub fn file(mut self, file: &'f str) -> Self {
self.file.push(file);
self
}
pub fn run(self) -> Result<Files, error::P4Error> {
let mut cmd = self.connection.connect_with_retries(None);
cmd.arg("where");
for file in self.file {
cmd.arg(file);
}
let data = cmd.output().map_err(|e| {
error::ErrorKind::SpawnFailed
.error()
.set_cause(e)
.set_context(format!("Command: {:?}", cmd))
})?;
let (_remains, (mut items, exit)) = where_parser::where_(&data.stdout).map_err(|_| {
error::ErrorKind::ParseFailed
.error()
.set_context(format!("Command: {:?}", cmd))
})?;
items.push(exit);
Ok(Files(items))
}
}
pub type FileItem = error::Item<File>;
pub struct Files(Vec<FileItem>);
impl IntoIterator for Files {
type Item = FileItem;
type IntoIter = FilesIntoIter;
fn into_iter(self) -> FilesIntoIter {
FilesIntoIter(self.0.into_iter())
}
}
#[derive(Debug)]
pub struct FilesIntoIter(vec::IntoIter<FileItem>);
impl Iterator for FilesIntoIter {
type Item = FileItem;
#[inline]
fn next(&mut self) -> Option<FileItem> {
self.0.next()
}
#[inline]
fn size_hint(&self) -> (usize, Option<usize>) {
self.0.size_hint()
}
#[inline]
fn count(self) -> usize {
self.0.count()
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct File {
pub depot_file: String,
pub client_file: String,
pub path: path::PathBuf,
non_exhaustive: (),
}
mod where_parser {
use super::*;
use super::super::parser::*;
named!(file<&[u8], File>,
do_parse!(
depot_file: depot_file >>
client_file: client_file >>
path: path >>
(
File {
depot_file: depot_file.path.to_owned(),
client_file: client_file.path.to_owned(),
path: path::PathBuf::from(path.path),
non_exhaustive: (),
}
)
)
);
named!(item<&[u8], FileItem>,
alt!(
map!(file, data_to_item) |
map!(error, error_to_item) |
map!(info, info_to_item)
)
);
named!(pub where_<&[u8], (Vec<FileItem>, FileItem)>,
pair!(
many0!(item),
map!(exit, exit_to_item)
)
);
}