multiversx_sc_meta_lib/cargo_toml/
cargo_toml_contents.rs

1use std::{
2    fs,
3    io::Write,
4    path::{Path, PathBuf},
5};
6
7use toml::{value::Table, Value};
8
9use crate::contract::sc_config::ContractVariantProfile;
10
11use super::DependencyRawValue;
12
13pub const CARGO_TOML_DEPENDENCIES: &str = "dependencies";
14pub const CARGO_TOML_DEV_DEPENDENCIES: &str = "dev-dependencies";
15pub const PACKAGE: &str = "package";
16pub const AUTHORS: &str = "authors";
17const AUTO_GENERATED: &str = "# Code generated by the multiversx-sc build system. DO NOT EDIT.
18
19# ##########################################
20# ############## AUTO-GENERATED #############
21# ##########################################
22
23";
24
25/// Contains an in-memory representation of a Cargo.toml file.
26///
27/// Implementation notes:
28///
29/// - Currently contains a raw toml tree, but in principle it could also work with a cargo_toml::Manifest.
30/// - It keeps an ordered representation, thanks to the `toml` `preserve_order` feature.
31#[derive(Clone, Debug)]
32pub struct CargoTomlContents {
33    pub path: PathBuf,
34    pub toml_value: toml::Table,
35    pub prepend_auto_generated_comment: bool,
36}
37
38impl CargoTomlContents {
39    pub fn parse_string(raw_str: &str, path: &Path) -> Self {
40        let toml_value = toml::from_str::<toml::Table>(raw_str).unwrap_or_else(|e| {
41            panic!(
42                "failed to parse Cargo.toml toml format, path:{}, error: {e}",
43                path.display()
44            )
45        });
46        CargoTomlContents {
47            path: path.to_owned(),
48            toml_value,
49            prepend_auto_generated_comment: false,
50        }
51    }
52
53    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Self {
54        let path_ref = path.as_ref();
55        let cargo_toml_content = fs::read(path_ref).expect("failed to open Cargo.toml file");
56        let cargo_toml_content_str =
57            String::from_utf8(cargo_toml_content).expect("error decoding Cargo.toml utf-8");
58        Self::parse_string(&cargo_toml_content_str, path_ref)
59    }
60
61    pub fn new() -> Self {
62        CargoTomlContents {
63            path: PathBuf::new(),
64            toml_value: Table::new(),
65            prepend_auto_generated_comment: false,
66        }
67    }
68
69    pub fn save_to_file<P: AsRef<Path>>(&self, path: P) {
70        let cargo_toml_content_str = &self.to_string_pretty();
71        let mut file = std::fs::File::create(path).expect("failed to create Cargo.toml file");
72        file.write_all(cargo_toml_content_str.as_bytes())
73            .expect("failed to write Cargo.toml contents to file");
74    }
75
76    pub fn package_name(&self) -> String {
77        self.toml_value
78            .get(PACKAGE)
79            .expect("missing package in Cargo.toml")
80            .get("name")
81            .expect("missing package name in Cargo.toml")
82            .as_str()
83            .expect("package name not a string value")
84            .to_string()
85    }
86
87    pub fn package_edition(&self) -> String {
88        self.toml_value
89            .get(PACKAGE)
90            .expect("missing package in Cargo.toml")
91            .get("edition")
92            .expect("missing package name in Cargo.toml")
93            .as_str()
94            .expect("package name not a string value")
95            .to_string()
96    }
97
98    /// Interprets the dependency value and organizes values in a struct.
99    pub fn dependency_raw_value(&self, crate_name: &str) -> Option<DependencyRawValue> {
100        self.dependency(crate_name)
101            .map(DependencyRawValue::parse_toml_value)
102    }
103
104    pub fn insert_dependency_raw_value(&mut self, crate_name: &str, raw_value: DependencyRawValue) {
105        self.dependencies_mut()
106            .insert(crate_name.to_owned(), raw_value.into_toml_value());
107    }
108
109    /// Assumes that a package section already exists.
110    pub fn change_package_name(&mut self, new_package_name: String) {
111        let package = self
112            .toml_value
113            .get_mut("package")
114            .expect("missing package in Cargo.toml");
115        package
116            .as_table_mut()
117            .expect("malformed package in Cargo.toml")
118            .insert("name".to_string(), toml::Value::String(new_package_name));
119    }
120
121    pub fn dependencies_table(&self) -> Option<&Table> {
122        if let Some(deps) = self.toml_value.get(CARGO_TOML_DEPENDENCIES) {
123            deps.as_table()
124        } else if let Some(deps) = self.toml_value.get(CARGO_TOML_DEV_DEPENDENCIES) {
125            deps.as_table()
126        } else {
127            None
128        }
129    }
130
131    pub fn dependency(&self, dep_name: &str) -> Option<&Value> {
132        if let Some(deps_map) = self.dependencies_table() {
133            deps_map.get(dep_name)
134        } else {
135            None
136        }
137    }
138
139    pub fn has_dependencies(&self) -> bool {
140        self.toml_value.get(CARGO_TOML_DEPENDENCIES).is_some()
141    }
142
143    pub fn dependencies_mut(&mut self) -> &mut Table {
144        self.toml_value
145            .entry(CARGO_TOML_DEPENDENCIES)
146            .or_insert(toml::Value::Table(toml::map::Map::new()))
147            .as_table_mut()
148            .expect("malformed crate Cargo.toml")
149    }
150
151    pub fn has_dev_dependencies(&self) -> bool {
152        self.toml_value.get(CARGO_TOML_DEV_DEPENDENCIES).is_some()
153    }
154
155    pub fn change_author(&mut self, authors: String) -> bool {
156        let package = self
157            .toml_value
158            .get_mut(PACKAGE)
159            .unwrap_or_else(|| panic!("no dependencies found in crate {}", self.path.display()))
160            .as_table_mut()
161            .expect("missing package in Cargo.toml");
162
163        package.remove(AUTHORS);
164
165        package.insert(
166            AUTHORS.to_owned(),
167            toml::Value::Array(vec![toml::Value::String(authors)]),
168        );
169
170        true
171    }
172
173    pub fn dev_dependencies_mut(&mut self) -> &mut Table {
174        self.toml_value
175            .get_mut(CARGO_TOML_DEV_DEPENDENCIES)
176            .unwrap_or_else(|| panic!("no dependencies found in crate {}", self.path.display()))
177            .as_table_mut()
178            .expect("malformed crate Cargo.toml")
179    }
180
181    pub fn add_crate_type(&mut self, crate_type: &str) {
182        let mut value = toml::map::Map::new();
183        let array = vec![toml::Value::String(crate_type.to_string())];
184        let members = toml::Value::Array(array);
185        value.insert("crate-type".to_string(), members);
186
187        self.toml_value
188            .insert("lib".to_string(), toml::Value::Table(value));
189    }
190
191    pub fn add_package_info(
192        &mut self,
193        name: &String,
194        version: String,
195        current_edition: String,
196        publish: bool,
197    ) {
198        let mut value = toml::map::Map::new();
199        value.insert("name".to_string(), Value::String(name.to_string()));
200        value.insert("version".to_string(), Value::String(version));
201        value.insert("edition".to_string(), Value::String(current_edition));
202        value.insert("publish".to_string(), Value::Boolean(publish));
203
204        self.toml_value
205            .insert("package".to_string(), toml::Value::Table(value));
206    }
207
208    pub fn add_contract_variant_profile(&mut self, contract_profile: &ContractVariantProfile) {
209        let mut profile_props = toml::map::Map::new();
210        profile_props.insert(
211            "codegen-units".to_string(),
212            Value::Integer(contract_profile.codegen_units.into()),
213        );
214        profile_props.insert(
215            "opt-level".to_string(),
216            Value::String(contract_profile.opt_level.to_owned()),
217        );
218        profile_props.insert("lto".to_string(), Value::Boolean(contract_profile.lto));
219        profile_props.insert("debug".to_string(), Value::Boolean(contract_profile.debug));
220        profile_props.insert(
221            "panic".to_string(),
222            Value::String(contract_profile.panic.to_owned()),
223        );
224        profile_props.insert(
225            "overflow-checks".to_string(),
226            Value::Boolean(contract_profile.overflow_checks),
227        );
228
229        // add contract variant profile
230        let mut toml_table = toml::map::Map::new();
231        toml_table.insert("release".to_string(), toml::Value::Table(profile_props));
232
233        // add profile dev
234        let mut dev_value = toml::map::Map::new();
235        dev_value.insert("panic".to_string(), Value::String("abort".to_string()));
236        toml_table.insert("dev".to_string(), toml::Value::Table(dev_value));
237
238        self.toml_value
239            .insert("profile".to_string(), toml::Value::Table(toml_table));
240    }
241
242    pub fn add_workspace(&mut self, members: &[&str]) {
243        let array: Vec<toml::Value> = members
244            .iter()
245            .map(|s| toml::Value::String(s.to_string()))
246            .collect();
247        let members_toml = toml::Value::Array(array);
248
249        let mut workspace = toml::Value::Table(toml::map::Map::new());
250        workspace
251            .as_table_mut()
252            .expect("malformed package in Cargo.toml")
253            .insert("members".to_string(), members_toml);
254
255        self.toml_value.insert("workspace".to_string(), workspace);
256    }
257
258    pub fn local_dependency_paths(&self, ignore_deps: &[&str]) -> Vec<String> {
259        let mut result = Vec::new();
260        if let Some(deps_map) = self.dependencies_table() {
261            for (key, value) in deps_map {
262                if ignore_deps.contains(&key.as_str()) {
263                    continue;
264                }
265
266                if let Some(path) = value.get("path") {
267                    result.push(path.as_str().expect("path is not a string").to_string());
268                }
269            }
270        }
271        result
272    }
273
274    pub fn change_features_for_parent_crate_dep(
275        &mut self,
276        features: &[String],
277        default_features: Option<bool>,
278    ) {
279        let deps_mut = self.dependencies_mut();
280        for (_, dep) in deps_mut {
281            if is_dep_path_above(dep) {
282                let feature_values = features
283                    .iter()
284                    .map(|feature| Value::String(feature.clone()))
285                    .collect();
286                let deps_table = dep.as_table_mut().expect("malformed crate Cargo.toml");
287                deps_table.insert("features".to_string(), Value::Array(feature_values));
288                if let Some(default_features_value) = default_features {
289                    deps_table.insert(
290                        "default-features".to_string(),
291                        Value::Boolean(default_features_value),
292                    );
293                }
294            }
295        }
296    }
297
298    pub fn to_string_pretty(&self) -> String {
299        let toml_string =
300            toml::to_string_pretty(&self.toml_value).expect("failed to format Cargo.toml contents");
301        if self.prepend_auto_generated_comment {
302            return format!("{}{}", AUTO_GENERATED, toml_string);
303        }
304
305        toml_string
306    }
307}
308
309/// Checks that path == ".." in a dependency.
310fn is_dep_path_above(dep: &Value) -> bool {
311    if let Some(path) = dep.get("path") {
312        if let Some(s) = path.as_str() {
313            return s == "..";
314        }
315    }
316
317    false
318}
319
320pub fn change_from_base_to_adapter_path(base_path: &Path) -> PathBuf {
321    let path = base_path.to_string_lossy().replace("base", "wasm-adapter");
322
323    Path::new("..").join(path)
324}
325
326/// TODO: still useful?
327#[allow(unused)]
328fn remove_quotes(var: &Value) -> String {
329    var.to_string().replace('\"', "")
330}
331
332#[cfg(test)]
333mod tests {
334    use super::*;
335    use crate::cargo_toml::{DependencyReference, GitCommitReference, VersionReq};
336
337    #[test]
338    fn test_change_from_base_to_adapter_path() {
339        let base_path = Path::new("..")
340            .join("..")
341            .join("..")
342            .join("framework")
343            .join("base");
344        let adapter_path = Path::new("..")
345            .join("..")
346            .join("..")
347            .join("..")
348            .join("framework")
349            .join("wasm-adapter");
350
351        assert_eq!(
352            super::change_from_base_to_adapter_path(&base_path),
353            adapter_path
354        );
355    }
356
357    const CARGO_TOML_RAW: &str = r#"
358[dependencies.by-version-1]
359version = "0.54.0"
360
361[dependencies.by-version-1-strict]
362version = "=0.54.1"
363
364[dependencies.by-git-commit-1]
365git = "https://github.com/multiversx/repo1"
366rev = "85c31b9ce730bd5ffe41589c353d935a14baaa96"
367
368[dependencies.by-path-1]
369path = "a/b/c"
370
371[dependencies]
372by-version-2 = "0.54.2"
373by-version-2-strict = "=0.54.3"
374by-path-2 = { path = "d/e/f" }
375by-git-commit-2 = { git = "https://github.com/multiversx/repo2", rev = "e990be823f26d1e7f59c71536d337b7240dc3fa2" }
376    "#;
377
378    #[test]
379    fn test_dependency_value() {
380        let cargo_toml = CargoTomlContents::parse_string(CARGO_TOML_RAW, Path::new("test"));
381
382        // version
383        let raw_value = cargo_toml.dependency_raw_value("by-version-1").unwrap();
384        assert_eq!(
385            raw_value,
386            DependencyRawValue {
387                version: Some("0.54.0".to_owned()),
388                ..Default::default()
389            },
390        );
391        assert_eq!(
392            raw_value.interpret(),
393            DependencyReference::Version(VersionReq::from_version_str("0.54.0").unwrap()),
394        );
395
396        // version, strict
397        let raw_value = cargo_toml
398            .dependency_raw_value("by-version-1-strict")
399            .unwrap();
400        assert_eq!(
401            raw_value,
402            DependencyRawValue {
403                version: Some("=0.54.1".to_owned()),
404                ..Default::default()
405            },
406        );
407        assert_eq!(
408            raw_value.interpret(),
409            DependencyReference::Version(VersionReq::from_version_str("0.54.1").unwrap().strict()),
410        );
411
412        // version, compact
413        let raw_value = cargo_toml.dependency_raw_value("by-version-2").unwrap();
414        assert_eq!(
415            raw_value,
416            DependencyRawValue {
417                version: Some("0.54.2".to_owned()),
418                ..Default::default()
419            },
420        );
421        assert_eq!(
422            raw_value.interpret(),
423            DependencyReference::Version(VersionReq::from_version_str("0.54.2").unwrap()),
424        );
425
426        // version, compact, strict
427        let raw_value = cargo_toml
428            .dependency_raw_value("by-version-2-strict")
429            .unwrap();
430        assert_eq!(
431            raw_value,
432            DependencyRawValue {
433                version: Some("=0.54.3".to_owned()),
434                ..Default::default()
435            },
436        );
437        assert_eq!(
438            raw_value.interpret(),
439            DependencyReference::Version(VersionReq::from_version_str("0.54.3").unwrap().strict()),
440        );
441
442        // git
443        let raw_value = cargo_toml.dependency_raw_value("by-git-commit-1").unwrap();
444        assert_eq!(
445            raw_value,
446            DependencyRawValue {
447                git: Some("https://github.com/multiversx/repo1".to_owned()),
448                rev: Some("85c31b9ce730bd5ffe41589c353d935a14baaa96".to_owned()),
449                ..Default::default()
450            },
451        );
452        assert_eq!(
453            raw_value.interpret(),
454            DependencyReference::GitCommit(GitCommitReference {
455                git: "https://github.com/multiversx/repo1".to_owned(),
456                rev: "85c31b9ce730bd5ffe41589c353d935a14baaa96".to_owned(),
457            })
458        );
459
460        // git, compact
461        let raw_value = cargo_toml.dependency_raw_value("by-git-commit-2").unwrap();
462        assert_eq!(
463            raw_value,
464            DependencyRawValue {
465                git: Some("https://github.com/multiversx/repo2".to_owned()),
466                rev: Some("e990be823f26d1e7f59c71536d337b7240dc3fa2".to_owned()),
467                ..Default::default()
468            },
469        );
470        assert_eq!(
471            raw_value.interpret(),
472            DependencyReference::GitCommit(GitCommitReference {
473                git: "https://github.com/multiversx/repo2".to_owned(),
474                rev: "e990be823f26d1e7f59c71536d337b7240dc3fa2".to_owned(),
475            })
476        );
477
478        // path
479        let raw_value = cargo_toml.dependency_raw_value("by-path-1").unwrap();
480        let path = Path::new("a").join("b").join("c");
481        assert_eq!(
482            raw_value,
483            DependencyRawValue {
484                path: Some(path.clone()),
485                ..Default::default()
486            },
487        );
488        assert_eq!(raw_value.interpret(), DependencyReference::Path(path),);
489
490        // path, compact
491        let raw_value = cargo_toml.dependency_raw_value("by-path-2").unwrap();
492        let path = Path::new("d").join("e").join("f");
493        assert_eq!(
494            raw_value,
495            DependencyRawValue {
496                path: Some(path.clone()),
497                ..Default::default()
498            },
499        );
500        assert_eq!(raw_value.interpret(), DependencyReference::Path(path),);
501    }
502}