multiversx_sc_meta_lib/cargo_toml/
cargo_toml_deps.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
use crate::version::FrameworkVersion;

use super::{DependencyRawValue, VersionReq};

/// A dependency reference to a git commit. We mostly use git commits when referencing git.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitCommitReference {
    pub git: String,
    pub rev: String,
}

/// A dependency reference to a git branch.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitBranchReference {
    pub git: String,
    pub branch: String,
}

/// A dependency reference to a git tag.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct GitTagReference {
    pub git: String,
    pub tag: String,
}

/// Models how a dependency is expressed in Cargo.toml.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DependencyReference {
    Version(VersionReq),
    GitCommit(GitCommitReference),
    GitBranch(GitBranchReference),
    GitTag(GitTagReference),
    Path(String),
    Unsupported(String),
}

impl DependencyReference {
    pub fn is_framework_version(&self, version: &FrameworkVersion) -> bool {
        if let DependencyReference::Version(version_req) = self {
            &version_req.semver == version
        } else {
            false
        }
    }
}

impl DependencyRawValue {
    /// Interprets the raw dependency value as one of several possible formats.
    pub fn interpret(self) -> DependencyReference {
        // path is top priority
        if let Some(path) = self.path {
            return DependencyReference::Path(path);
        }

        if let Some(git) = self.git {
            return match (self.rev, self.branch, self.tag) {
                (Some(rev), None, None) => {
                    DependencyReference::GitCommit(GitCommitReference { git, rev })
                },
                (None, Some(branch), None) => {
                    DependencyReference::GitBranch(GitBranchReference { git, branch })
                },

                (None, None, Some(tag)) => {
                    DependencyReference::GitTag(GitTagReference { git, tag })
                },

                (None, None, None) => DependencyReference::Unsupported(
                    "need at least one of: git commit, git brach, or git tag".to_owned(),
                ),
                _ => DependencyReference::Unsupported(
                    "can only have one of: git commit, git brach, or git tag".to_owned(),
                ),
            };
        }

        // explicit version = "..."
        // handled last, because it has the lowest priority, both path and git fields override it
        if let Some(version) = self.version {
            if let Some(version_req) = VersionReq::from_version_str(&version) {
                return DependencyReference::Version(version_req);
            } else {
                return DependencyReference::Unsupported(format!(
                    "unknown framework version: {version}"
                ));
            }
        }

        DependencyReference::Unsupported("expected at least one of: version, git, path".to_owned())
    }
}