modinfo/
impls.rs

1use super::*;
2
3impl Modinfo {
4    /// Create a new Modinfo instance with default values
5    pub fn new() -> Self {
6        Modinfo::default()
7    }
8
9    /// Write the Modinfo to a file
10    /// uses `modinfo_version` to determine which format to use
11    pub fn write(&self, file: Option<&Path>) -> Result<(), ModinfoError> {
12        match file {
13            Some(path) => {
14                fs::write(path, self.to_string())?;
15            }
16            None => {
17                fs::write(self.meta.path.clone(), self.to_string())?;
18            }
19        }
20
21        Ok(())
22    }
23
24    /// Retrieve the value for a given field.
25    ///
26    /// Note: This is not case-sensitive so you can use `get_value_for("Author")` or `get_value_for("author")`
27    ///
28    /// Please note that `version` is excluded from this function, use `get_version` instead
29    ///
30    /// ```rust
31    /// use modinfo::Modinfo;
32    ///
33    /// let mut modinfo = Modinfo::default();
34    /// modinfo.set_value_for("author", "Joe");
35    ///
36    /// assert_eq!(modinfo.get_value_for("author"), Some(&std::borrow::Cow::from("Joe")));
37    /// ```
38    pub fn get_value_for<F>(&self, field: F) -> Option<&Cow<'_, str>>
39    where
40        F: AsRef<str>,
41    {
42        match field.as_ref().to_lowercase().as_ref() {
43            "author" => self.author.value.as_ref(),
44            "description" => self.description.value.as_ref(),
45            "display_name" => self.display_name.value.as_ref(),
46            "name" => self.name.value.as_ref(),
47            "website" => self.website.value.as_ref(),
48            "compat" => self.version.compat.as_ref(),
49            _ => None,
50        }
51    }
52
53    /// Set the value for a given `field` to `value`
54    ///
55    /// Note: `field` is not case-sensitive, so you can use `set_value_for("Author", "Joe")` or `get_value_for("author", "Joe")`
56    ///
57    /// ```rust
58    /// use modinfo::Modinfo;
59    ///
60    /// let mut modinfo = Modinfo::default();
61    /// modinfo.set_value_for("name", "MyMod");
62    ///
63    /// assert_eq!(modinfo.get_value_for("name"), Some(&std::borrow::Cow::from("MyMod")));
64    /// ```
65    pub fn set_value_for(&mut self, field: &str, value: &str) {
66        match field.to_lowercase().as_ref() {
67            "author" => self.author.value = Some(value.to_owned().into()),
68            "description" => self.description.value = Some(value.to_owned().into()),
69            "display_name" => self.display_name.value = Some(value.to_owned().into()),
70            "name" => self.name.value = Some(value.to_owned().into()),
71            "website" => self.website.value = Some(value.to_owned().into()),
72            "version" => self.version.value.set_version(value),
73            "compat" => self.version.compat = Some(value.to_owned().into()),
74            _ => (),
75        }
76    }
77
78    /// Retrieve the value for the version field included the ModInfo
79    ///
80    /// Note: This is the version of the modlet, not the version of the ModInfo file format
81    /// Please see `get_modinfo_version` for that.
82    ///
83    /// ```rust
84    /// use modinfo::Modinfo;
85    ///
86    /// let modinfo = Modinfo::default();
87    ///
88    /// assert_eq!(modinfo.get_version(), &semver::Version::new(0, 1, 0));
89    /// ```
90    pub fn get_version(&self) -> &Version {
91        &self.version.value
92    }
93
94    /// Sets the version field inside the modinfo.xml file (modlet version)
95    ///
96    /// ```rust
97    /// use modinfo::Modinfo;
98    ///
99    /// let mut modinfo = Modinfo::default();
100    /// modinfo.set_version("1.2.3".to_owned());
101    ///
102    /// assert_eq!(modinfo.get_version(), &semver::Version::new(1, 2, 3));
103    /// ```
104    pub fn set_version(&mut self, version: String) {
105        self.set_value_for("version", &version)
106    }
107
108    /// Retrieves the current version of the ModInfo.xml file (V1 or V2)
109    ///
110    /// returns a `ModinfoVersion` enum:
111    ///     ModinfoVersion::V1
112    ///     ModinfoVersion::V2
113    ///
114    /// ```rust
115    /// use modinfo::{Modinfo, ModinfoVersion};
116    ///
117    /// let mut modinfo = Modinfo::default();
118    ///
119    /// assert_eq!(modinfo.get_modinfo_version(), ModinfoVersion::V2);
120    /// ```
121    pub fn get_modinfo_version(&self) -> ModinfoVersion {
122        self.meta.version.clone()
123    }
124
125    /// Sets the version of the ModInfo.xml file itesle (V1 or V2)
126    ///
127    /// Accepts a `ModinfoVersion` enum:
128    ///     ModinfoVersion::V1
129    ///     ModinfoVersion::V2
130    ///
131    /// ```rust
132    /// use modinfo::{Modinfo, ModinfoVersion};
133    ///
134    /// let mut modinfo = Modinfo::default();
135    /// modinfo.set_modinfo_version(ModinfoVersion::V1);
136    ///
137    /// assert_eq!(modinfo.get_modinfo_version(), ModinfoVersion::V1);
138    /// ```
139    pub fn set_modinfo_version(&mut self, version: ModinfoVersion) {
140        self.meta.version = version
141    }
142
143    /// Retrieves the current modinfo.xml file path
144    ///
145    /// ```rust
146    /// use modinfo::Modinfo;
147    /// use std::path::PathBuf;
148    ///
149    /// let mut modinfo = Modinfo::default();
150    /// modinfo.set_file_path(PathBuf::from("modinfo.xml"));
151    ///
152    /// assert_eq!(modinfo.get_file_path(), &PathBuf::from("modinfo.xml"));
153    /// ```
154    pub fn get_file_path(&self) -> &PathBuf {
155        &self.meta.path
156    }
157
158    /// Sets the modinfo.xml file path
159    ///
160    /// This is normally set automatically when a file is parsed, but can be set manually
161    /// such as when creating a new modinfo.xml file.
162    ///
163    /// ```rust
164    /// use modinfo::Modinfo;
165    /// use std::path::PathBuf;
166    ///
167    /// let mut modinfo = Modinfo::default();
168    /// modinfo.set_file_path(PathBuf::from("modinfo.xml"));
169    ///
170    /// assert_eq!(modinfo.get_file_path(), &PathBuf::from("modinfo.xml"));
171    /// ```
172    pub fn set_file_path(&mut self, path: PathBuf) {
173        self.meta.path = path.clone();
174    }
175
176    /// Increases the Major version number by 1,
177    /// sets Minor and Patch to 0, and removes any pre or build data.
178    ///
179    /// ```rust
180    /// use modinfo::Modinfo;
181    ///
182    /// let mut modinfo = Modinfo::default();
183    /// modinfo.set_version("1.2.3-foo+bar".to_owned());
184    /// modinfo.bump_version_major();
185    ///
186    /// assert_eq!(modinfo.get_version(), &semver::Version::new(2, 0, 0));
187    /// ```
188    pub fn bump_version_major(&mut self) {
189        self.version.value.bump_major()
190    }
191
192    /// Increases the Minor version number by 1,
193    /// sets Patch to 0, and removes any pre or build data.
194    ///
195    /// ```rust
196    /// use modinfo::Modinfo;
197    ///
198    /// let mut modinfo = Modinfo::default();
199    /// modinfo.set_version("1.2.3-foo+bar".to_owned());
200    /// modinfo.bump_version_minor();
201    ///
202    /// assert_eq!(modinfo.get_version(), &semver::Version::new(1, 3, 0));
203    /// ```
204    pub fn bump_version_minor(&mut self) {
205        self.version.value.bump_minor()
206    }
207
208    /// Increases the Patch version number by 1,
209    /// and removes any pre or build data.
210    ///
211    /// ```rust
212    /// use modinfo::Modinfo;
213    ///
214    /// let mut modinfo = Modinfo::default();
215    /// modinfo.set_version("1.2.3-foo+bar".to_owned());
216    /// modinfo.bump_version_patch();
217    /// assert_eq!(modinfo.get_version(), &semver::Version::new(1, 2, 4));
218    /// ```
219    pub fn bump_version_patch(&mut self) {
220        self.version.value.bump_patch()
221    }
222
223    /// Adds a pre-release version to the version field
224    ///
225    /// ```rust
226    /// use modinfo::Modinfo;
227    ///
228    /// let mut modinfo = Modinfo::default();
229    /// modinfo.set_version("1.2.3".to_owned());
230    /// modinfo.add_version_pre("foo");
231    /// assert_eq!(modinfo.get_version(), &semver::Version::parse("1.2.3-foo").unwrap());
232    /// ```
233    pub fn add_version_pre(&mut self, pre: &str) {
234        self.version.value.add_pre(pre)
235    }
236
237    /// Adds build data to the version field
238    ///
239    /// ```rust
240    /// use modinfo::Modinfo;
241    ///
242    /// let mut modinfo = Modinfo::default();
243    /// modinfo.set_version("1.2.3".to_owned());
244    /// modinfo.add_version_build("bar");
245    /// assert_eq!(modinfo.get_version(), &semver::Version::parse("1.2.3+bar").unwrap());
246    /// ```
247    pub fn add_version_build(&mut self, build: &str) {
248        self.version.value.add_build(build)
249    }
250}