use crate::PackageHashes;
use pep440_rs::VersionSpecifiers;
use pep508_rs::{ExtraName, PackageName, Requirement};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, skip_serializing_none};
use std::cmp::Ordering;
use std::collections::BTreeSet;
use url::Url;
#[serde_as]
#[skip_serializing_none]
#[derive(Serialize, Deserialize, Eq, PartialEq, Clone, Debug, Hash)]
pub struct PypiPackageData {
pub name: PackageName,
pub version: pep440_rs::Version,
pub url: Url,
#[serde(flatten)]
pub hash: Option<PackageHashes>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub requires_dist: Vec<Requirement>,
pub requires_python: Option<VersionSpecifiers>,
}
#[derive(Clone, Debug, Default)]
pub struct PypiPackageEnvironmentData {
pub extras: BTreeSet<ExtraName>,
}
impl PartialOrd for PypiPackageData {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PypiPackageData {
fn cmp(&self, other: &Self) -> Ordering {
self.name
.cmp(&other.name)
.then_with(|| self.version.cmp(&other.version))
}
}
impl PypiPackageData {
pub fn satisfies(&self, spec: &Requirement) -> bool {
if spec.name != self.name {
return false;
}
match &spec.version_or_url {
None => {}
Some(pep508_rs::VersionOrUrl::Url(_)) => return false,
Some(pep508_rs::VersionOrUrl::VersionSpecifier(spec)) => {
if !spec.contains(&self.version) {
return false;
}
}
}
true
}
}