multiversx_sc_meta_lib/cargo_toml/
cargo_toml_deps_raw.rs

1use std::{collections::BTreeSet, path::PathBuf};
2
3#[derive(Default, Debug, Clone, PartialEq, Eq)]
4pub struct DependencyRawValue {
5    pub version: Option<String>,
6    pub git: Option<String>,
7    pub rev: Option<String>,
8    pub branch: Option<String>,
9    pub tag: Option<String>,
10    pub path: Option<PathBuf>,
11    pub features: BTreeSet<String>,
12}
13
14impl DependencyRawValue {
15    pub fn from_version(version: &str) -> Self {
16        Self {
17            version: Some(version.to_owned()),
18            ..Default::default()
19        }
20    }
21
22    pub fn parse_toml_value(toml_value: &toml::Value) -> Self {
23        match toml_value {
24            toml::Value::String(version) => DependencyRawValue::from_version(version),
25            toml::Value::Table(table) => {
26                let mut result = DependencyRawValue::default();
27                if let Some(toml::Value::String(version)) = table.get("version") {
28                    result.version = Some(version.to_owned());
29                }
30                if let Some(toml::Value::String(path)) = table.get("path") {
31                    result.path = Some(PathBuf::from(path));
32                }
33                if let Some(toml::Value::String(git)) = table.get("git") {
34                    result.git = Some(git.to_owned());
35                }
36                if let Some(toml::Value::String(rev)) = table.get("rev") {
37                    result.rev = Some(rev.to_owned());
38                }
39                if let Some(toml::Value::String(branch)) = table.get("branch") {
40                    result.branch = Some(branch.to_owned());
41                }
42                if let Some(toml::Value::String(tag)) = table.get("tag") {
43                    result.tag = Some(tag.to_owned());
44                }
45                result
46            }
47            _ => panic!("Unsupported dependency value"),
48        }
49    }
50
51    pub fn into_toml_value(self) -> toml::Value {
52        let mut table = toml::map::Map::new();
53
54        if let Some(version) = self.version {
55            table.insert("version".to_string(), toml::Value::String(version));
56        }
57
58        if let Some(git) = self.git {
59            table.insert("git".to_string(), toml::Value::String(git));
60        }
61
62        if let Some(rev) = self.rev {
63            table.insert("rev".to_string(), toml::Value::String(rev));
64        }
65
66        if let Some(branch) = self.branch {
67            table.insert("branch".to_string(), toml::Value::String(branch));
68        }
69
70        if let Some(tag) = self.tag {
71            table.insert("tag".to_string(), toml::Value::String(tag));
72        }
73
74        if let Some(path) = self.path {
75            table.insert(
76                "path".to_string(),
77                toml::Value::String(path.to_string_lossy().into_owned()),
78            );
79        }
80
81        if !self.features.is_empty() {
82            table.insert(
83                "features".to_string(),
84                toml::Value::Array(
85                    self.features
86                        .iter()
87                        .map(|feature| toml::Value::String(feature.clone()))
88                        .collect(),
89                ),
90            );
91        }
92
93        toml::Value::Table(table)
94    }
95}