multiversx_sc_meta_lib/cargo_toml/
cargo_toml_contents.rs

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
use std::{
    fs,
    io::Write,
    path::{Path, PathBuf},
};

use toml::{value::Table, Value};

use crate::contract::sc_config::ContractVariantProfile;

use super::DependencyRawValue;

pub const CARGO_TOML_DEPENDENCIES: &str = "dependencies";
pub const CARGO_TOML_DEV_DEPENDENCIES: &str = "dev-dependencies";
pub const PACKAGE: &str = "package";
pub const AUTHORS: &str = "authors";
const AUTO_GENERATED: &str = "# Code generated by the multiversx-sc build system. DO NOT EDIT.

# ##########################################
# ############## AUTO-GENERATED #############
# ##########################################

";

/// Contains an in-memory representation of a Cargo.toml file.
///
/// Implementation notes:
///
/// - Currently contains a raw toml tree, but in principle it could also work with a cargo_toml::Manifest.
/// - It keeps an ordered representation, thanks to the `toml` `preserve_order` feature.
#[derive(Clone, Debug)]
pub struct CargoTomlContents {
    pub path: PathBuf,
    pub toml_value: toml::Value,
    pub prepend_auto_generated_comment: bool,
}

impl CargoTomlContents {
    pub fn parse_string(raw_str: &str, path: &Path) -> Self {
        let toml_value = raw_str.parse::<toml::Value>().unwrap_or_else(|e| {
            panic!(
                "failed to parse Cargo.toml toml format, path:{}, error: {e}",
                path.display()
            )
        });
        CargoTomlContents {
            path: path.to_owned(),
            toml_value,
            prepend_auto_generated_comment: false,
        }
    }

    pub fn load_from_file<P: AsRef<Path>>(path: P) -> Self {
        let path_ref = path.as_ref();
        let cargo_toml_content = fs::read(path_ref).expect("failed to open Cargo.toml file");
        let cargo_toml_content_str =
            String::from_utf8(cargo_toml_content).expect("error decoding Cargo.toml utf-8");
        Self::parse_string(&cargo_toml_content_str, path_ref)
    }

    pub fn new() -> Self {
        CargoTomlContents {
            path: PathBuf::new(),
            toml_value: toml::Value::Table(Table::new()),
            prepend_auto_generated_comment: false,
        }
    }

    pub fn save_to_file<P: AsRef<Path>>(&self, path: P) {
        let cargo_toml_content_str = &self.to_string_pretty();
        let mut file = std::fs::File::create(path).expect("failed to create Cargo.toml file");
        file.write_all(cargo_toml_content_str.as_bytes())
            .expect("failed to write Cargo.toml contents to file");
    }

    pub fn package_name(&self) -> String {
        self.toml_value
            .get(PACKAGE)
            .expect("missing package in Cargo.toml")
            .get("name")
            .expect("missing package name in Cargo.toml")
            .as_str()
            .expect("package name not a string value")
            .to_string()
    }

    pub fn package_edition(&self) -> String {
        self.toml_value
            .get(PACKAGE)
            .expect("missing package in Cargo.toml")
            .get("edition")
            .expect("missing package name in Cargo.toml")
            .as_str()
            .expect("package name not a string value")
            .to_string()
    }

    /// Interprets the dependency value and organizes values in a struct.
    pub fn dependency_raw_value(&self, crate_name: &str) -> Option<DependencyRawValue> {
        self.dependency(crate_name)
            .map(DependencyRawValue::parse_toml_value)
    }

    pub fn insert_dependency_raw_value(&mut self, crate_name: &str, raw_value: DependencyRawValue) {
        self.dependencies_mut()
            .insert(crate_name.to_owned(), raw_value.into_toml_value());
    }

    /// Assumes that a package section already exists.
    pub fn change_package_name(&mut self, new_package_name: String) {
        let package = self
            .toml_value
            .get_mut("package")
            .expect("missing package in Cargo.toml");
        package
            .as_table_mut()
            .expect("malformed package in Cargo.toml")
            .insert("name".to_string(), toml::Value::String(new_package_name));
    }

