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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use std::vec;

use error;
use p4;

/// Write a depot file to standard output
///
/// Retrieve the contents of a depot file to the client's standard output.
/// The file is not synced.  If file is specified using client syntax,
/// Perforce uses the client view to determine the corresponding depot
/// file.
///
/// By default, the head revision is printed.  If the file argument
/// includes a revision, the specified revision is printed.  If the
/// file argument has a revision range,  then only files selected by
/// that revision range are printed, and the highest revision in the
/// range is printed. For details about revision specifiers, see 'p4
/// help revisions'.
///
/// # Examples
///
/// ```rust,no_run
/// let p4 = p4_cmd::P4::new();
/// let files = p4.print("//depot/dir/file").run().unwrap();
/// for file in files {
///     println!("{:?}", file);
/// }
/// ```
#[derive(Debug, Clone)]
pub struct PrintCommand<'p, 'f> {
    connection: &'p p4::P4,
    file: Vec<&'f str>,

    all_revs: bool,
    keyword_expansion: bool,
    max_files: Option<usize>,
}

impl<'p, 'f> PrintCommand<'p, 'f> {
    pub fn new(connection: &'p p4::P4, file: &'f str) -> Self {
        Self {
            connection,
            file: vec![file],
            all_revs: false,
            keyword_expansion: true,
            max_files: None,
        }
    }

    pub fn file(mut self, dir: &'f str) -> Self {
        self.file.push(dir);
        self
    }

    /// The -a flag prints all revisions within the specified range, rather
    /// than just the highest revision in the range.
    pub fn all_revs(mut self, all_revs: bool) -> Self {
        self.all_revs = all_revs;
        self
    }

    /// The -k flag suppresses keyword expansion.
    pub fn keyword_expansion(mut self, keyword_expansion: bool) -> Self {
        self.keyword_expansion = keyword_expansion;
        self
    }

    /// The -m flag limits print to the first 'max' number of files.
    pub fn max_files(mut self, max_files: usize) -> Self {
        self.max_files = Some(max_files);
        self
    }

    /// Run the `print` command.
    pub fn run(self) -> Result<Files, error::P4Error> {
        let mut cmd = self.connection.connect_with_retries(None);
        cmd.arg("print");
        if self.all_revs {
            cmd.arg("-s");
        }
        if !self.keyword_expansion {
            cmd.arg("-k");
        }
        if let Some(max_files) = self.max_files {
            let max_files = format!("{}", max_files);
            cmd.args(&["-m", &max_files]);
        }
        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)) = files_parser::files(&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 enum FileContent {
    #[doc(hidden)]
    __Nonexhaustive,

    Text(Vec<String>),
    Binary(Vec<u8>),
}

impl FileContent {
    pub fn as_text(&self) -> Option<&[String]> {
        match self {
            FileContent::Text(c) => Some(&c),
            _ => None,
        }
    }

    pub fn as_binary(&self) -> Option<&[u8]> {
        match self {
            FileContent::Binary(c) => Some(&c),
            _ => None,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct File {
    pub content: FileContent,
    pub depot_file: String,
    pub rev: usize,
    pub change: usize,
    pub action: p4::Action,
    pub file_type: p4::FileType,
    pub time: p4::Time,
    pub file_size: usize,
    non_exhaustive: (),
}

mod files_parser {
    use super::*;

    use super::super::parser::*;

    named!(pub file<&[u8], File>,
        do_parse!(
            depot_file: depot_file >>
            rev: rev >>
            change: change >>
            action: action >>
            file_type: file_type >>
            time: time >>
            file_size: file_size >>
            content: alt!(
                map!(many1!(text), texts_to_content) |
                map!(take!(file_size.size), slice_to_content)
            ) >>
            (
                File {
                    content: content,
                    depot_file: depot_file.path.to_owned(),
                    rev: rev.rev,
                    change: change.change,
                    action: action.action.parse().expect("`Unknown` to capture all"),
                    file_type: file_type.ft.parse().expect("`Unknown` to capture all"),
                    time: p4::from_timestamp(time.time),
                    file_size: file_size.size,
                    non_exhaustive: (),
                }
            )
        )
    );

    named!(item<&[u8], FileItem>,
        alt!(
            map!(file, data_to_item) |
            map!(error, error_to_item) |
            map!(info, info_to_item)
        )
    );

    named!(pub files<&[u8], (Vec<FileItem>, FileItem)>,
        pair!(
            many0!(item),
            map!(exit, exit_to_item)
        )
    );

    fn texts_to_content(texts: Vec<String>) -> FileContent {
        FileContent::Text(texts)
    }

    fn slice_to_content(s: &[u8]) -> FileContent {
        FileContent::Binary(s.to_vec())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn print_text_single() {
        let output: &[u8] = br#"info1: depotFile //depot/dir/file
info1: rev 3
info1: change 42
info1: action edit
info1: type text
info1: time 1527128624
info1: fileSize 494514
text: Hello
text: World
exit: 0
"#;
        let (_remains, (items, exit)) = files_parser::files(output).unwrap();
        let item = items[0].as_data().unwrap();
        assert_eq!(
            item.content,
            FileContent::Text(vec!["Hello".to_owned(), "World".to_owned()])
        );
        assert_eq!(exit.as_error(), Some(&error::OperationError::new(0)));
    }

    #[test]
    fn print_text_multi() {
        let output: &[u8] = br#"info1: depotFile //depot/dir/file
info1: rev 3
info1: change 42
info1: action edit
info1: type text
info1: time 1527128624
info1: fileSize 494514
text: Hello
text: World
info1: depotFile //depot/dir/file2
info1: rev 3
info1: change 42
info1: action edit
info1: type text
info1: time 1527128624
info1: fileSize 494514
text: Goodbye
text: World
exit: 0
"#;
        let (_remains, (items, exit)) = files_parser::files(output).unwrap();
        let first = items[0].as_data().unwrap();
        let last = items[1].as_data().unwrap();
        assert_eq!(
            first.content,
            FileContent::Text(vec!["Hello".to_owned(), "World".to_owned()])
        );
        assert_eq!(
            last.content,
            FileContent::Text(vec!["Goodbye".to_owned(), "World".to_owned()])
        );
        assert_eq!(exit.as_error(), Some(&error::OperationError::new(0)));
    }

    #[test]
    fn print_binary_single() {
        let output: &[u8] = b"info1: depotFile //depot/dir/file
info1: rev 3
info1: change 42
info1: action edit
info1: type binary
info1: time 1527128624
info1: fileSize 5
1\02\n3exit: 0
";
        let (_remains, (items, exit)) = files_parser::files(output).unwrap();
        assert_eq!(
            items[0].as_data().unwrap().content,
            FileContent::Binary(b"1\02\n3".to_vec())
        );
        assert_eq!(exit.as_error(), Some(&error::OperationError::new(0)));
    }

    #[test]
    fn file_binary() {
        let output: &[u8] = b"info1: depotFile //depot/dir/file
info1: rev 3
info1: change 42
info1: action edit
info1: type binary
info1: time 1527128624
info1: fileSize 5
1\02\n3
";
        let (_remains, item) = files_parser::file(output).unwrap();
        assert_eq!(item.content, FileContent::Binary(b"1\02\n3".to_vec()));
    }
}