wasm_pkg_loader/source/
mod.rs

1use std::cmp::Ordering;
2
3use async_trait::async_trait;
4use bytes::Bytes;
5use futures_util::{stream::BoxStream, StreamExt};
6use wasm_pkg_common::{
7    package::{PackageRef, Version},
8    Error,
9};
10
11use crate::Release;
12
13pub mod local;
14pub mod oci;
15pub mod warg;
16
17#[derive(Clone, Debug, Eq)]
18pub struct VersionInfo {
19    pub version: Version,
20    pub yanked: bool,
21}
22
23impl Ord for VersionInfo {
24    fn cmp(&self, other: &Self) -> Ordering {
25        self.version.cmp(&other.version)
26    }
27}
28
29impl PartialOrd for VersionInfo {
30    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
31        Some(self.version.cmp(&other.version))
32    }
33}
34
35impl PartialEq for VersionInfo {
36    fn eq(&self, other: &Self) -> bool {
37        self.version == other.version
38    }
39}
40
41impl std::fmt::Display for VersionInfo {
42    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
43        write!(f, "{version}", version = self.version)
44    }
45}
46
47#[async_trait]
48pub trait PackageSource: Send {
49    async fn list_all_versions(&mut self, package: &PackageRef) -> Result<Vec<VersionInfo>, Error>;
50
51    async fn get_release(
52        &mut self,
53        package: &PackageRef,
54        version: &Version,
55    ) -> Result<Release, Error>;
56
57    async fn stream_content_unvalidated(
58        &mut self,
59        package: &PackageRef,
60        release: &Release,
61    ) -> Result<BoxStream<Result<Bytes, Error>>, Error>;
62
63    async fn stream_content(
64        &mut self,
65        package: &PackageRef,
66        release: &Release,
67    ) -> Result<BoxStream<Result<Bytes, Error>>, Error> {
68        let stream = self.stream_content_unvalidated(package, release).await?;
69        Ok(release.content_digest.validating_stream(stream).boxed())
70    }
71}