Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
Co-authored-by: Joshua Nelson <joshua@yottadb.com>
  • Loading branch information
Kixiron and Joshua Nelson committed May 27, 2020
1 parent 9655e92 commit f5f1982
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 117 deletions.
69 changes: 25 additions & 44 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use iron::prelude::*;
use iron::{status, Url};
use postgres::Connection;
use router::Router;
use serde::ser::{Serialize, SerializeStruct, Serializer};
use serde::{
ser::{SerializeStruct, Serializer},
Serialize,
};
use serde_json::Value;

// TODO: Add target name and versions
Expand Down Expand Up @@ -51,7 +54,15 @@ impl Serialize for CrateDetails {
where
S: Serializer,
{
let mut state = serializer.serialize_struct("CrateDetails", 28)?;
// Make sure that the length parameter passed to serde is correct by
// adding the someness of `readme` and `rustdoc` to the total. `true`
// is 1 and `false` is 0, so it increments if the value is some (and therefore
// needs to be serialized)
let mut state = serializer.serialize_struct(
"CrateDetails",
26 + self.readme.is_some() as usize + self.rustdoc.is_some() as usize,
)?;

state.serialize_field("metadata", &self.metadata)?;
state.serialize_field("name", &self.name)?;
state.serialize_field("version", &self.version)?;
Expand Down Expand Up @@ -94,7 +105,7 @@ impl Serialize for CrateDetails {
}
}

#[derive(Debug, Eq, PartialEq, serde::Serialize)]
#[derive(Debug, Eq, PartialEq, Serialize)]
pub struct Release {
pub version: String,
pub build_status: bool,
Expand Down Expand Up @@ -626,7 +637,7 @@ mod tests {
let time = time::get_time();
let mut details = CrateDetails::default_tester(time);

let correct_json = json!({
let mut correct_json = json!({
"name": "rcc",
"version": "100.0.0",
"description": null,
Expand Down Expand Up @@ -665,50 +676,20 @@ mod tests {

assert_eq!(correct_json, serde_json::to_value(&details).unwrap());

details.description = Some("serde does stuff".to_string());
details.owners = vec![("Owner".to_string(), "owner@ownsstuff.com".to_string())];

let authors = vec![("Somebody".to_string(), "somebody@somebody.com".to_string())];
let owners = vec![("Owner".to_string(), "owner@ownsstuff.com".to_string())];
let description = "serde does stuff".to_string();

correct_json["description"] = Value::String(description.clone());
correct_json["owners"] = serde_json::to_value(&owners).unwrap();
correct_json["authors_json"] = serde_json::to_value(&authors).unwrap();
correct_json["authors"] = serde_json::to_value(&authors).unwrap();

details.description = Some(description);
details.owners = owners;
details.authors_json = Some(serde_json::to_value(&authors).unwrap());
details.authors = authors;

let correct_json = json!({
"name": "rcc",
"version": "100.0.0",
"description": "serde does stuff",
"authors": [["Somebody", "somebody@somebody.com"]],
"owners": [["Owner", "owner@ownsstuff.com"]],
"authors_json": [["Somebody", "somebody@somebody.com"]],
"dependencies": null,
"release_time": super::super::duration_to_str(time),
"build_status": true,
"last_successful_build": null,
"rustdoc_status": true,
"repository_url": null,
"homepage_url": null,
"keywords": null,
"have_examples": true,
"target_name": "x86_64-unknown-linux-gnu",
"releases": [],
"github": true,
"yanked": false,
"github_stars": null,
"github_forks": null,
"github_issues": null,
"metadata": {
"name": "serde",
"version": "1.0.0",
"description": "serde does stuff",
"target_name": null,
"rustdoc_status": true,
"default_target": "x86_64-unknown-linux-gnu"
},
"is_library": true,
"doc_targets": [],
"license": null,
"documentation_url": null
});

assert_eq!(correct_json, serde_json::to_value(&details).unwrap());
}

Expand Down
35 changes: 24 additions & 11 deletions src/web/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,12 @@ impl<T: Serialize> Serialize for Page<T> {
where
S: Serializer,
{
let mut state = serializer.serialize_struct("Page", 10)?;
// Make sure that the length parameter passed to serde is correct by
// adding the someness of the global alert to the total. `true`
// is 1 and `false` is 0, so it increments if the value is some (and therefore
// needs to be serialized)
let mut state =
serializer.serialize_struct("Page", 9 + crate::GLOBAL_ALERT.is_some() as usize)?;

if let Some(ref title) = self.title {
state.serialize_field("title", title)?;
Expand All @@ -132,10 +137,7 @@ impl<T: Serialize> Serialize for Page<T> {
state.serialize_field("cratesfyi_version", crate::BUILD_VERSION)?;
state.serialize_field(
"cratesfyi_version_safe",
&crate::BUILD_VERSION
.replace(" ", "-")
.replace("(", "")
.replace(")", ""),
&build_version_safe(crate::BUILD_VERSION),
)?;
state.serialize_field("varss", &self.varss)?;
state.serialize_field("varsb", &self.varsb)?;
Expand All @@ -145,10 +147,15 @@ impl<T: Serialize> Serialize for Page<T> {
}
}

fn build_version_safe(version: &str) -> String {
version.replace(" ", "-").replace("(", "").replace(")", "")
}

#[cfg(test)]
mod tests {
use super::super::releases::{self, Release};
use super::*;
use iron::Url;
use serde_json::json;

#[test]
Expand Down Expand Up @@ -193,10 +200,7 @@ mod tests {
"varsi": { "test3": 1337 },
"rustc_resource_suffix": &*RUSTC_RESOURCE_SUFFIX,
"cratesfyi_version": crate::BUILD_VERSION,
"cratesfyi_version_safe": crate::BUILD_VERSION
.replace(" ", "-")
.replace("(", "")
.replace(")", ""),
"cratesfyi_version_safe": build_version_safe(crate::BUILD_VERSION),
"has_global_alert": crate::GLOBAL_ALERT.is_some()
});

Expand Down Expand Up @@ -238,18 +242,27 @@ mod tests {
fn serialize_global_alert() {
let alert = GlobalAlert {
url: "http://www.hasthelargehadroncolliderdestroyedtheworldyet.com/",
text: "THE WORLD IS ENDING",
text: "THE WORLD WILL SOON END",
css_class: "THE END IS NEAR",
fa_icon: "https://gph.is/1uOvmqR",
};

let correct_json = json!({
"url": "http://www.hasthelargehadroncolliderdestroyedtheworldyet.com/",
"text": "THE WORLD IS ENDING",
"text": "THE WORLD WILL SOON END",
"css_class": "THE END IS NEAR",
"fa_icon": "https://gph.is/1uOvmqR"
});

assert_eq!(correct_json, serde_json::to_value(&alert).unwrap());
}

#[test]
fn build_version_url_safe() {
let safe = format!(
"https://docs.rs/builds/{}",
build_version_safe(crate::BUILD_VERSION)
);
assert!(Url::parse(&safe).is_ok());
}
}
18 changes: 10 additions & 8 deletions src/web/releases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1006,14 +1006,16 @@ mod tests {

#[test]
fn serialize_releases() {
let time = time::get_time();
let current_time = time::get_time();
let now = time::at(current_time);

let mut release = Release {
name: "serde".to_string(),
version: "0.0.0".to_string(),
description: Some("serde makes things other things".to_string()),
target_name: Some("x86_64-pc-windows-msvc".to_string()),
rustdoc_status: true,
release_time: time,
release_time: current_time,
stars: 100,
};

Expand All @@ -1023,8 +1025,8 @@ mod tests {
"description": "serde makes things other things",
"target_name": "x86_64-pc-windows-msvc",
"rustdoc_status": true,
"release_time": duration_to_str(time),
"release_time_rfc3339": time::at(time).rfc3339().to_string(),
"release_time": duration_to_str(current_time),
"release_time_rfc3339": now.rfc3339().to_string(),
"stars": 100
});

Expand All @@ -1037,8 +1039,8 @@ mod tests {
"description": "serde makes things other things",
"target_name": null,
"rustdoc_status": true,
"release_time": duration_to_str(time),
"release_time_rfc3339": time::at(time).rfc3339().to_string(),
"release_time": duration_to_str(current_time),
"release_time_rfc3339": now.rfc3339().to_string(),
"stars": 100
});

Expand All @@ -1051,8 +1053,8 @@ mod tests {
"description": null,
"target_name": null,
"rustdoc_status": true,
"release_time": duration_to_str(time),
"release_time_rfc3339": time::at(time).rfc3339().to_string(),
"release_time": duration_to_str(current_time),
"release_time_rfc3339": now.rfc3339().to_string(),
"stars": 100
});

Expand Down
59 changes: 5 additions & 54 deletions src/web/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,55 +29,6 @@ struct File {
}

/// A list of source files
///
///
/// Rust:
///
/// ```ignore
/// FileList {
/// metadata: MetaData {
/// name: "rcc",
/// version: "0.0.0",
/// description: Some("it compiles an unholy language"),
/// target_name: None,
/// rustdoc_status: true,
/// default_target: "x86_64-unknown-linux-gnu",
/// },
/// files: vec![
/// File {
/// name: "main.rs",
/// file_type: FileType::RustSource,
/// },
/// File {
/// name: "lib.rs",
/// file_type: FileType::RustSource,
/// },
/// ],
/// }
/// ```
///
/// Json:
///
/// ```json
/// {
/// "metadata": {
/// "name": "rcc",
/// "version": "0.0.0",
/// "description": "it compiles an unholy language",
/// "target_name": null,
/// "rustdoc_status": true,
/// "default_target": "x86_64-unknown-linux-gnu",
/// },
/// "files": [{
/// "name": "main.rs",
/// "file_type_rust_source": true,
/// }, {
/// "name": "lib.rs",
/// "file_type_rust_source": true,
/// }],
/// }
/// ```
///
struct FileList {
metadata: MetaData,
files: Vec<File>,
Expand All @@ -93,7 +44,7 @@ impl Serialize for FileList {

let mut files = Vec::with_capacity(self.files.len());
for file in &self.files {
let mut map = HashMap::with_capacity(3);
let mut map = HashMap::with_capacity(2);
map.insert("name", Value::String(file.name.to_owned()));

let file_type = match file.file_type {
Expand All @@ -119,10 +70,10 @@ impl FileList {
///
/// ```text
/// [
/// ["text/plain",".gitignore"],
/// ["text/x-c","src/reseeding.rs"],
/// ["text/x-c","src/lib.rs"],
/// ["text/x-c","README.md"],
/// ["text/plain", ".gitignore"],
/// ["text/x-c", "src/reseeding.rs"],
/// ["text/x-c", "src/lib.rs"],
/// ["text/x-c", "README.md"],
/// ...
/// ]
/// ```
Expand Down

0 comments on commit f5f1982

Please sign in to comment.