Skip to content

Commit 3809f94

Browse files
jyn514Koenraad Verheyden
and
Koenraad Verheyden
authored
Remove unused code (#558)
* remove copy_dir * make utils pub(crate) where possible * remove dead code in rustc version * make more things pub(crate) * only compile ApplyMode::Temporary in test mode * remove unused argument * remove unused `handle_html` argument * don't export metadata * remove `pub` where possible * remove unused import * make create_pool pub(crate) * fix bad merge * pub(crate) doesn't make sense in lib.rs Co-Authored-By: Koenraad Verheyden <mail@koenraadverheyden.com> * address review comments re: copy test * remote system dependencies from metadata this was causing confusion, see #164 * re-comment failing test * fix failing test Co-authored-by: Koenraad Verheyden <mail@koenraadverheyden.com>
1 parent 4abae1a commit 3809f94

18 files changed

+67
-127
lines changed

src/bin/cratesfyi.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ pub fn main() {
239239
let mut count = 1;
240240
let mut total = 0;
241241
while count != 0 {
242-
count = db::file::move_to_s3(&conn, 5_000).expect("Failed to upload batch to S3");
242+
count = db::move_to_s3(&conn, 5_000).expect("Failed to upload batch to S3");
243243
total += count;
244244
eprintln!(
245245
"moved {} rows to s3 in this batch, total moved so far: {}",

src/db/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ fn get_file_list_from_dir<P: AsRef<Path>>(path: P,
4242
}
4343

4444

45-
pub fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<PathBuf>> {
45+
fn get_file_list<P: AsRef<Path>>(path: P) -> Result<Vec<PathBuf>> {
4646
let path = path.as_ref();
4747
let mut files = Vec::new();
4848

src/db/migrate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::borrow::Cow;
1010
#[derive(Copy, Clone)]
1111
enum ApplyMode {
1212
Permanent,
13+
#[cfg(test)]
1314
Temporary,
1415
}
1516

@@ -22,6 +23,7 @@ impl MigrationContext {
2223
fn format_query<'a>(&self, query: &'a str) -> Cow<'a, str> {
2324
match self.apply_mode {
2425
ApplyMode::Permanent => Cow::Borrowed(query),
26+
#[cfg(test)]
2527
ApplyMode::Temporary => {
2628
Cow::Owned(query.replace("CREATE TABLE", "CREATE TEMPORARY TABLE"))
2729
}
@@ -73,6 +75,7 @@ pub fn migrate(version: Option<Version>, conn: &Connection) -> CratesfyiResult<(
7375
migrate_inner(version, conn, ApplyMode::Permanent)
7476
}
7577

78+
#[cfg(test)]
7679
pub fn migrate_temporary(version: Option<Version>, conn: &Connection) -> CratesfyiResult<()> {
7780
migrate_inner(version, conn, ApplyMode::Temporary)
7881
}

src/db/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
pub(crate) use self::add_package::add_package_into_database;
44
pub(crate) use self::add_package::add_build_into_database;
55
pub(crate) use self::add_package::CratesIoData;
6-
pub use self::file::add_path_into_database;
7-
pub use self::migrate::{migrate, migrate_temporary};
6+
pub use self::file::{add_path_into_database, move_to_s3};
7+
pub use self::migrate::migrate;
8+
#[cfg(test)]
9+
pub(crate) use self::migrate::migrate_temporary;
810
pub use self::delete_crate::delete_crate;
911

1012
use postgres::{Connection, TlsMode};
@@ -14,7 +16,7 @@ use r2d2;
1416
use r2d2_postgres;
1517

1618
mod add_package;
17-
pub mod file;
19+
pub(crate) mod file;
1820
mod migrate;
1921
mod delete_crate;
2022
pub mod blacklist;
@@ -29,7 +31,7 @@ pub fn connect_db() -> Result<Connection, Error> {
2931
}
3032

3133

32-
pub fn create_pool() -> r2d2::Pool<r2d2_postgres::PostgresConnectionManager> {
34+
pub(crate) fn create_pool() -> r2d2::Pool<r2d2_postgres::PostgresConnectionManager> {
3335
let db_url = env::var("CRATESFYI_DATABASE_URL")
3436
.expect("CRATESFYI_DATABASE_URL environment variable is not exists");
3537
let manager = r2d2_postgres::PostgresConnectionManager::new(&db_url[..],

src/docbuilder/metadata.rs

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use failure::err_msg;
2222
/// default-target = "x86_64-unknown-linux-gnu"
2323
/// rustc-args = [ "--example-rustc-arg" ]
2424
/// rustdoc-args = [ "--example-rustdoc-arg" ]
25-
/// dependencies = [ "example-system-dependency" ]
2625
/// ```
2726
///
2827
/// You can define one or more fields in your `Cargo.toml`.
@@ -49,11 +48,6 @@ pub struct Metadata {
4948

5049
/// List of command line arguments for `rustdoc`.
5150
pub rustdoc_args: Option<Vec<String>>,
52-
53-
/// System dependencies.
54-
///
55-
/// Docs.rs is running on a Debian jessie.
56-
pub dependencies: Option<Vec<String>>,
5751
}
5852

5953

@@ -69,7 +63,7 @@ impl Metadata {
6963
Err(err_msg("Manifest not found"))
7064
}
7165

72-
pub fn from_manifest<P: AsRef<Path>>(path: P) -> Metadata {
66+
fn from_manifest<P: AsRef<Path>>(path: P) -> Metadata {
7367
use std::fs::File;
7468
use std::io::Read;
7569
let mut f = match File::open(path) {
@@ -93,7 +87,6 @@ impl Metadata {
9387
default_target: None,
9488
rustc_args: None,
9589
rustdoc_args: None,
96-
dependencies: None,
9790
}
9891
}
9992

@@ -122,8 +115,6 @@ impl Metadata {
122115
.and_then(|f| f.iter().map(|v| v.as_str().map(|v| v.to_owned())).collect());
123116
metadata.rustdoc_args = table.get("rustdoc-args").and_then(|f| f.as_array())
124117
.and_then(|f| f.iter().map(|v| v.as_str().map(|v| v.to_owned())).collect());
125-
metadata.dependencies = table.get("dependencies").and_then(|f| f.as_array())
126-
.and_then(|f| f.iter().map(|v| v.as_str().map(|v| v.to_owned())).collect());
127118
}
128119

129120
metadata
@@ -151,7 +142,6 @@ mod test {
151142
default-target = "x86_64-unknown-linux-gnu"
152143
rustc-args = [ "--example-rustc-arg" ]
153144
rustdoc-args = [ "--example-rustdoc-arg" ]
154-
dependencies = [ "example-system-dependency" ]
155145
"#;
156146

157147
let metadata = Metadata::from_str(manifest);
@@ -176,9 +166,5 @@ mod test {
176166
let rustdoc_args = metadata.rustdoc_args.unwrap();
177167
assert_eq!(rustdoc_args.len(), 1);
178168
assert_eq!(rustdoc_args[0], "--example-rustdoc-arg".to_owned());
179-
180-
let dependencies = metadata.dependencies.unwrap();
181-
assert_eq!(dependencies.len(), 1);
182-
assert_eq!(dependencies[0], "example-system-dependency".to_owned());
183169
}
184170
}

src/docbuilder/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

2-
pub mod options;
3-
pub mod metadata;
2+
pub(crate) mod options;
3+
mod metadata;
44
mod limits;
55
mod rustwide_builder;
66
mod crates;
@@ -9,6 +9,7 @@ mod queue;
99
pub use self::rustwide_builder::RustwideBuilder;
1010
pub(crate) use self::rustwide_builder::BuildResult;
1111
pub(crate) use self::limits::Limits;
12+
pub(self) use self::metadata::Metadata;
1213

1314

1415
use std::fs;

src/docbuilder/rustwide_builder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::borrow::Cow;
1616
use std::collections::HashSet;
1717
use std::path::Path;
1818
use utils::{copy_doc_dir, parse_rustc_version, CargoMetadata};
19-
use Metadata;
19+
use super::Metadata;
2020

2121
const USER_AGENT: &str = "docs.rs builder (https://github.com/rust-lang/docs.rs)";
2222
const DEFAULT_RUSTWIDE_WORKSPACE: &str = ".rustwide";
@@ -522,8 +522,7 @@ impl RustwideBuilder {
522522
}
523523

524524
info!("{} {}", source.display(), dest.display());
525-
copy_doc_dir(source, dest, self.rustc_version.trim())?;
526-
Ok(())
525+
copy_doc_dir(source, dest)
527526
}
528527

529528
fn upload_docs(

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
33
use std::result::Result as StdResult;
44

5-
pub use failure::{Error, ResultExt};
5+
pub(crate) use failure::Error;
66

77
pub type Result<T> = StdResult<T, Error>;

src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,9 @@ extern crate once_cell;
4747
pub use self::docbuilder::RustwideBuilder;
4848
pub use self::docbuilder::DocBuilder;
4949
pub use self::docbuilder::options::DocBuilderOptions;
50-
pub use self::docbuilder::metadata::Metadata;
5150
pub use self::web::Server;
5251

53-
pub mod error;
52+
mod error;
5453
pub mod db;
5554
pub mod utils;
5655
mod docbuilder;

src/utils/copy.rs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -8,44 +8,16 @@ use error::Result;
88

99
use regex::Regex;
1010

11-
12-
/// Copies files from source directory to destination directory.
13-
pub fn copy_dir<P: AsRef<Path>>(source: P, destination: P) -> Result<()> {
14-
copy_files_and_handle_html(source.as_ref().to_path_buf(),
15-
destination.as_ref().to_path_buf(),
16-
false,
17-
"")
18-
}
19-
20-
2111
/// Copies documentation from a crate's target directory to destination.
2212
///
2313
/// Target directory must have doc directory.
2414
///
25-
/// This function is designed to avoid file duplications. It is using rustc version string
26-
/// to rename common files (css files, jquery.js, playpen.js, main.js etc.) in a standard rustdoc.
27-
pub fn copy_doc_dir<P: AsRef<Path>>(target: P,
28-
destination: P,
29-
rustc_version: &str)
30-
-> Result<()> {
15+
/// This function is designed to avoid file duplications.
16+
pub fn copy_doc_dir<P: AsRef<Path>>(target: P, destination: P) -> Result<()> {
3117
let source = PathBuf::from(target.as_ref()).join("doc");
32-
copy_files_and_handle_html(source,
33-
destination.as_ref().to_path_buf(),
34-
true,
35-
rustc_version)
36-
}
18+
let destination = destination.as_ref().to_path_buf();
3719

38-
39-
fn copy_files_and_handle_html(source: PathBuf,
40-
destination: PathBuf,
41-
handle_html: bool,
42-
rustc_version: &str)
43-
-> Result<()> {
44-
45-
// FIXME: handle_html is useless since we started using --resource-suffix
46-
// argument with rustdoc
47-
48-
// Make sure destination directory is exists
20+
// Make sure destination directory exists
4921
if !destination.exists() {
5022
fs::create_dir_all(&destination)?;
5123
}
@@ -65,11 +37,8 @@ fn copy_files_and_handle_html(source: PathBuf,
6537

6638
if metadata.is_dir() {
6739
fs::create_dir_all(&destination_full_path)?;
68-
copy_files_and_handle_html(file.path(),
69-
destination_full_path,
70-
handle_html,
71-
&rustc_version)?
72-
} else if handle_html && dup_regex.is_match(&file.file_name().into_string().unwrap()[..]) {
40+
copy_doc_dir(file.path(), destination_full_path)?
41+
} else if dup_regex.is_match(&file.file_name().into_string().unwrap()[..]) {
7342
continue;
7443
} else {
7544
fs::copy(&file.path(), &destination_full_path)?;
@@ -85,19 +54,21 @@ fn copy_files_and_handle_html(source: PathBuf,
8554
mod test {
8655
extern crate env_logger;
8756
use std::fs;
88-
use std::path::Path;
8957
use super::*;
9058

9159
#[test]
92-
#[ignore]
93-
fn test_copy_dir() {
94-
let destination = tempdir::TempDir::new("cratesfyi").unwrap();
60+
fn test_copy_doc_dir() {
61+
let source = tempdir::TempDir::new("cratesfyi-src").unwrap();
62+
let destination = tempdir::TempDir::new("cratesfyi-dst").unwrap();
63+
let doc = source.path().join("doc");
64+
fs::create_dir(&doc).unwrap();
9565

96-
// lets try to copy a src directory to tempdir
97-
let res = copy_dir(Path::new("src"), destination.path());
98-
// remove temp dir
99-
fs::remove_dir_all(destination.path()).unwrap();
66+
fs::write(doc.join("index.html"), "<html>spooky</html>").unwrap();
67+
fs::write(doc.join("index.txt"), "spooky").unwrap();
10068

101-
assert!(res.is_ok());
69+
// lets try to copy a src directory to tempdir
70+
copy_doc_dir(source.path(), destination.path()).unwrap();
71+
assert!(destination.path().join("index.html").exists());
72+
assert!(!destination.path().join("index.txt").exists());
10273
}
10374
}

src/utils/github_updater.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ use failure::err_msg;
99
/// Fields we need use in cratesfyi
1010
#[derive(Debug)]
1111
struct GitHubFields {
12-
pub description: String,
13-
pub stars: i64,
14-
pub forks: i64,
15-
pub issues: i64,
16-
pub last_commit: time::Timespec,
12+
description: String,
13+
stars: i64,
14+
forks: i64,
15+
issues: i64,
16+
last_commit: time::Timespec,
1717
}
1818

1919

src/utils/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
//! Various utilities for cratesfyi
22
33

4-
pub use self::copy::{copy_dir, copy_doc_dir};
4+
pub(crate) use self::copy::copy_doc_dir;
55
pub use self::github_updater::github_updater;
66
pub use self::release_activity_updater::update_release_activity;
77
pub use self::daemon::start_daemon;
8-
pub use self::rustc_version::{parse_rustc_version, get_current_versions, command_result};
9-
pub use self::html::extract_head_and_body;
8+
pub(crate) use self::rustc_version::parse_rustc_version;
9+
pub(crate) use self::html::extract_head_and_body;
1010
pub use self::queue::add_crate_to_queue;
1111
pub(crate) use self::cargo_metadata::{CargoMetadata, Package as MetadataPackage};
1212

src/utils/rustc_version.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11

2-
use std::process::{Command, Output};
32
use regex::Regex;
43
use error::Result;
54
use failure::err_msg;
@@ -18,26 +17,6 @@ pub fn parse_rustc_version<S: AsRef<str>>(version: S) -> Result<String> {
1817
captures.get(2).unwrap().as_str()))
1918
}
2019

21-
22-
/// Returns current version of rustc and cratesfyi
23-
pub fn get_current_versions() -> Result<(String, String)> {
24-
let rustc_version = command_result(Command::new("rustc").arg("--version").output()?)?;
25-
let cratesfyi_version = command_result(Command::new("rustc").arg("--version").output()?)?;
26-
27-
Ok((rustc_version, cratesfyi_version))
28-
}
29-
30-
31-
pub fn command_result(output: Output) -> Result<String> {
32-
let mut command_out = String::from_utf8_lossy(&output.stdout).into_owned();
33-
command_out.push_str(&String::from_utf8_lossy(&output.stderr).into_owned()[..]);
34-
match output.status.success() {
35-
true => Ok(command_out),
36-
false => Err(err_msg(command_out)),
37-
}
38-
}
39-
40-
4120
#[test]
4221
fn test_parse_rustc_version() {
4322
assert_eq!(parse_rustc_version("rustc 1.10.0-nightly (57ef01513 2016-05-23)").unwrap(),

src/web/crate_details.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub struct CrateDetails {
4343
github_stars: Option<i32>,
4444
github_forks: Option<i32>,
4545
github_issues: Option<i32>,
46-
pub metadata: MetaData,
46+
pub(crate) metadata: MetaData,
4747
is_library: bool,
4848
doc_targets: Option<Json>,
4949
license: Option<String>,

0 commit comments

Comments
 (0)