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
use std::default::Default;

#[cfg_attr(test, derive(Debug, PartialEq))]
pub(super) enum InnerState {
    Removed,
    Original,
    Replaced(Vec<String>),
}

impl Default for InnerState {
    fn default() -> Self {
        InnerState::Original
    }
}

/// Represents actions that are available while reading live output from a process.
///
/// This will be available inside the function you provide to [`Command::process_lines`](struct.Command.html#method.process_lines)
pub struct ProcessLinesActions {
    state: InnerState,
}

impl<'a> ProcessLinesActions {
    pub(super) fn new() -> Self {
        ProcessLinesActions {
            state: InnerState::default(),
        }
    }

    pub(super) fn take_lines(&mut self) -> InnerState {
        std::mem::take(&mut self.state)
    }

    /// Replace last read line from output with the lines provided.
    ///
    /// The new lines will be logged instead of the original line.
    pub fn replace_with_lines(&mut self, new_lines: impl Iterator<Item = &'a str>) {
        self.state = InnerState::Replaced(new_lines.map(|str| str.to_string()).collect());
    }

    /// Remove last read line from output.
    ///
    /// This means that the line will not be logged.
    pub fn remove_line(&mut self) {
        self.state = InnerState::Removed;
    }
}

#[cfg(test)]
mod test {
    use super::InnerState;
    use super::ProcessLinesActions;
    #[test]
    fn test_replace() {
        let mut actions = ProcessLinesActions::new();

        actions.replace_with_lines("ipsum".split("\n"));
        assert_eq!(
            actions.take_lines(),
            InnerState::Replaced(vec!["ipsum".to_string()])
        );

        actions.replace_with_lines("lorem ipsum dolor".split(" "));
        assert_eq!(
            actions.take_lines(),
            InnerState::Replaced(vec![
                "lorem".to_string(),
                "ipsum".to_string(),
                "dolor".to_string()
            ])
        );

        // assert last input is discarded
        assert_eq!(actions.take_lines(), InnerState::Original);
    }

    #[test]
    fn test_remove() {
        let mut actions = ProcessLinesActions::new();
        actions.remove_line();
        assert_eq!(actions.take_lines(), InnerState::Removed);
    }

    #[test]
    fn test_no_actions() {
        let mut actions = ProcessLinesActions::new();
        assert_eq!(actions.take_lines(), InnerState::Original);
    }
}