Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced rustc_serialize with serde #709

Merged
merged 5 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 27 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ edition = "2018"

[dependencies]
log = "0.4"
rustc-serialize = "0.3"
regex = "1"
structopt = "0.3"
crates-index-diff = "7"
Expand Down Expand Up @@ -41,10 +40,14 @@ rustwide = "=0.7.0"
mime_guess = "2"
dotenv = "0.15"

# Data serialization and deserialization
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"

# iron dependencies
iron = "0.5"
router = "0.5"
handlebars-iron = "0.22"
handlebars-iron = "0.25"
params = "0.8"
staticfile = { version = "0.4", features = [ "cache" ] }
tempfile = "3.1.0"
Expand All @@ -60,7 +63,7 @@ path-slash = "0.1.1"

[dependencies.postgres]
version = "0.15"
features = [ "with-time", "with-rustc-serialize" ]
features = ["with-time", "with-serde_json"]

[dev-dependencies]
once_cell = "1.2.0"
Expand Down
20 changes: 10 additions & 10 deletions src/db/add_package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ use crate::{
index::api::{CrateOwner, RegistryCrateData},
utils::MetadataPackage,
};

use log::debug;
use postgres::Connection;
use regex::Regex;
use rustc_serialize::json::{Json, ToJson};
use semver::Version;
use serde_json::Value;
use slug::slugify;

/// Adds a package into database.
Expand All @@ -31,7 +30,7 @@ pub(crate) fn add_package_into_database(
source_dir: &Path,
res: &BuildResult,
default_target: &str,
source_files: Option<Json>,
source_files: Option<Value>,
doc_targets: Vec<String>,
cratesio_data: &RegistryCrateData,
has_docs: bool,
Expand Down Expand Up @@ -88,7 +87,7 @@ pub(crate) fn add_package_into_database(
&crate_id,
&metadata_pkg.version,
&cratesio_data.release_time,
&dependencies.to_json(),
&serde_json::to_value(&dependencies)?,
&metadata_pkg.package_name(),
&cratesio_data.yanked,
&res.successful,
Expand All @@ -100,12 +99,12 @@ pub(crate) fn add_package_into_database(
&metadata_pkg.description,
&rustdoc,
&readme,
&metadata_pkg.authors.to_json(),
&metadata_pkg.keywords.to_json(),
&serde_json::to_value(&metadata_pkg.authors)?,
&serde_json::to_value(&metadata_pkg.keywords)?,
&has_examples,
&cratesio_data.downloads,
&source_files,
&doc_targets.to_json(),
&serde_json::to_value(&doc_targets)?,
&is_library,
&res.rustc_version,
&metadata_pkg.documentation,
Expand All @@ -122,20 +121,21 @@ pub(crate) fn add_package_into_database(
// Update versions
{
let metadata_version = Version::parse(&metadata_pkg.version)?;
let mut versions: Json = conn
let mut versions: Value = conn
.query("SELECT versions FROM crates WHERE id = $1", &[&crate_id])?
.get(0)
.get(0);
if let Some(versions_array) = versions.as_array_mut() {
let mut found = false;
for version in versions_array.clone() {
let version = Version::parse(version.as_string().unwrap())?;
let version = Version::parse(version.as_str().unwrap())?;
if version == metadata_version {
found = true;
}
}

if !found {
versions_array.push(metadata_pkg.version.to_string().to_json());
versions_array.push(Value::String(metadata_pkg.version.clone()));
}
}
let _ = conn.query(
Expand Down
21 changes: 12 additions & 9 deletions src/db/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::error::Result;
use crate::storage::Storage;
use postgres::Connection;

use rustc_serialize::json::{Json, ToJson};
use serde_json::Value;
use std::path::{Path, PathBuf};

pub(crate) use crate::storage::Blob;
Expand All @@ -30,21 +30,24 @@ pub fn add_path_into_database<P: AsRef<Path>>(
conn: &Connection,
prefix: &str,
path: P,
) -> Result<Json> {
) -> Result<Value> {
let mut backend = Storage::new(conn);
let file_list = backend.store_all(conn, prefix, path.as_ref())?;
file_list_to_json(file_list.into_iter().collect())
}

fn file_list_to_json(file_list: Vec<(PathBuf, String)>) -> Result<Json> {
let mut file_list_json: Vec<Json> = Vec::new();
fn file_list_to_json(file_list: Vec<(PathBuf, String)>) -> Result<Value> {
let mut file_list_json: Vec<Value> = Vec::new();

for file in file_list {
let mut v: Vec<String> = Vec::with_capacity(2);
v.push(file.1);
v.push(file.0.into_os_string().into_string().unwrap());
file_list_json.push(v.to_json());
let mut v = Vec::with_capacity(2);
v.push(Value::String(file.1));
v.push(Value::String(
file.0.into_os_string().into_string().unwrap(),
));

file_list_json.push(Value::Array(v));
}

Ok(file_list_json.to_json())
Ok(Value::Array(file_list_json))
}
17 changes: 6 additions & 11 deletions src/docbuilder/crates.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use crate::error::Result;
use failure::err_msg;
use rustc_serialize::json::Json;
use std::fs;
use serde_json::Value;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::PathBuf;
use std::{fs, path::PathBuf, str::FromStr};

fn crates_from_file<F>(path: &PathBuf, func: &mut F) -> Result<()>
where
Expand All @@ -24,7 +23,7 @@ where
continue;
};

let data = if let Ok(data) = Json::from_str(line.trim()) {
let data = if let Ok(data) = Value::from_str(line.trim()) {
data
} else {
continue;
Expand All @@ -35,19 +34,15 @@ where
.ok_or_else(|| err_msg("Not a JSON object"))?;
let crate_name = obj
.get("name")
.and_then(|n| n.as_string())
.and_then(|n| n.as_str())
.ok_or_else(|| err_msg("`name` not found in JSON object"))?;
let vers = obj
.get("vers")
.and_then(|n| n.as_string())
.and_then(|n| n.as_str())
.ok_or_else(|| err_msg("`vers` not found in JSON object"))?;

// Skip yanked crates
if obj
.get("yanked")
.and_then(|n| n.as_boolean())
.unwrap_or(false)
{
if obj.get("yanked").and_then(|n| n.as_bool()).unwrap_or(false) {
continue;
}

Expand Down
2 changes: 1 addition & 1 deletion src/docbuilder/limits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use postgres::Connection;
use std::collections::BTreeMap;
use std::time::Duration;

#[derive(Debug, PartialEq)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Limits {
memory: usize,
targets: usize,
Expand Down
4 changes: 2 additions & 2 deletions src/docbuilder/rustwide_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use crate::utils::{copy_doc_dir, parse_rustc_version, CargoMetadata};
use failure::ResultExt;
use log::{debug, info, warn, LevelFilter};
use postgres::Connection;
use rustc_serialize::json::ToJson;
use rustwide::cmd::{Command, SandboxBuilder};
use rustwide::logging::{self, LogStorage};
use rustwide::toolchain::ToolchainError;
use rustwide::{Build, Crate, Toolchain, Workspace, WorkspaceBuilder};
use serde_json::Value;
use std::borrow::Cow;
use std::collections::HashSet;
use std::path::Path;
Expand Down Expand Up @@ -246,7 +246,7 @@ impl RustwideBuilder {
conn.query(
"INSERT INTO config (name, value) VALUES ('rustc_version', $1) \
ON CONFLICT (name) DO UPDATE SET value = $1;",
&[&self.rustc_version.to_json()],
&[&Value::String(self.rustc_version.clone())],
)?;

Ok(())
Expand Down