pesde/registry/src/endpoints/package_versions.rs

55 lines
1.7 KiB
Rust
Raw Normal View History

2024-08-13 20:26:02 +01:00
use std::collections::{BTreeMap, BTreeSet};
2024-07-30 11:37:54 +01:00
use actix_web::{web, HttpResponse, Responder};
2024-11-10 15:43:25 +00:00
use crate::{error::Error, package::PackageResponse, AppState};
2024-08-08 16:59:59 +01:00
use pesde::{
names::PackageName,
2024-11-10 15:43:25 +00:00
source::{
git_index::{read_file, root_tree, GitBasedSource},
pesde::IndexFile,
},
2024-08-08 16:59:59 +01:00
};
2024-07-30 11:37:54 +01:00
pub async fn get_package_versions(
app_state: web::Data<AppState>,
path: web::Path<PackageName>,
) -> Result<impl Responder, Error> {
let name = path.into_inner();
let (scope, name_part) = name.as_str();
2024-11-10 15:43:25 +00:00
let versions: IndexFile = {
let source = app_state.source.lock().await;
let repo = gix::open(source.path(&app_state.project))?;
let tree = root_tree(&repo)?;
match read_file(&tree, [scope, name_part])? {
2024-08-11 15:16:25 +01:00
Some(versions) => toml::de::from_str(&versions)?,
None => return Ok(HttpResponse::NotFound().finish()),
2024-11-10 15:43:25 +00:00
}
};
2024-07-30 11:37:54 +01:00
2024-08-13 20:26:02 +01:00
let mut responses = BTreeMap::new();
for (v_id, entry) in versions {
let info = responses
.entry(v_id.version().clone())
.or_insert_with(|| PackageResponse {
2024-07-30 11:37:54 +01:00
name: name.to_string(),
version: v_id.version().to_string(),
2024-08-13 20:26:02 +01:00
targets: BTreeSet::new(),
2024-07-30 11:37:54 +01:00
description: entry.description.unwrap_or_default(),
published_at: entry.published_at,
license: entry.license.unwrap_or_default(),
2024-08-14 18:55:58 +01:00
authors: entry.authors.clone(),
repository: entry.repository.clone().map(|url| url.to_string()),
2024-08-13 20:26:02 +01:00
});
info.targets.insert(entry.target.into());
info.published_at = info.published_at.max(entry.published_at);
}
Ok(HttpResponse::Ok().json(responses.into_values().collect::<Vec<_>>()))
2024-07-30 11:37:54 +01:00
}