1use std::{error::Error, fmt::Display, path::Path, str::FromStr};
4
5use git2::Repository;
6
7const ROOT_GITIGNORE: &str = "\
8# FXServer
9/artifact/
10
11# txAdmin
12/txData/
13";
14
15const DATA_GITIGNORE: &str = "\
16# Cache
17/cache/
18
19# KVP
20/db/
21
22# Miscellaneous
23/.replxx_history
24/imgui.ini
25";
26
27#[derive(Default, Clone)]
29pub enum VcsOption {
30 #[default]
31 None,
32 Git,
33}
34
35#[derive(Debug)]
36pub struct ParseVcsOptionError {
37 option: String,
38}
39
40impl Display for ParseVcsOptionError {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 write!(f, "unknown vsc option {}", &self.option)?;
43
44 Ok(())
45 }
46}
47
48impl Error for ParseVcsOptionError {}
49
50impl FromStr for VcsOption {
51 type Err = ParseVcsOptionError;
52
53 fn from_str(option: &str) -> Result<Self, Self::Err> {
54 match option {
55 "none" => Ok(Self::None),
56 "git" => Ok(Self::Git),
57 _ => Err(ParseVcsOptionError {
58 option: option.to_owned(),
59 }),
60 }
61 }
62}
63
64impl VcsOption {
65 pub fn init<P>(&self, path: P) -> Result<(), Box<dyn Error>>
67 where
68 P: AsRef<Path>,
69 {
70 let path = path.as_ref();
71
72 match self {
73 VcsOption::None => Ok(()),
74 VcsOption::Git => {
75 Repository::init(path)?;
76
77 fs_err::write(path.join(".gitignore"), ROOT_GITIGNORE)?;
78 fs_err::write(path.join("data").join(".gitignore"), DATA_GITIGNORE)?;
79
80 Ok(())
81 }
82 }
83 }
84}