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
use crate::element::Element;
use crate::format_list::format_list;
use anyhow::Result;
use std::fmt;
use std::fs;
use std::io::{self, Read};

#[derive(PartialEq, Debug)]
pub struct State {
    pub elements: Vec<Element>,
}

impl fmt::Display for State {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", format_list(&self.elements))
    }
}

impl State {
    pub fn from_file(file_name: &str) -> Result<Self> {
        let content = fs::read_to_string(file_name)?;
        Ok(State::new(&content))
    }

    // TODO: cover in test
    pub fn from_stdin() -> Result<Self> {
        let mut buffer = String::new();
        io::stdin().lock().read_to_string(&mut buffer)?;
        Ok(State::new(&buffer))
    }

    fn new(content: &str) -> Self {
        Self {
            elements: content
                .lines()
                .map(|l| Element::Resource(l.to_string()))
                .collect(),
        }
    }

    pub fn diff(&self, other: &State) -> Vec<String> {
        self.elements
            .iter()
            .zip(other.elements.iter())
            .map(|(src, dst)| src.diff(&dst))
            .collect()
    }
}

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

    fn current() -> State {
        State {
            elements: vec![
                Element::Resource("docker_container.container".to_string()),
                Element::Resource("docker_image.image".to_string()),
                Element::Resource("public_network.docker_network.network".to_string()),
            ],
        }
    }

    #[test]
    fn test_from_file_loads_a_migration() {
        assert_eq!(
            State::from_file("fixtures/state/current").unwrap(),
            current()
        )
    }

    #[test]
    fn test_diff_compares_state_line_by_line() {
        let lines: Vec<String> = current().elements.iter().map(|l| l.to_string()).collect();
        assert_eq!(
            State::from_file("fixtures/state/current")
                .unwrap()
                .diff(&current()),
            lines
        )
    }
}