spin_serde/
dependencies.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
//! Types for working with component dependencies.

use crate::KebabId;
use anyhow::anyhow;
use serde::{Deserialize, Serialize};
use std::str::FromStr;
use wasm_pkg_common::package::PackageRef;

/// Name of an import package dependency.
///
/// For example: `foo:bar/baz@0.1.0`, `foo:bar/baz`, `foo:bar@0.1.0`, `foo:bar`.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
#[serde(into = "String", try_from = "String")]
pub struct DependencyPackageName {
    /// The package spec, `foo:bar`, `foo:bar@0.1.0`.
    pub package: PackageRef,
    /// Package version
    pub version: Option<semver::Version>,
    /// Optional interface name.
    pub interface: Option<KebabId>,
}

impl std::fmt::Display for DependencyPackageName {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.package)?;
        if let Some(interface) = &self.interface {
            write!(f, "/{interface}")?;
        }
        if let Some(version) = &self.version {
            write!(f, "@{version}")?;
        }
        Ok(())
    }
}

impl TryFrom<String> for DependencyPackageName {
    type Error = anyhow::Error;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        s.parse()
    }
}

impl From<DependencyPackageName> for String {
    fn from(value: DependencyPackageName) -> Self {
        value.to_string()
    }
}

impl FromStr for DependencyPackageName {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (name, version) = match s.split_once('@') {
            Some((name, version)) => (name, Some(version.parse()?)),
            None => (s, None),
        };

        let (package, interface) = match name.split_once('/') {
            Some((package, interface)) => (
                package.parse()?,
                Some(
                    interface
                        .to_string()
                        .try_into()
                        .map_err(|e| anyhow::anyhow!("{e}"))?,
                ),
            ),
            None => (name.parse()?, None),
        };

        Ok(DependencyPackageName {
            package,
            version,
            interface,
        })
    }
}

/// Name of an import dependency.
///
/// For example: `foo:bar/baz@0.1.0`, `foo:bar/baz`, `foo:bar@0.1.0`, `foo:bar`, `foo-bar`.
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, Hash)]
#[serde(into = "String", try_from = "String")]
pub enum DependencyName {
    /// Plain name
    Plain(KebabId),
    /// Package spec
    Package(DependencyPackageName),
}

// TODO: replace with derive once wasm-pkg-common is released
impl PartialOrd for DependencyName {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

// TODO: replace with derive once wasm-pkg-common is released
impl Ord for DependencyName {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        match (self, other) {
            (DependencyName::Plain(a), DependencyName::Plain(b)) => a.cmp(b),
            (DependencyName::Package(a), DependencyName::Package(b)) => {
                let big_ole_tup = (
                    a.package.namespace().as_ref(),
                    a.package.name().as_ref(),
                    a.interface.as_ref(),
                    a.version.as_ref(),
                );
                let other_big_ole_tup = (
                    b.package.namespace().as_ref(),
                    b.package.name().as_ref(),
                    b.interface.as_ref(),
                    b.version.as_ref(),
                );
                big_ole_tup.cmp(&other_big_ole_tup)
            }
            (DependencyName::Plain(_), DependencyName::Package(_)) => std::cmp::Ordering::Less,
            (DependencyName::Package(_), DependencyName::Plain(_)) => std::cmp::Ordering::Greater,
        }
    }
}

impl std::fmt::Display for DependencyName {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        match self {
            DependencyName::Plain(plain) => write!(f, "{plain}"),
            DependencyName::Package(name) => {
                write!(f, "{}", name.package)?;
                if let Some(interface) = &name.interface {
                    write!(f, "/{interface}")?;
                }
                if let Some(version) = &name.version {
                    write!(f, "@{version}")?;
                }
                Ok(())
            }
        }
    }
}

impl TryFrom<String> for DependencyName {
    type Error = anyhow::Error;

    fn try_from(s: String) -> Result<Self, Self::Error> {
        s.parse()
    }
}

impl From<DependencyName> for String {
    fn from(value: DependencyName) -> Self {
        value.to_string()
    }
}

impl FromStr for DependencyName {
    type Err = anyhow::Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.contains([':', '/']) {
            Ok(Self::Package(s.parse()?))
        } else {
            Ok(Self::Plain(
                s.to_string().try_into().map_err(|e| anyhow!("{e}"))?,
            ))
        }
    }
}

impl DependencyName {
    /// Returns the package reference if this is a package dependency name.
    pub fn package(&self) -> Option<&PackageRef> {
        match self {
            DependencyName::Package(name) => Some(&name.package),
            DependencyName::Plain(_) => None,
        }
    }
}