conventional_semver_rs/config/
mod.rs

1use serde::Deserialize;
2use std::path::Path;
3use std::{fs, io};
4use std::str::FromStr;
5
6mod presets;
7use presets::FilePresets;
8
9const CONFIG_PATH: &str = "conventional_release.toml";
10
11#[derive(Deserialize, Debug)]
12pub struct ConventionalSemverConfig {
13    #[serde(default = "ConventionalSemverConfig::default_v")]
14    pub v: bool,
15    pub version_files: Option<Vec<VersionFileConfig>>,
16    #[serde(default = "CommitSignature::default_sig")]
17    pub commit_signature: CommitSignature,
18}
19
20impl ConventionalSemverConfig {
21    fn default_v() -> bool {
22        false
23    }
24    fn default_path() -> String {
25        String::from("")
26    }
27}
28
29#[derive(Deserialize, Debug)]
30pub struct CommitSignature {
31    #[serde(default = "CommitSignature::default_sig_name")]
32    pub name: String,
33    #[serde(default = "CommitSignature::default_sig_email")]
34    pub email: String,
35}
36
37impl CommitSignature {
38    fn default_sig_name() -> String {
39        String::from("conventional-semver-rs")
40    }
41
42    fn default_sig_email() -> String {
43        String::from("conventional-semver-rs@github.com")
44    }
45
46    fn default_sig() -> CommitSignature {
47        CommitSignature {
48            name: Self::default_sig_name(),
49            email: Self::default_sig_email(),
50        }
51    }
52}
53
54#[derive(Deserialize, Debug, Clone)]
55pub struct VersionFileConfig {
56    #[serde(default = "ConventionalSemverConfig::default_v")]
57    pub v: bool,
58    #[serde(default = "ConventionalSemverConfig::default_path")]
59    pub path: String,
60    pub version_prefix: Option<String>,
61    pub version_postfix: Option<String>,
62    pub preset: Option<String>,
63}
64
65impl ConventionalSemverConfig {
66    pub fn new(v: bool, commit_signature: CommitSignature, version_files: Vec<VersionFileConfig>) -> Self {
67        Self {
68            v,
69            commit_signature,
70            version_files: Some(version_files)
71        }
72    }
73
74    pub fn default() -> Self {
75        Self {
76            v: false,
77            commit_signature: CommitSignature::default_sig(),
78            version_files: None
79        }
80    }
81
82    pub fn load_config() -> Result<Self, crate::Error> {
83        let pth = Path::new(CONFIG_PATH);
84        match fs::read_to_string(pth) {
85            Ok(c_file) => {
86                let str = c_file.as_str();
87                let mut config = toml::from_str::<ConventionalSemverConfig>(str)?;
88                if config.version_files.is_some() {
89                    for mut f in config.version_files.as_mut().unwrap().iter_mut() {
90                        if let Some(pre) = &f.preset{
91                            let preset = FilePresets::from_str(&pre)?;
92                            let cp = presets::PRESETS.get(&preset).expect("Preset not part of preset map");
93                            f.v = cp.v.clone();
94                            f.path = cp.path.clone();
95                            f.version_prefix = cp.version_prefix.clone();
96                            f.version_postfix = cp.version_postfix.clone();
97                            f.preset = cp.preset.clone();
98                            ()
99                        } else if f.path == "" {
100                            return Err(crate::Error::InvalidConfigError{
101                                reason: String::from("version_file path cannot be blank, without a preset")
102                            })
103                        }
104                    };
105                }
106                Ok(config)
107            },
108            Err(err) => {
109                if err.kind() == io::ErrorKind::NotFound {
110                    eprintln!("convention_release.toml not found, using default configuration");
111                    Ok(Self::default())
112                } else {
113                    Err(err.into())
114                }
115            }
116        }
117    }
118}