use std::collections::HashMap;
use crate::catalog::{Artifact, CATALOG, Ecosystem};
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ArtifactId {
pub(super) ecosystem: u8,
pub(super) artifact: u8,
}
impl ArtifactId {
#[must_use]
pub fn ecosystem(self) -> &'static Ecosystem {
&CATALOG[self.ecosystem as usize]
}
#[must_use]
pub fn artifact(self) -> &'static Artifact {
&self.ecosystem().artifacts[self.artifact as usize]
}
#[must_use]
pub fn spec(self) -> String {
format!("{}.{}", self.ecosystem().id, self.artifact().slug())
}
#[must_use]
pub fn from_spec(spec: &str) -> Option<Self> {
let (ecosystem_id, artifact_slug) = spec.split_once('.')?;
let index = CATALOG.iter().position(|ecosystem| ecosystem.id == ecosystem_id)?;
let offset = CATALOG[index]
.artifacts
.iter()
.position(|artifact| artifact.slug() == artifact_slug)?;
Some(Self {
ecosystem: u8::try_from(index).ok()?,
artifact: u8::try_from(offset).ok()?,
})
}
}
#[derive(Debug, Clone)]
pub struct Selection {
ecosystems: u32,
overrides: HashMap<ArtifactId, bool>,
permissive: bool,
}
impl Default for Selection {
fn default() -> Self {
Self::all()
}
}
impl Selection {
#[must_use]
pub fn all() -> Self {
let ecosystems = if CATALOG.len() >= u32::BITS as usize {
u32::MAX
} else {
(1 << CATALOG.len()) - 1
};
Self {
ecosystems,
overrides: HashMap::new(),
permissive: false,
}
}
#[must_use]
pub fn only<'a>(ids: impl IntoIterator<Item = &'a str>) -> (Self, Vec<String>) {
let mut selection = Self {
ecosystems: 0,
overrides: HashMap::new(),
permissive: false,
};
let mut unknown = Vec::new();
for id in ids {
match CATALOG.iter().position(|ecosystem| ecosystem.id == id) {
Some(index) => selection.ecosystems |= 1 << index,
None => unknown.push(id.to_owned()),
}
}
(selection, unknown)
}
#[must_use]
pub fn permissive(mut self) -> Self {
self.permissive = true;
self
}
pub fn set(&mut self, spec: &str, enabled: bool) -> bool {
let (ecosystem_id, artifact_slug) = match spec.split_once('.') {
Some((ecosystem_id, artifact_slug)) => (ecosystem_id, Some(artifact_slug)),
None => (spec, None),
};
let Some(index) = CATALOG.iter().position(|ecosystem| ecosystem.id == ecosystem_id) else {
return false;
};
let ecosystem = &CATALOG[index];
let mut matched = false;
for (offset, artifact) in ecosystem.artifacts.iter().enumerate() {
if artifact_slug.is_some_and(|slug| artifact.slug() != slug) {
continue;
}
if artifact_slug.is_none()
&& enabled
&& crate::catalog::is_dependency_artifact(&format!("{ecosystem_id}.{}", artifact.slug()))
{
continue;
}
let Ok(ecosystem_index) = u8::try_from(index) else {
continue;
};
let Ok(artifact_index) = u8::try_from(offset) else {
continue;
};
self.overrides.insert(
ArtifactId {
ecosystem: ecosystem_index,
artifact: artifact_index,
},
enabled,
);
matched = true;
}
if matched && enabled {
self.ecosystems |= 1 << index;
}
matched
}
#[must_use]
pub fn ecosystem_enabled(&self, id: ArtifactId) -> bool {
self.ecosystems & (1 << id.ecosystem) != 0
}
#[must_use]
pub fn is_enabled(&self, id: ArtifactId) -> bool {
if !self.ecosystem_enabled(id) {
return false;
}
self.overrides
.get(&id)
.copied()
.unwrap_or_else(|| self.permissive || id.artifact().default_on)
}
}