github_templates/
templates.rs

1use failure::ResultExt;
2use mkdirp::mkdirp;
3
4use std::fs::File;
5use std::io::prelude::*;
6use std::path::PathBuf;
7
8static BUG_REPORT: &str = include_str!("../templates/bug_report.md");
9static QUESTION: &str = include_str!("../templates/question.md");
10static FEATURE_REQUEST: &str = include_str!("../templates/feature_request.md");
11static OTHER_ISSUE: &str = include_str!("../templates/other_issue.md");
12
13/// GitHub template struct.
14pub struct Templates {
15  name: String,
16  dir: PathBuf,
17}
18
19impl Templates {
20  /// Create a new instance. Creates a `.github` directory if it doesn't exist
21  /// already.
22  pub fn new(mut dir: PathBuf, name: String) -> ::Result<Self> {
23    dir.push(".github");
24    mkdirp(&dir).context(::ErrorKind::Other)?;
25    Ok(Self { name, dir })
26  }
27
28  pub fn write_all(&self) -> ::Result<()> {
29    let issue_dir = self.dir.join("ISSUE_TEMPLATE");
30    mkdirp(&issue_dir).context(::ErrorKind::Other)?;
31
32    self.write(&issue_dir, "bug_report.md", BUG_REPORT)?;
33    self.write(&issue_dir, "question.md", QUESTION)?;
34    self.write(&issue_dir, "feature_request.md", FEATURE_REQUEST)?;
35    self.write(&self.dir, "ISSUE_TEMPLATE.md", OTHER_ISSUE)?;
36
37    Ok(())
38  }
39
40  fn write(
41    &self,
42    issue_dir: &PathBuf,
43    file_name: &str,
44    template: &str,
45  ) -> ::Result<()> {
46    let mut file =
47      File::create(issue_dir.join(file_name)).context(::ErrorKind::Other)?;
48
49    let bug_report = str::replace(template, "{{Project}}", &self.name);
50    file
51      .write_all(bug_report.as_bytes())
52      .context(::ErrorKind::Other)?;
53    Ok(())
54  }
55}