    pub fn dependencies_table(&self) -> Option<&Table> {
        if let Some(deps) = self.toml_value.get(CARGO_TOML_DEPENDENCIES) {
            deps.as_table()
        } else if let Some(deps) = self.toml_value.get(CARGO_TOML_DEV_DEPENDENCIES) {
            deps.as_table()
        } else {
            None
        }
    }

    pub fn dependency(&self, dep_name: &str) -> Option<&Value> {
        if let Some(deps_map) = self.dependencies_table() {
            deps_map.get(dep_name)
        } else {
            None
        }
    }

    pub fn has_dependencies(&self) -> bool {
        self.toml_value.get(CARGO_TOML_DEPENDENCIES).is_some()
    }

    pub fn dependencies_mut(&mut self) -> &mut Table {
        self.toml_value
            .as_table_mut()
            .expect("add deps cargo toml error wasm adapter")
            .entry(CARGO_TOML_DEPENDENCIES)
            .or_insert(toml::Value::Table(toml::map::Map::new()))
            .as_table_mut()
            .expect("malformed crate Cargo.toml")
    }

    pub fn has_dev_dependencies(&self) -> bool {
        self.toml_value.get(CARGO_TOML_DEV_DEPENDENCIES).is_some()
    }

    pub fn change_author(&mut self, authors: String) -> bool {
        let package = self
            .toml_value
            .get_mut(PACKAGE)
            .unwrap_or_else(|| panic!("no dependencies found in crate {}", self.path.display()))
            .as_table_mut()
            .expect("missing package in Cargo.toml");

        package.remove(AUTHORS);

        package.insert(
            AUTHORS.to_owned(),
            toml::Value::Array(vec![toml::Value::String(authors)]),
        );

        true
    }

    pub fn dev_dependencies_mut(&mut self) -> &mut Table {
        self.toml_value
            .get_mut(CARGO_TOML_DEV_DEPENDENCIES)
            .unwrap_or_else(|| panic!("no dependencies found in crate {}", self.path.display()))
            .as_table_mut()
            .expect("malformed crate Cargo.toml")
    }

    pub fn add_crate_type(&mut self, crate_type: &str) {
        let mut value = toml::map::Map::new();
        let array = vec![toml::Value::String(crate_type.to_string())];
        let members = toml::Value::Array(array);
        value.insert("crate-type".to_string(), members);

        self.toml_value
            .as_table_mut()
            .expect("malformed package in Cargo.toml")
            .insert("lib".to_string(), toml::Value::Table(value));
    }

    pub fn add_package_info(
        &mut self,
        name: &String,
        version: String,
        current_edition: String,
        publish: bool,
    ) {
        let mut value = toml::map::Map::new();
        value.insert("name".to_string(), Value::String(name.to_string()));
        value.insert("version".to_string(), Value::String(version));
        value.insert("edition".to_string(), Value::String(current_edition));
        value.insert("publish".to_string(), Value::Boolean(publish));

        self.toml_value
            .as_table_mut()
            .expect("malformed package in Cargo.toml / add_package")
            .insert("package".to_string(), toml::Value::Table(value));
    }

    pub fn add_contract_variant_profile(&mut self, contract_profile: &ContractVariantProfile) {
        let mut profile_props = toml::map::Map::new();
        profile_props.insert(
            "codegen-units".to_string(),
            Value::Integer(contract_profile.codegen_units.into()),
        );
        profile_props.insert(
            "opt-level".to_string(),
            Value::String(contract_profile.opt_level.to_owned()),
        );
        profile_props.insert("lto".to_string(), Value::Boolean(contract_profile.lto));
        profile_props.insert("debug".to_string(), Value::Boolean(contract_profile.debug));
        profile_props.insert(
            "panic".to_string(),
            Value::String(contract_profile.panic.to_owned()),
        );
        profile_props.insert(
            "overflow-checks".to_string(),
            Value::Boolean(contract_profile.overflow_checks),
        );

        // add contract variant profile
        let mut toml_table = toml::map::Map::new();
        toml_table.insert("release".to_string(), toml::Value::Table(profile_props));

        // add profile dev
        let mut dev_value = toml::map::Map::new();
        dev_value.insert("panic".to_string(), Value::String("abort".to_string()));
        toml_table.insert("dev".to_string(), toml::Value::Table(dev_value));

        self.toml_value
            .as_table_mut()
            .expect("malformed package in Cargo.toml")
            .insert("profile".to_string(), toml::Value::Table(toml_table));
    }

