diff --git a/registry/src/endpoints/package_version.rs b/registry/src/endpoints/package_version.rs index 825ac12..7c27f53 100644 --- a/registry/src/endpoints/package_version.rs +++ b/registry/src/endpoints/package_version.rs @@ -71,7 +71,7 @@ pub async fn get_package_version( let (scope, name_part) = name.as_str(); - let entries: IndexFile = { + let file: IndexFile = { let source = app_state.source.lock().await; let repo = gix::open(source.path(&app_state.project))?; let tree = root_tree(&repo)?; @@ -84,14 +84,15 @@ pub async fn get_package_version( let Some((v_id, entry, targets)) = ({ let version = match version { - VersionRequest::Latest => match entries.keys().map(|k| k.version()).max() { + VersionRequest::Latest => match file.entries.keys().map(|k| k.version()).max() { Some(latest) => latest.clone(), None => return Ok(HttpResponse::NotFound().finish()), }, VersionRequest::Specific(version) => version, }; - let versions = entries + let versions = file + .entries .iter() .filter(|(v_id, _)| *v_id.version() == version); diff --git a/registry/src/endpoints/package_versions.rs b/registry/src/endpoints/package_versions.rs index 4226807..93c8311 100644 --- a/registry/src/endpoints/package_versions.rs +++ b/registry/src/endpoints/package_versions.rs @@ -19,7 +19,7 @@ pub async fn get_package_versions( let (scope, name_part) = name.as_str(); - let versions: IndexFile = { + let file: IndexFile = { let source = app_state.source.lock().await; let repo = gix::open(source.path(&app_state.project))?; let tree = root_tree(&repo)?; @@ -32,7 +32,7 @@ pub async fn get_package_versions( let mut responses = BTreeMap::new(); - for (v_id, entry) in versions { + for (v_id, entry) in file.entries { let info = responses .entry(v_id.version().clone()) .or_insert_with(|| PackageResponse { diff --git a/registry/src/endpoints/publish_version.rs b/registry/src/endpoints/publish_version.rs index 42b21e2..eb1fadd 100644 --- a/registry/src/endpoints/publish_version.rs +++ b/registry/src/endpoints/publish_version.rs @@ -371,7 +371,7 @@ pub async fn publish_package( } }; - let mut entries: IndexFile = + let mut file: IndexFile = toml::de::from_str(&read_file(&gix_tree, [scope, name])?.unwrap_or_default())?; let new_entry = IndexFileEntry { @@ -386,11 +386,12 @@ pub async fn publish_package( dependencies, }; - let this_version = entries + let this_version = file + .entries .keys() .find(|v_id| *v_id.version() == manifest.version); if let Some(this_version) = this_version { - let other_entry = entries.get(this_version).unwrap(); + let other_entry = file.entries.get(this_version).unwrap(); // description cannot be different - which one to render in the "Recently published" list? // the others cannot be different because what to return from the versions endpoint? @@ -406,7 +407,8 @@ pub async fn publish_package( } } - if entries + if file + .entries .insert( VersionId::new(manifest.version.clone(), manifest.target.kind()), new_entry.clone(), @@ -422,7 +424,7 @@ pub async fn publish_package( let reference = repo.find_reference(&refspec)?; { - let index_content = toml::to_string(&entries)?; + let index_content = toml::to_string(&file)?; let mut blob_writer = repo.blob_writer(None)?; blob_writer.write_all(index_content.as_bytes())?; oids.push((name, blob_writer.commit()?)); diff --git a/registry/src/endpoints/search.rs b/registry/src/endpoints/search.rs index cb59eeb..0047a9d 100644 --- a/registry/src/endpoints/search.rs +++ b/registry/src/endpoints/search.rs @@ -68,10 +68,11 @@ pub async fn search_packages( .unwrap(); let (scope, name) = id.as_str(); - let versions: IndexFile = + let file: IndexFile = toml::de::from_str(&read_file(&tree, [scope, name]).unwrap().unwrap()).unwrap(); - let (latest_version, entry) = versions + let (latest_version, entry) = file + .entries .iter() .max_by_key(|(v_id, _)| v_id.version()) .unwrap(); @@ -79,17 +80,19 @@ pub async fn search_packages( PackageResponse { name: id.to_string(), version: latest_version.version().to_string(), - targets: versions + targets: file + .entries .iter() .filter(|(v_id, _)| v_id.version() == latest_version.version()) .map(|(_, entry)| (&entry.target).into()) .collect(), description: entry.description.clone().unwrap_or_default(), - published_at: versions + published_at: file + .entries .values() - .max_by_key(|entry| entry.published_at) - .unwrap() - .published_at, + .map(|entry| entry.published_at) + .max() + .unwrap(), license: entry.license.clone().unwrap_or_default(), authors: entry.authors.clone(), repository: entry.repository.clone().map(|url| url.to_string()), diff --git a/registry/src/search.rs b/registry/src/search.rs index bfcbba3..abb2419 100644 --- a/registry/src/search.rs +++ b/registry/src/search.rs @@ -104,7 +104,7 @@ pub async fn make_search( pin!(stream); while let Some((pkg_name, mut file)) = stream.next().await { - let Some((_, latest_entry)) = file.pop_last() else { + let Some((_, latest_entry)) = file.entries.pop_last() else { tracing::error!("no versions found for {pkg_name}"); continue; }; diff --git a/src/source/pesde/mod.rs b/src/source/pesde/mod.rs index 746eef6..0f4e89c 100644 --- a/src/source/pesde/mod.rs +++ b/src/source/pesde/mod.rs @@ -128,7 +128,7 @@ impl PackageSource for PesdePackageSource { } }; - let entries: IndexFile = toml::from_str(&string) + let IndexFile { entries, .. } = toml::from_str(&string) .map_err(|e| Self::ResolveError::Parse(specifier.name.to_string(), e))?; tracing::debug!("{} has {} possible entries", specifier.name, entries.len()); @@ -432,8 +432,20 @@ pub struct IndexFileEntry { pub dependencies: BTreeMap, } +/// The package metadata in the index file +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Default)] +pub struct IndexMetadata {} + /// The index file for a package -pub type IndexFile = BTreeMap; +#[derive(Debug, Serialize, Deserialize, Clone, PartialEq, Eq)] +pub struct IndexFile { + /// Any package-wide metadata + #[serde(default, skip_serializing_if = "crate::util::is_default")] + pub meta: IndexMetadata, + /// The entries in the index file + #[serde(flatten)] + pub entries: BTreeMap, +} /// Errors that can occur when interacting with the pesde package source pub mod errors { diff --git a/src/util.rs b/src/util.rs index 3baca73..cb942d9 100644 --- a/src/util.rs +++ b/src/util.rs @@ -83,3 +83,7 @@ pub fn deserialize_git_like_url<'de, D: Deserializer<'de>>( pub fn hash>(struc: S) -> String { format!("{:x}", Sha256::digest(struc.as_ref())) } + +pub fn is_default(t: &T) -> bool { + t == &T::default() +}