use std::cmp::Ordering;
use async_trait::async_trait;
use bytes::Bytes;
use futures_util::{stream::BoxStream, StreamExt};
use wasm_pkg_common::{
package::{PackageRef, Version},
Error,
};
use crate::Release;
pub mod local;
pub mod oci;
pub mod warg;
#[derive(Clone, Debug, Eq)]
pub struct VersionInfo {
pub version: Version,
pub yanked: bool,
}
impl Ord for VersionInfo {
fn cmp(&self, other: &Self) -> Ordering {
self.version.cmp(&other.version)
}
}
impl PartialOrd for VersionInfo {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.version.cmp(&other.version))
}
}
impl PartialEq for VersionInfo {
fn eq(&self, other: &Self) -> bool {
self.version == other.version
}
}
impl std::fmt::Display for VersionInfo {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{version}", version = self.version)
}
}
#[async_trait]
pub trait PackageSource: Send {
async fn list_all_versions(&mut self, package: &PackageRef) -> Result<Vec<VersionInfo>, Error>;
async fn get_release(
&mut self,
package: &PackageRef,
version: &Version,
) -> Result<Release, Error>;
async fn stream_content_unvalidated(
&mut self,
package: &PackageRef,
release: &Release,
) -> Result<BoxStream<Result<Bytes, Error>>, Error>;
async fn stream_content(
&mut self,
package: &PackageRef,
release: &Release,
) -> Result<BoxStream<Result<Bytes, Error>>, Error> {
let stream = self.stream_content_unvalidated(package, release).await?;
Ok(release.content_digest.validating_stream(stream).boxed())
}
}