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
use std::collections::HashSet;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;

impl super::Remotes {
    pub fn new(path: &Path) -> super::Remotes {
        super::Remotes {
            origins: HashSet::new(),
            filename: path.to_path_buf(),
        }
    }

    pub fn load(path: &Path) -> super::errors::Result<super::Remotes> {
        let mut file = std::fs::File::open(&path)?;
        let mut json = String::new();
        file.read_to_string(&mut json)?;
        let mut remotes = serde_json::from_str::<super::Remotes>(&json)?;
        remotes.filename = path.to_path_buf();
        Ok(remotes)
    }

    pub fn save(&self) -> super::errors::Result<()> {
        let mut file: File = std::fs::File::create(&self.filename)?;
        let _size = file.write(serde_json::to_string(&self)?.as_bytes())?;
        Ok(())
    }

    pub fn origins(&self) -> &HashSet<String> {
        &self.origins
    }

    pub fn import(&mut self, path: &Path) -> super::errors::Result<()> {
        let origins = super::git::collect_remote_origins(path)?;
        for origin in origins {
            self.origins.insert(origin);
        }
        &self.save()?;
        Ok(())
    }

    pub fn clobber(&mut self) -> super::errors::Result<()> {
        &self.origins.clear();
        &self.save()?;
        Ok(())
    }
}