Skip to content

Commit

Permalink
Add rust-version field to the index
Browse files Browse the repository at this point in the history
  • Loading branch information
cassaundra committed Apr 5, 2023
1 parent 6342381 commit 427d874
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cargo-registry-index/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub struct Crate {
pub yanked: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub links: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub rust_version: Option<String>,
/// The schema version for this entry.
///
/// If this is None, it defaults to version 1. Entries with unknown
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE versions DROP COLUMN rust_version;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE versions ADD COLUMN rust_version VARCHAR;
3 changes: 3 additions & 0 deletions src/controllers/krate/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
let name = new_crate.name;
let vers = &*new_crate.vers;
let links = new_crate.links;
let rust_version = new_crate.rust_version.as_deref();
let repo = new_crate.repository;
let features = new_crate
.features
Expand Down Expand Up @@ -201,6 +202,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
user.id,
hex_cksum.clone(),
links.clone(),
rust_version.map(String::as_str),
)?
.save(conn, &verified_email_address)?;

Expand Down Expand Up @@ -269,6 +271,7 @@ pub async fn publish(app: AppState, req: BytesRequest) -> AppResult<Json<GoodCra
deps: git_deps,
yanked: Some(false),
links,
rust_version: rust_version.map(ToOwned::to_owned),
v,
};
worker::add_crate(git_crate).enqueue(conn)?;
Expand Down
1 change: 1 addition & 0 deletions src/downloads_counter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ mod tests {
self.user.id,
"0000000000000000000000000000000000000000000000000000000000000000".to_string(),
None,
None,
)
.expect("failed to create version")
.save(conn, "ghost@example.com")
Expand Down
4 changes: 4 additions & 0 deletions src/models/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ pub struct Version {
pub published_by: Option<i32>,
pub checksum: String,
pub links: Option<String>,
pub rust_version: Option<String>,
}

#[derive(Insertable, Debug)]
Expand All @@ -38,6 +39,7 @@ pub struct NewVersion {
published_by: i32,
checksum: String,
links: Option<String>,
rust_version: Option<String>,
}

/// The highest version (semver order) and the most recently updated version.
Expand Down Expand Up @@ -139,6 +141,7 @@ impl NewVersion {
published_by: i32,
checksum: String,
links: Option<String>,
rust_version: Option<&str>,
) -> AppResult<Self> {
let features = serde_json::to_value(features)?;

Expand All @@ -151,6 +154,7 @@ impl NewVersion {
published_by,
checksum,
links,
rust_version: rust_version.map(ToOwned::to_owned),
};

new_version.validate_license(license_file)?;
Expand Down
6 changes: 6 additions & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -966,6 +966,12 @@ diesel::table! {
///
/// (Automatically generated by Diesel.)
links -> Nullable<Varchar>,
/// The `rust_version` column of the `versions` table.
///
/// Its SQL type is `Nullable<Varchar>`.
///
/// (Automatically generated by Diesel.)
rust_version -> Nullable<Varchar>,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/tests/builders/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ impl PublishBuilder {
license_file: self.license_file,
repository: None,
links: None,
rust_version: None,
};

(serde_json::to_string(&new_crate).unwrap(), self.tarball)
Expand Down
3 changes: 3 additions & 0 deletions src/tests/builders/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct VersionBuilder<'a> {
yanked: bool,
checksum: String,
links: Option<String>,
rust_version: Option<String>,
}

impl<'a> VersionBuilder<'a> {
Expand All @@ -45,6 +46,7 @@ impl<'a> VersionBuilder<'a> {
yanked: false,
checksum: String::new(),
links: None,
rust_version: None,
}
}

Expand Down Expand Up @@ -103,6 +105,7 @@ impl<'a> VersionBuilder<'a> {
published_by,
self.checksum,
self.links,
self.rust_version.as_deref(),
)?
.save(connection, "someone@example.com")?;

Expand Down
30 changes: 30 additions & 0 deletions src/views/krate_publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ pub struct EncodableCrateUpload {
pub repository: Option<String>,
#[serde(default)]
pub links: Option<String>,
#[serde(default)]
pub rust_version: Option<EncodableRustVersion>,
}

#[derive(PartialEq, Eq, Hash, Serialize, Debug, Deref)]
Expand All @@ -42,6 +44,8 @@ pub struct EncodableDependencyName(pub String);
pub struct EncodableCrateVersion(pub semver::Version);
#[derive(Debug, Deref)]
pub struct EncodableCrateVersionReq(pub String);
#[derive(Debug, Deref, Clone)]
pub struct EncodableRustVersion(pub String);
#[derive(Serialize, Debug, Deref, Default)]
pub struct EncodableKeywordList(pub Vec<EncodableKeyword>);
#[derive(Serialize, Debug, Deref)]
Expand Down Expand Up @@ -177,6 +181,23 @@ impl<'de> Deserialize<'de> for EncodableCrateVersionReq {
}
}

impl<'de> Deserialize<'de> for EncodableRustVersion {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableRustVersion, D::Error> {
let s = String::deserialize(d)?;
match semver::VersionReq::parse(&s) {
// Exclude semver operators like `^` and pre-release identifiers
Ok(_) if s.chars().all(|c| c.is_ascii_digit() || c == '.') => {
Ok(EncodableRustVersion(s))
}
Ok(_) | Err(..) => {
let value = de::Unexpected::Str(&s);
let expected = "a valid rust-version";
Err(de::Error::invalid_value(value, &expected))
}
}
}
}

impl<'de> Deserialize<'de> for EncodableKeywordList {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<EncodableKeywordList, D::Error> {
let inner = <Vec<EncodableKeyword> as Deserialize<'de>>::deserialize(d)?;
Expand Down Expand Up @@ -224,6 +245,15 @@ impl Serialize for EncodableCrateVersionReq {
}
}

impl Serialize for EncodableRustVersion {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&(**self).to_string())
}
}

use diesel::pg::Pg;
use diesel::serialize::{self, Output, ToSql};
use diesel::sql_types::Text;
Expand Down
1 change: 1 addition & 0 deletions src/worker/dump_db/dump-db.toml
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ crate_size = "public"
published_by = "public"
checksum = "public"
links = "public"
rust_version = "public"

[versions_published_by.columns]
version_id = "private"
Expand Down
1 change: 1 addition & 0 deletions src/worker/update_downloads.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ mod test {
user_id,
"0000000000000000000000000000000000000000000000000000000000000000".to_string(),
None,
None,
)
.unwrap();
let version = version.save(conn, "someone@example.com").unwrap();
Expand Down

0 comments on commit 427d874

Please sign in to comment.