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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::{fs, io::Error as IoError, path::Path};
use thiserror::Error;
use toml_edit::{value, Document, Item, TomlError};
#[derive(Debug, Error)]
pub enum Error {
#[error("an io error occurred")]
IoError(#[from] IoError),
#[error("a parser error occurred")]
ParseError(#[from] TomlError),
#[error("the field {field:?} is not of type {ty:?}")]
InvalidFieldType { field: String, ty: String },
}
pub fn get_version(path: impl AsRef<Path>) -> Result<String, Error> {
let cargo_toml_content = fs::read_to_string(path.as_ref())?;
let doc = cargo_toml_content.parse::<Document>()?;
let item: &Item = &doc["package"]["version"];
item.as_str()
.map(|s| s.to_string())
.ok_or_else(|| Error::InvalidFieldType {
field: "version".to_string(),
ty: "string".to_string(),
})
}
pub fn set_version(path: impl AsRef<Path>, version: impl AsRef<str>) -> Result<(), Error> {
let cargo_toml_content = fs::read_to_string(path.as_ref())?;
let mut doc = cargo_toml_content.parse::<Document>()?;
doc["package"]["version"] = value(version.as_ref());
fs::write(path.as_ref(), doc.to_string())?;
Ok(())
}