Skip to main content

facto_core/registries/
trait_def.rs

1use std::future::Future;
2use std::pin::Pin;
3
4use thiserror::Error;
5
6use crate::models::*;
7
8#[derive(Debug, Error)]
9pub enum RegistryError {
10    #[error("http error: {0}")]
11    Http(#[from] reqwest::Error),
12    #[error("timeout")]
13    Timeout,
14    #[error("not found")]
15    NotFound,
16    #[error("rate limited")]
17    RateLimited,
18    #[error("parse error: {0}")]
19    Parse(String),
20    #[error("not supported")]
21    NotSupported,
22}
23
24pub type RegistryResult<T> = Result<T, RegistryError>;
25
26/// A package registry backend (e.g., PyPI, npm, crates.io).
27///
28/// Implementations provide package metadata, version listings, and search
29/// across a single registry.
30pub trait Registry: Send + Sync {
31    /// Unique registry identifier (e.g., "pypi", "npm").
32    fn id(&self) -> &str;
33    /// Human-readable registry name.
34    fn display_name(&self) -> &str;
35
36    /// Fetch package metadata (description, latest version, URLs).
37    fn get_package<'a>(
38        &'a self,
39        name: &'a str,
40    ) -> Pin<Box<dyn Future<Output = RegistryResult<PackageInfo>> + Send + 'a>>;
41    /// List all published versions, newest first.
42    fn get_versions<'a>(
43        &'a self,
44        name: &'a str,
45    ) -> Pin<Box<dyn Future<Output = RegistryResult<Vec<VersionInfo>>> + Send + 'a>>;
46    /// Search packages by query string.
47    fn search<'a>(
48        &'a self,
49        query: &'a str,
50        limit: usize,
51    ) -> Pin<Box<dyn Future<Output = RegistryResult<Vec<SearchResult>>> + Send + 'a>>;
52
53    /// Whether `get_latest_version` yields a reliable result. Registries whose
54    /// versions can be ordered by semver return `true`. Docker Hub orders
55    /// free-form, non-semver tags by push date, so "latest" is not meaningful;
56    /// it returns `false` and callers should use `list_versions` instead.
57    fn supports_latest_version(&self) -> bool {
58        true
59    }
60}