2024-03-04 20:18:49 +00:00
|
|
|
use std::{
|
2024-03-24 13:31:11 +00:00
|
|
|
any::Any,
|
2024-03-04 20:18:49 +00:00
|
|
|
collections::{BTreeSet, HashMap},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2024-03-24 13:31:11 +00:00
|
|
|
use url::Url;
|
2024-03-04 20:18:49 +00:00
|
|
|
|
|
|
|
use pesde::{
|
|
|
|
index::{
|
|
|
|
ConfigError, CreatePackageVersionError, CredentialsFn, Index, IndexConfig, IndexFile,
|
|
|
|
IndexFileEntry, IndexPackageError, ScopeOwners, ScopeOwnersError,
|
|
|
|
},
|
|
|
|
manifest::Manifest,
|
|
|
|
package_name::PackageName,
|
|
|
|
};
|
|
|
|
|
|
|
|
/// An in-memory implementation of the [`Index`] trait. Used for testing.
|
2024-03-24 13:31:11 +00:00
|
|
|
#[derive(Debug, Clone)]
|
2024-03-04 20:18:49 +00:00
|
|
|
pub struct InMemoryIndex {
|
|
|
|
packages: HashMap<String, (BTreeSet<u64>, IndexFile)>,
|
2024-03-24 13:31:11 +00:00
|
|
|
url: Url,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for InMemoryIndex {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
packages: HashMap::new(),
|
|
|
|
url: Url::parse("https://example.com").unwrap(),
|
|
|
|
}
|
|
|
|
}
|
2024-03-04 20:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl InMemoryIndex {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_scope(mut self, scope: &str, owners: BTreeSet<u64>) -> Self {
|
|
|
|
self.packages
|
|
|
|
.insert(scope.to_string(), (owners, IndexFile::default()));
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn with_package(mut self, scope: &str, index_file: IndexFileEntry) -> Self {
|
|
|
|
self.packages
|
|
|
|
.entry(scope.to_string())
|
|
|
|
.or_insert_with(|| (BTreeSet::new(), IndexFile::default()))
|
|
|
|
.1
|
2024-03-16 21:36:12 +00:00
|
|
|
.insert(index_file);
|
2024-03-04 20:18:49 +00:00
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Index for InMemoryIndex {
|
|
|
|
fn scope_owners(&self, scope: &str) -> Result<Option<ScopeOwners>, ScopeOwnersError> {
|
|
|
|
Ok(self.packages.get(scope).map(|(owners, _)| owners).cloned())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_scope_for(
|
|
|
|
&mut self,
|
|
|
|
scope: &str,
|
|
|
|
owners: &ScopeOwners,
|
|
|
|
) -> Result<bool, ScopeOwnersError> {
|
|
|
|
self.packages
|
|
|
|
.insert(scope.to_string(), (owners.clone(), IndexFile::default()));
|
|
|
|
Ok(true)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn package(&self, name: &PackageName) -> Result<Option<IndexFile>, IndexPackageError> {
|
|
|
|
Ok(self
|
|
|
|
.packages
|
|
|
|
.get(name.scope())
|
|
|
|
.map(|(_, file)| file.clone()))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn create_package_version(
|
|
|
|
&mut self,
|
|
|
|
manifest: &Manifest,
|
|
|
|
uploader: &u64,
|
2024-03-16 21:36:12 +00:00
|
|
|
) -> Result<Option<IndexFileEntry>, CreatePackageVersionError> {
|
2024-03-04 20:18:49 +00:00
|
|
|
let scope = manifest.name.scope();
|
|
|
|
|
|
|
|
if let Some(owners) = self.scope_owners(scope)? {
|
|
|
|
if !owners.contains(uploader) {
|
|
|
|
return Err(CreatePackageVersionError::MissingScopeOwnership);
|
|
|
|
}
|
|
|
|
} else if !self.create_scope_for(scope, &BTreeSet::from([*uploader]))? {
|
|
|
|
return Err(CreatePackageVersionError::MissingScopeOwnership);
|
|
|
|
}
|
|
|
|
|
|
|
|
let package = self.packages.get_mut(scope).unwrap();
|
|
|
|
|
2024-03-24 13:31:11 +00:00
|
|
|
let entry: IndexFileEntry = manifest.clone().try_into()?;
|
2024-03-16 21:36:12 +00:00
|
|
|
package.1.insert(entry.clone());
|
2024-03-04 20:18:49 +00:00
|
|
|
|
2024-03-16 21:36:12 +00:00
|
|
|
Ok(Some(entry))
|
2024-03-04 20:18:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn config(&self) -> Result<IndexConfig, ConfigError> {
|
|
|
|
Ok(IndexConfig {
|
|
|
|
download: None,
|
2024-03-24 13:31:11 +00:00
|
|
|
api: "http://127.0.0.1:8080".parse().unwrap(),
|
2024-03-04 20:18:49 +00:00
|
|
|
github_oauth_client_id: "".to_string(),
|
|
|
|
custom_registry_allowed: false,
|
|
|
|
git_allowed: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn credentials_fn(&self) -> Option<&Arc<CredentialsFn>> {
|
|
|
|
None
|
|
|
|
}
|
2024-03-24 13:31:11 +00:00
|
|
|
|
|
|
|
fn url(&self) -> &Url {
|
|
|
|
&self.url
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
self
|
|
|
|
}
|
2024-03-04 20:18:49 +00:00
|
|
|
}
|