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
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use std::path;
use std::vec;

use error;
use p4;

/// Show how file names are mapped by the client view
///
/// Where shows how the specified files are mapped by the client view.
/// For each argument, three names are produced: the name in the depot,
/// the name on the client in Perforce syntax, and the name on the client
/// in local syntax.
///
/// If the file parameter is omitted, the mapping for all files in the
/// current directory and below) is returned.
///
/// Note that 'p4 where' does not determine where any real files reside.
/// It only displays the locations that are mapped by the client view.
///
/// # Examples
///
/// ```rust,no_run
/// let p4 = p4_cmd::P4::new();
/// let files = p4.where_().file("//depot/dir/*").run().unwrap();
/// for file in files {
///     println!("{:?}", file);
/// }
/// ```
#[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![],
        }
    }

    /// Restrict the operation to the specified path.
    pub fn file(mut self, file: &'f str) -> Self {
        self.file.push(file);
        self
    }

    /// Run the `where` command.
    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)
        )
    );
}