Skip to content

Commit

Permalink
WIP stubs of db helpers and FIXMEs for where they are needed.
Browse files Browse the repository at this point in the history
  • Loading branch information
onelson committed Jun 7, 2021
1 parent 56f6bb5 commit 915c034
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
34 changes: 29 additions & 5 deletions src/database/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use crate::package_index::{Dependency, PackageIndex, PackageVersion};
use rusqlite::{named_params, params, Connection};
use semver::Version;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

/// Prepare the database schema.
pub fn init(conn: &Connection) -> crate::Result<()> {
pub(crate) fn init(conn: &Connection) -> crate::Result<()> {
conn.execute_batch(
r#"
BEGIN;
Expand Down Expand Up @@ -66,7 +67,7 @@ pub fn backfill_db(conn: &mut Connection, index: &PackageIndex) -> crate::Result
Ok(())
}

pub fn publish(conn: &mut Connection, new_crate: &NewCrate) -> crate::Result<()> {
pub(crate) fn publish(conn: &mut Connection, new_crate: &NewCrate) -> crate::Result<()> {
let tx = conn.transaction()?;
let crate_id = if let Ok(1) = tx.execute(
"INSERT INTO crates (name) VALUES (?)",
Expand Down Expand Up @@ -114,7 +115,7 @@ pub fn publish(conn: &mut Connection, new_crate: &NewCrate) -> crate::Result<()>
Ok(())
}

pub fn _set_yanked(
pub(crate) fn set_yanked(
conn: &mut Connection,
crate_name: &str,
version: &semver::Version,
Expand Down Expand Up @@ -155,7 +156,7 @@ pub fn _set_yanked(
/// This type is "borrowed" from `cargo`'s `crates-io` crate.
/// <https://github.com/rust-lang/cargo/blob/master/crates/crates-io/lib.rs>
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NewCrate {
pub(crate) struct NewCrate {
pub name: String,
pub vers: String,
pub deps: Vec<NewCrateDependency>,
Expand All @@ -178,7 +179,7 @@ pub struct NewCrate {
/// This type is "borrowed" from `cargo`'s `crates-io` crate.
/// <https://github.com/rust-lang/cargo/blob/master/crates/crates-io/lib.rs>
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct NewCrateDependency {
pub(crate) struct NewCrateDependency {
pub optional: bool,
pub default_features: bool,
pub name: String,
Expand Down Expand Up @@ -232,3 +233,26 @@ impl From<PackageVersion> for NewCrate {
}
}
}

/// Get a list of crate names, with their latest version numbers.
///
/// When `include_yanked` is false, yanked versions are excluded from
/// consideration.
/// If all versions of a given crate have been yanked, it will be omitted from
/// this listing entirely.
pub(crate) fn list_crates(include_yanked: bool, limit: usize, offset: usize) {
todo!()
}

/// Get crate details by name and version.
///
/// When `version` isn't specified, the latest (unyanked) version will be
/// returned.
pub(crate) fn get_crate_version(name: &str, version: &Option<Version>) {
todo!()
}

/// List all versions for a given crate (including yanked).
pub(crate) fn get_crate_versions(name: &str) {
todo!()
}
6 changes: 6 additions & 0 deletions src/handlers/frontend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub struct CrateDetailTemplate {

#[get("/")]
pub async fn landing(index: web::Data<Mutex<PackageIndex>>) -> Result<LandingTemplate<'static>> {
// FIXME: read from database

let index = index.lock().unwrap();
let mut names = index.list_crates()?;
names.sort();
Expand Down Expand Up @@ -78,6 +80,8 @@ pub async fn version_list(
path: web::Path<CrateVersionListPath>,
index: web::Data<Mutex<PackageIndex>>,
) -> Result<CrateVersionListTemplate> {
// FIXME: read from database

let index = index.lock().unwrap();
let releases = index
.get_package_versions(&path.crate_name)
Expand Down Expand Up @@ -107,6 +111,8 @@ pub async fn crate_detail(
path: web::Path<CrateDetailPath>,
index: web::Data<Mutex<PackageIndex>>,
) -> Result<CrateDetailTemplate> {
// FIXME: read from database

// 404 if:
// - the crate isn't in the index
// - the crate version doesn't exist
Expand Down
4 changes: 4 additions & 0 deletions src/handlers/registry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub async fn yank(
path: web::Path<Crate>,
package_index: web::Data<Mutex<PackageIndex>>,
) -> ApiResponse {
// FIXME: update database and index (both)
let package_index = package_index.lock().unwrap();
package_index.set_yanked(&path.crate_name, &path.version, true)?;
Ok(HttpResponse::Ok().json(json!({ "ok": true })))
Expand All @@ -103,6 +104,7 @@ pub async fn unyank(
path: web::Path<Crate>,
package_index: web::Data<Mutex<PackageIndex>>,
) -> ApiResponse {
// FIXME: update database and index (both)
let index = package_index.lock().unwrap();
index.set_yanked(&path.crate_name, &path.version, false)?;
Ok(HttpResponse::Ok().json(json!({ "ok": true })))
Expand Down Expand Up @@ -149,6 +151,8 @@ pub async fn search(
query: web::Query<SearchQuery>,
index: web::Data<Mutex<PackageIndex>>,
) -> ApiResponse {
// FIXME: look at database instead of index on disk

let index = index.lock().unwrap();
let names = index.list_crates()?;
let terms: Vec<&str> = query.q.split(&['-', '_', ' ', '\t'][..]).collect();
Expand Down

0 comments on commit 915c034

Please sign in to comment.