    pub fn add_workspace(&mut self, members: &[&str]) {
        let array: Vec<toml::Value> = members
            .iter()
            .map(|s| toml::Value::String(s.to_string()))
            .collect();
        let members_toml = toml::Value::Array(array);

        let mut workspace = toml::Value::Table(toml::map::Map::new());
        workspace
            .as_table_mut()
            .expect("malformed package in Cargo.toml")
            .insert("members".to_string(), members_toml);

        self.toml_value
            .as_table_mut()
            .expect("malformed package in Cargo.toml")
            .insert("workspace".to_string(), workspace);
    }

    pub fn local_dependency_paths(&self, ignore_deps: &[&str]) -> Vec<String> {
        let mut result = Vec::new();
        if let Some(deps_map) = self.dependencies_table() {
            for (key, value) in deps_map {
                if ignore_deps.contains(&key.as_str()) {
                    continue;
                }

                if let Some(path) = value.get("path") {
                    result.push(path.as_str().expect("path is not a string").to_string());
                }
            }
        }
        result
    }

    pub fn change_features_for_parent_crate_dep(
        &mut self,
        features: &[String],
        default_features: Option<bool>,
    ) {
        let deps_mut = self.dependencies_mut();
        for (_, dep) in deps_mut {
            if is_dep_path_above(dep) {
                let feature_values = features
                    .iter()
                    .map(|feature| Value::String(feature.clone()))
                    .collect();
                let deps_table = dep.as_table_mut().expect("malformed crate Cargo.toml");
                deps_table.insert("features".to_string(), Value::Array(feature_values));
                if let Some(default_features_value) = default_features {
                    deps_table.insert(
                        "default-features".to_string(),
                        Value::Boolean(default_features_value),
                    );
                }
            }
        }
    }

    pub fn to_string_pretty(&self) -> String {
        let toml_string =
            toml::to_string_pretty(&self.toml_value).expect("failed to format Cargo.toml contents");
        if self.prepend_auto_generated_comment {
            return format!("{}{}", AUTO_GENERATED, toml_string);
        }

        toml_string
    }
}

/// Checks that path == ".." in a depdency.
fn is_dep_path_above(dep: &Value) -> bool {
    if let Some(path) = dep.get("path") {
        if let Some(s) = path.as_str() {
            return s == "..";
        }
    }

    false
}

pub fn change_from_base_to_adapter_path(base_path: &str) -> String {
    format!(
        "../{}",
        base_path.to_string().replace("base", "wasm-adapter")
    )
}

