aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/repository/repository_interface.rs
blob: cece0d6f788e557ac86678cea57041a41a84a109 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//! ref: composer/src/Composer/Repository/RepositoryInterface.php

use crate::package::BasePackageHandle;
use crate::package::PackageInterfaceHandle;
use crate::repository::AdvisoryProviderInterface;
use indexmap::IndexMap;
use shirabe_semver::constraint::AnyConstraint;

pub enum FindPackageConstraint {
    String(String),
    Constraint(AnyConstraint),
}

impl Clone for FindPackageConstraint {
    fn clone(&self) -> Self {
        match self {
            Self::String(s) => Self::String(s.clone()),
            Self::Constraint(c) => Self::Constraint(c.clone()),
        }
    }
}

#[derive(Debug)]
pub struct LoadPackagesResult {
    pub names_found: Vec<String>,
    pub packages: IndexMap<String, BasePackageHandle>,
}

#[derive(Debug, Clone)]
pub enum AbandonedInfo {
    Replacement(String),
    Abandoned,
}

#[derive(Debug, Clone)]
pub struct SearchResult {
    pub name: String,
    pub description: Option<String>,
    pub abandoned: Option<AbandonedInfo>,
    pub url: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ProviderInfo {
    pub name: String,
    pub description: Option<String>,
    pub r#type: String,
}

pub const SEARCH_FULLTEXT: i64 = 0;
pub const SEARCH_NAME: i64 = 1;
pub const SEARCH_VENDOR: i64 = 2;

pub trait RepositoryInterface: std::fmt::Debug {
    // count/has_package take &mut self (and has_package returns Result) because PHP's
    // ArrayRepository::count()/hasPackage() late-bind $this->initialize() to the concrete
    // repository class, which lazily loads packages and can throw; lazy repositories need the
    // same guard here (see FilesystemRepository/PlatformRepository/ComposerRepository).
    fn count(&mut self) -> anyhow::Result<usize>;

    fn has_package(&mut self, package: PackageInterfaceHandle) -> anyhow::Result<bool>;

    fn find_package(
        &mut self,
        name: &str,
        constraint: FindPackageConstraint,
    ) -> anyhow::Result<Option<BasePackageHandle>>;

    fn find_packages(
        &mut self,
        name: &str,
        constraint: Option<FindPackageConstraint>,
    ) -> anyhow::Result<Vec<BasePackageHandle>>;

    fn get_packages(&mut self) -> anyhow::Result<Vec<BasePackageHandle>>;

    fn load_packages(
        &mut self,
        package_name_map: IndexMap<String, Option<AnyConstraint>>,
        acceptable_stabilities: IndexMap<String, i64>,
        stability_flags: IndexMap<String, i64>,
        already_loaded: IndexMap<String, IndexMap<String, PackageInterfaceHandle>>,
    ) -> anyhow::Result<LoadPackagesResult>;

    fn search(
        &mut self,
        query: String,
        mode: i64,
        r#type: Option<String>,
    ) -> anyhow::Result<Vec<SearchResult>>;

    fn get_providers(
        &mut self,
        package_name: String,
    ) -> anyhow::Result<IndexMap<String, ProviderInfo>>;

    // PHP's getRepoName() can throw: ArrayRepository::getRepoName() counts through the
    // late-bound $this->initialize(), which is fallible in subclasses that read files
    // (FilesystemRepository, PackageRepository).
    fn get_repo_name(&self) -> anyhow::Result<String>;

    fn as_advisory_provider(&self) -> Option<&dyn AdvisoryProviderInterface> {
        None
    }

    fn as_advisory_provider_mut(&mut self) -> Option<&mut dyn AdvisoryProviderInterface> {
        None
    }

    fn as_installed_repository_interface(
        &self,
    ) -> Option<&dyn crate::repository::InstalledRepositoryInterface> {
        None
    }

    fn as_installed_repository_interface_mut(
        &mut self,
    ) -> Option<&mut dyn crate::repository::InstalledRepositoryInterface> {
        None
    }

    fn as_writable_repository_interface_mut(
        &mut self,
    ) -> Option<&mut dyn crate::repository::WritableRepositoryInterface> {
        None
    }

    fn as_any(&self) -> &dyn std::any::Any;

    /// Injects this repository's own weak handle so that `add_package` can wire package ->
    /// repository back-references (PHP `setRepository($this)`). Called once when the repository is
    /// wrapped in a [`RepositoryInterfaceHandle`](crate::repository::RepositoryInterfaceHandle).
    /// Wrapper repositories forward the same weak (the outermost handle) to their inner
    /// `ArrayRepository`.
    fn set_self_handle(&self, weak: crate::repository::RepositoryInterfaceWeakHandle) {
        let _ = weak;
    }
}