canic_host/release_set/paths/
version.rs1use std::{fs, path::Path};
2use toml::Value as TomlValue;
3
4pub fn load_root_package_version(
6 root_manifest_path: &Path,
7 workspace_manifest_path: &Path,
8) -> Result<String, Box<dyn std::error::Error>> {
9 let manifest_source = fs::read_to_string(root_manifest_path)?;
10 let manifest = toml::from_str::<TomlValue>(&manifest_source)?;
11 let version_value = manifest
12 .get("package")
13 .and_then(TomlValue::as_table)
14 .and_then(|package| package.get("version"))
15 .ok_or_else(|| {
16 format!(
17 "missing package.version in {}",
18 root_manifest_path.display()
19 )
20 })?;
21
22 if let Some(version) = version_value.as_str() {
23 return Ok(version.to_string());
24 }
25
26 if version_value
27 .as_table()
28 .and_then(|value| value.get("workspace"))
29 .and_then(TomlValue::as_bool)
30 == Some(true)
31 {
32 return load_workspace_package_version(workspace_manifest_path);
33 }
34
35 Err(format!(
36 "unsupported package.version format in {}",
37 root_manifest_path.display()
38 )
39 .into())
40}
41
42pub fn load_workspace_package_version(
44 workspace_manifest_path: &Path,
45) -> Result<String, Box<dyn std::error::Error>> {
46 let manifest_source = fs::read_to_string(workspace_manifest_path)?;
47 let manifest = toml::from_str::<TomlValue>(&manifest_source)?;
48 let version = manifest
49 .get("workspace")
50 .and_then(TomlValue::as_table)
51 .and_then(|workspace| workspace.get("package"))
52 .and_then(TomlValue::as_table)
53 .and_then(|package| package.get("version"))
54 .and_then(TomlValue::as_str)
55 .ok_or_else(|| {
56 format!(
57 "missing workspace.package.version in {}",
58 workspace_manifest_path.display()
59 )
60 })?;
61
62 Ok(version.to_string())
63}