-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathactive_copirate.rs
47 lines (37 loc) · 1.48 KB
/
active_copirate.rs
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
//! Records the currently active co-authors collaborating on this Git repository.
use std::fs::{self, File, OpenOptions};
use std::io::Write;
use std::path::Path;
use crate::copirate::CoPirate;
use crate::BoxResult;
const ACTIVE_COPIRATES_PATH: &str = ".git/.git-rmob-template";
/// File containing `Co-authored-by` lines for all active co-authors.
#[derive(Debug)]
pub struct ActiveCoPirates {
file: File,
}
impl ActiveCoPirates {
/// Opens a pre-existing co-pirates file for the repository located at `repo_dir`.
pub fn get(repo_dir: &Path) -> BoxResult<String> {
let active_copirates = fs::read_to_string(repo_dir.join(ACTIVE_COPIRATES_PATH))?;
Ok(active_copirates)
}
/// Creates an empty active co-pirates file for the repository located at `repo_dir`.
///
/// If the file already exists, its contents will be cleared.
pub fn create_empty(repo_dir: &Path) -> BoxResult<ActiveCoPirates> {
let active_copirates_path = repo_dir.join(ACTIVE_COPIRATES_PATH);
fs::write(&active_copirates_path, "")?;
let file = OpenOptions::new()
.append(true)
.open(active_copirates_path)?;
Ok(ActiveCoPirates { file })
}
/// Generates `Co-authored-by` messages for the given co-pirates and saves the file to disk.
pub fn save(mut self, copirates: &[&CoPirate]) -> BoxResult<()> {
for pirate in copirates {
writeln!(self.file, "Co-authored-by: {}", pirate)?;
}
Ok(())
}
}