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
use crate::{tar, Digest, DigestWriter, Identifier};

use core::ops::{Deref, DerefMut};

use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};

use anyhow::Context;
use futures::io::sink;
use serde::{Deserialize, Serialize};
use url::Url;

/// Source of this dependency
#[derive(Clone, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
pub enum EntrySource {
    /// URL
    #[serde(rename = "url")]
    Url(Url),
    /// Local path
    #[serde(rename = "path")]
    Path(PathBuf),
}

/// WIT dependency [Lock] entry
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Entry {
    /// Resource source, [None] if the dependency is transitive
    #[serde(flatten)]
    pub source: Option<EntrySource>,
    /// Resource digest
    #[serde(flatten)]
    pub digest: Digest,
    /// Transitive dependency identifiers
    #[serde(default, skip_serializing_if = "BTreeSet::is_empty")]
    pub deps: BTreeSet<Identifier>,
}

impl Entry {
    /// Create a new entry given a dependency source and path containing it
    #[must_use]
    pub fn new(source: Option<EntrySource>, digest: Digest, deps: BTreeSet<Identifier>) -> Self {
        Self {
            source,
            digest,
            deps,
        }
    }

    /// Create a new entry given a dependency url and path containing the unpacked contents of it
    ///
    /// # Errors
    ///
    /// Returns an error if [`Self::digest`] of `path` fails
    pub async fn from_url(
        url: Url,
        path: impl AsRef<Path>,
        deps: BTreeSet<Identifier>,
    ) -> anyhow::Result<Self> {
        let digest = Self::digest(path)
            .await
            .context("failed to compute digest")?;
        Ok(Self::new(Some(EntrySource::Url(url)), digest, deps))
    }

    /// Create a new entry given a dependency path
    ///
    /// # Errors
    ///
    /// Returns an error if [`Self::digest`] of `path` fails
    pub async fn from_path(
        src: PathBuf,
        dst: impl AsRef<Path>,
        deps: BTreeSet<Identifier>,
    ) -> anyhow::Result<Self> {
        let digest = Self::digest(dst)
            .await
            .context("failed to compute digest")?;
        Ok(Self::new(Some(EntrySource::Path(src)), digest, deps))
    }

    /// Create a new entry given a transitive dependency path
    ///
    /// # Errors
    ///
    /// Returns an error if [`Self::digest`] of `path` fails
    pub async fn from_transitive_path(dst: impl AsRef<Path>) -> anyhow::Result<Self> {
        let digest = Self::digest(dst)
            .await
            .context("failed to compute digest")?;
        Ok(Self::new(None, digest, BTreeSet::default()))
    }

    /// Compute the digest of an entry from path
    ///
    /// # Errors
    ///
    /// Returns an error if tar-encoding the path fails
    pub async fn digest(path: impl AsRef<Path>) -> std::io::Result<Digest> {
        tar(path, DigestWriter::from(sink())).await.map(Into::into)
    }
}

/// WIT dependency lock mapping [Identifiers](Identifier) to [Entries](Entry)
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct Lock(BTreeMap<Identifier, Entry>);

impl Deref for Lock {
    type Target = BTreeMap<Identifier, Entry>;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Lock {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl FromIterator<(Identifier, Entry)> for Lock {
    fn from_iter<T: IntoIterator<Item = (Identifier, Entry)>>(iter: T) -> Self {
        Self(BTreeMap::from_iter(iter))
    }
}

impl Extend<(Identifier, Entry)> for Lock {
    fn extend<T: IntoIterator<Item = (Identifier, Entry)>>(&mut self, iter: T) {
        self.0.extend(iter);
    }
}

impl<const N: usize> From<[(Identifier, Entry); N]> for Lock {
    fn from(entries: [(Identifier, Entry); N]) -> Self {
        Self::from_iter(entries)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use anyhow::{ensure, Context};
    use hex::FromHex;

    const FOO_URL: &str = "https://example.com/baz";
    const FOO_SHA256: &str = "9f86d081884c7d658a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
    const FOO_SHA512: &str = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff";

    #[test]
    fn decode() -> anyhow::Result<()> {
        fn assert_lock(lock: Lock) -> anyhow::Result<Lock> {
            ensure!(
                lock == Lock::from([(
                    "foo".parse().expect("failed to `foo` parse identifier"),
                    Entry {
                        source: Some(EntrySource::Url(
                            FOO_URL.parse().expect("failed to parse `foo` URL")
                        )),
                        digest: Digest {
                            sha256: FromHex::from_hex(FOO_SHA256)
                                .expect("failed to decode `foo` sha256"),
                            sha512: FromHex::from_hex(FOO_SHA512)
                                .expect("failed to decode `foo` sha512"),
                        },
                        deps: BTreeSet::default(),
                    }
                )])
            );
            Ok(lock)
        }

        let lock = toml::from_str(&format!(
            r#"
foo = {{ url = "{FOO_URL}", sha256 = "{FOO_SHA256}", sha512 = "{FOO_SHA512}" }}
"#
        ))
        .context("failed to decode lock")
        .and_then(assert_lock)?;

        let lock = toml::to_string(&lock).context("failed to encode lock")?;
        toml::from_str(&lock)
            .context("failed to decode lock")
            .and_then(assert_lock)?;

        Ok(())
    }
}