/// TODO: still useful?
#[allow(unused)]
fn remove_quotes(var: &Value) -> String {
    var.to_string().replace('\"', "")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cargo_toml::{DependencyReference, GitCommitReference, VersionReq};

    #[test]
    fn test_change_from_base_to_adapter_path() {
        let base_path = "../../../framework/base";
        let adapter_path = "../../../../framework/wasm-adapter".to_string();
        assert_eq!(
            super::change_from_base_to_adapter_path(base_path),
            adapter_path
        );
    }

    const CARGO_TOML_RAW: &str = r#"
[dependencies.by-version-1]
version = "0.54.0"

[dependencies.by-version-1-strict]
version = "=0.54.1"

[dependencies.by-git-commit-1]
git = "https://github.com/multiversx/repo1"
rev = "85c31b9ce730bd5ffe41589c353d935a14baaa96"

[dependencies.by-path-1]
path = "a/b/c"

[dependencies]
by-version-2 = "0.54.2"
by-version-2-strict = "=0.54.3"
by-path-2 = { path = "d/e/f" }
by-git-commit-2 = { git = "https://github.com/multiversx/repo2", rev = "e990be823f26d1e7f59c71536d337b7240dc3fa2" }
    "#;

    #[test]
    fn test_dependency_value() {
        let cargo_toml = CargoTomlContents::parse_string(CARGO_TOML_RAW, "/test".as_ref());

        // version
        let raw_value = cargo_toml.dependency_raw_value("by-version-1").unwrap();
        assert_eq!(
            raw_value,
            DependencyRawValue {
                version: Some("0.54.0".to_owned()),
                ..Default::default()
            },
        );
        assert_eq!(
            raw_value.interpret(),
            DependencyReference::Version(VersionReq::from_version_str("0.54.0").unwrap()),
        );

        // version, strict
        let raw_value = cargo_toml
            .dependency_raw_value("by-version-1-strict")
            .unwrap();
        assert_eq!(
            raw_value,
            DependencyRawValue {
                version: Some("=0.54.1".to_owned()),
                ..Default::default()
            },
        );
        assert_eq!(
            raw_value.interpret(),
            DependencyReference::Version(VersionReq::from_version_str("0.54.1").unwrap().strict()),
        );

        // version, compact
        let raw_value = cargo_toml.dependency_raw_value("by-version-2").unwrap();
        assert_eq!(
            raw_value,
            DependencyRawValue {
                version: Some("0.54.2".to_owned()),
                ..Default::default()
            },
        );
        assert_eq!(
            raw_value.interpret(),
            DependencyReference::Version(VersionReq::from_version_str("0.54.2").unwrap()),
        );

        // version, compact, strict
        let raw_value = cargo_toml
            .dependency_raw_value("by-version-2-strict")
            .unwrap();
        assert_eq!(
            raw_value,
            DependencyRawValue {
                version: Some("=0.54.3".to_owned()),
                ..Default::default()
            },
        );
        assert_eq!(
            raw_value.interpret(),
            DependencyReference::Version(VersionReq::from_version_str("0.54.3").unwrap().strict()),
        );

        // git
        let raw_value = cargo_toml.dependency_raw_value("by-git-commit-1").unwrap();
        assert_eq!(
            raw_value,
            DependencyRawValue {
                git: Some("https://github.com/multiversx/repo1".to_owned()),
                rev: Some("85c31b9ce730bd5ffe41589c353d935a14baaa96".to_owned()),
                ..Default::default()
            },
        );
        assert_eq!(
            raw_value.interpret(),
            DependencyReference::GitCommit(GitCommitReference {
                git: "https://github.com/multiversx/repo1".to_owned(),
                rev: "85c31b9ce730bd5ffe41589c353d935a14baaa96".to_owned(),
            })
        );

        // git, compact
        let raw_value = cargo_toml.dependency_raw_value("by-git-commit-2").unwrap();
        assert_eq!(
            raw_value,
            DependencyRawValue {
                git: Some("https://github.com/multiversx/repo2".to_owned()),
                rev: Some("e990be823f26d1e7f59c71536d337b7240dc3fa2".to_owned()),
                ..Default::default()
            },
        );
        assert_eq!(
            raw_value.interpret(),
            DependencyReference::GitCommit(GitCommitReference {
                git: "https://github.com/multiversx/repo2".to_owned(),
                rev: "e990be823f26d1e7f59c71536d337b7240dc3fa2".to_owned(),
            })
        );

        // path
        let raw_value = cargo_toml.dependency_raw_value("by-path-1").unwrap();
        assert_eq!(
            raw_value,
            DependencyRawValue {
                path: Some("a/b/c".to_owned()),
                ..Default::default()
            },
        );
        assert_eq!(
            raw_value.interpret(),
            DependencyReference::Path("a/b/c".to_owned()),
        );

        // path, compact
        let raw_value = cargo_toml.dependency_raw_value("by-path-2").unwrap();
        assert_eq!(
            raw_value,
            DependencyRawValue {
                path: Some("d/e/f".to_owned()),
                ..Default::default()
            },
        );
        assert_eq!(
            raw_value.interpret(),
            DependencyReference::Path("d/e/f".to_owned()),
        );
    }
}