Skip to content

Commit ef0f56d

Browse files
committed
rustfmt
1 parent b3ddfd3 commit ef0f56d

File tree

8 files changed

+72
-68
lines changed

8 files changed

+72
-68
lines changed

src/app.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
//! Application-wide components in a struct accessible from each request
22
33
use crate::{db, util::CargoResult, Config, Env};
4-
use std::{
5-
env,
6-
path::PathBuf,
7-
sync::Arc,
8-
time::Duration,
9-
};
4+
use std::{env, path::PathBuf, sync::Arc, time::Duration};
105

116
use diesel::r2d2;
127
use scheduled_thread_pool::ScheduledThreadPool;

src/background/job.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use diesel::PgConnection;
2-
use serde::{Serialize, de::DeserializeOwned};
2+
use serde::{de::DeserializeOwned, Serialize};
33

44
use super::storage;
55
use crate::util::CargoResult;

src/background/registry.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ use super::Job;
66
use crate::util::CargoResult;
77

88
#[doc(hidden)]
9-
pub type PerformFn<Env> = Box<dyn Fn(serde_json::Value, &Env) -> CargoResult<()> + RefUnwindSafe + Send + Sync>;
9+
pub type PerformFn<Env> =
10+
Box<dyn Fn(serde_json::Value, &Env) -> CargoResult<()> + RefUnwindSafe + Send + Sync>;
1011

1112
#[derive(Default)]
1213
#[allow(missing_debug_implementations)] // Can't derive debug
@@ -32,9 +33,12 @@ impl<Env> Registry<Env> {
3233
/// Register a new background job. This will override any existing
3334
/// registries with the same `JOB_TYPE`, if one exists.
3435
pub fn register<T: Job<Environment = Env>>(&mut self) {
35-
self.job_types.insert(T::JOB_TYPE, Box::new(|data, env| {
36-
let data = serde_json::from_value(data)?;
37-
T::perform(data, env)
38-
}));
36+
self.job_types.insert(
37+
T::JOB_TYPE,
38+
Box::new(|data, env| {
39+
let data = serde_json::from_value(data)?;
40+
T::perform(data, env)
41+
}),
42+
);
3943
}
4044
}

src/background/runner.rs

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#![allow(dead_code)]
22
use diesel::prelude::*;
33
use std::any::Any;
4-
use std::panic::{catch_unwind, UnwindSafe, RefUnwindSafe, PanicInfo};
4+
use std::panic::{catch_unwind, PanicInfo, RefUnwindSafe, UnwindSafe};
55
use std::sync::Arc;
66
use threadpool::ThreadPool;
77

8+
use super::{storage, Job, Registry};
89
use crate::db::{DieselPool, DieselPooledConn};
9-
use super::{storage, Registry, Job};
1010
use crate::util::errors::*;
1111

1212
#[allow(missing_debug_implementations)]
@@ -68,7 +68,8 @@ impl<Env: RefUnwindSafe + Send + Sync + 'static> Runner<Env> {
6868
let environment = Arc::clone(&self.environment);
6969
let registry = Arc::clone(&self.registry);
7070
self.get_single_job(move |job| {
71-
let perform_fn = registry.get(&job.job_type)
71+
let perform_fn = registry
72+
.get(&job.job_type)
7273
.ok_or_else(|| internal(&format_args!("Unknown job type {}", job.job_type)))?;
7374
perform_fn(job.data, &environment)
7475
})
@@ -100,7 +101,8 @@ impl<Env: RefUnwindSafe + Send + Sync + 'static> Runner<Env> {
100101
}
101102
}
102103
Ok(())
103-
}).expect("Could not retrieve or update job")
104+
})
105+
.expect("Could not retrieve or update job")
104106
})
105107
}
106108

@@ -144,10 +146,10 @@ mod tests {
144146
use diesel::prelude::*;
145147
use diesel::r2d2;
146148

149+
use super::*;
147150
use crate::schema::background_jobs::dsl::*;
148-
use std::sync::{Mutex, MutexGuard, Barrier, Arc};
149151
use std::panic::AssertUnwindSafe;
150-
use super::*;
152+
use std::sync::{Arc, Barrier, Mutex, MutexGuard};
151153

152154
#[test]
153155
fn jobs_are_locked_when_fetched() {
@@ -185,12 +187,11 @@ mod tests {
185187
let runner = runner();
186188
create_dummy_job(&runner);
187189

188-
runner.get_single_job(|_| {
189-
Ok(())
190-
});
190+
runner.get_single_job(|_| Ok(()));
191191
runner.wait_for_jobs();
192192

193-
let remaining_jobs = background_jobs.count()
193+
let remaining_jobs = background_jobs
194+
.count()
194195
.get_result(&runner.connection().unwrap());
195196
assert_eq!(Ok(0), remaining_jobs);
196197
}
@@ -243,9 +244,7 @@ mod tests {
243244
let runner = runner();
244245
let job_id = create_dummy_job(&runner).id;
245246

246-
runner.get_single_job(|_| {
247-
panic!()
248-
});
247+
runner.get_single_job(|_| panic!());
249248
runner.wait_for_jobs();
250249

251250
let tries = background_jobs
@@ -289,14 +288,9 @@ mod tests {
289288
let database_url =
290289
dotenv::var("TEST_DATABASE_URL").expect("TEST_DATABASE_URL must be set to run tests");
291290
let manager = r2d2::ConnectionManager::new(database_url);
292-
let pool = r2d2::Pool::builder()
293-
.max_size(2)
294-
.build(manager)
295-
.unwrap();
291+
let pool = r2d2::Pool::builder().max_size(2).build(manager).unwrap();
296292

297-
Runner::builder(pool, ())
298-
.thread_count(2)
299-
.build()
293+
Runner::builder(pool, ()).thread_count(2).build()
300294
}
301295

302296
fn create_dummy_job(runner: &Runner<()>) -> storage::BackgroundJob {

src/background/storage.rs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use diesel::dsl::now;
22
use diesel::pg::Pg;
33
use diesel::prelude::*;
4-
use diesel::{delete, insert_into, update};
54
use diesel::sql_types::{Bool, Integer, Interval};
5+
use diesel::{delete, insert_into, update};
66
use serde_json;
77

8-
use crate::schema::background_jobs;
98
use super::Job;
9+
use crate::schema::background_jobs;
1010
use crate::util::CargoResult;
1111

1212
#[derive(Queryable, Identifiable, Debug, Clone)]
@@ -22,10 +22,7 @@ pub fn enqueue_job<T: Job>(conn: &PgConnection, job: T) -> CargoResult<()> {
2222

2323
let job_data = serde_json::to_value(job)?;
2424
insert_into(background_jobs)
25-
.values((
26-
job_type.eq(T::JOB_TYPE),
27-
data.eq(job_data),
28-
))
25+
.values((job_type.eq(T::JOB_TYPE), data.eq(job_data)))
2926
.execute(conn)?;
3027
Ok(())
3128
}
@@ -67,10 +64,7 @@ pub fn failed_job_count(conn: &PgConnection) -> QueryResult<i64> {
6764
pub fn available_job_count(conn: &PgConnection) -> QueryResult<i64> {
6865
use crate::schema::background_jobs::dsl::*;
6966

70-
background_jobs
71-
.count()
72-
.filter(retriable())
73-
.get_result(conn)
67+
background_jobs.count().filter(retriable()).get_result(conn)
7468
}
7569

7670
/// Deletes a job that has successfully completed running
@@ -89,9 +83,6 @@ pub fn update_failed_job(conn: &PgConnection, job_id: i64) {
8983
use crate::schema::background_jobs::dsl::*;
9084

9185
let _ = update(background_jobs.find(job_id))
92-
.set((
93-
retries.eq(retries + 1),
94-
last_retry.eq(now),
95-
))
86+
.set((retries.eq(retries + 1), last_retry.eq(now)))
9687
.execute(conn);
9788
}

src/background_jobs.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
use url::Url;
22

3-
use crate::background::{Runner, Builder};
3+
use crate::background::{Builder, Runner};
44
use crate::git::{AddCrate, Yank};
55

66
pub fn job_runner(config: Builder<Environment>) -> Runner<Environment> {
7-
config
8-
.register::<AddCrate>()
9-
.register::<Yank>()
10-
.build()
7+
config.register::<AddCrate>().register::<Yank>().build()
118
}
129

1310
#[allow(missing_debug_implementations)]
@@ -18,6 +15,8 @@ pub struct Environment {
1815

1916
impl Environment {
2017
pub fn credentials(&self) -> Option<(&str, &str)> {
21-
self.credentials.as_ref().map(|(u, p)| (u.as_str(), p.as_str()))
18+
self.credentials
19+
.as_ref()
20+
.map(|(u, p)| (u.as_str(), p.as_str()))
2221
}
2322
}

src/git.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ use url::Url;
1010

1111
use crate::background::Job;
1212
use crate::background_jobs::Environment;
13-
use crate::util::{internal, CargoResult};
1413
use crate::models::DependencyKind;
14+
use crate::util::{internal, CargoResult};
1515

1616
#[derive(Serialize, Deserialize, Debug)]
1717
pub struct Crate {
@@ -61,7 +61,8 @@ impl Repository {
6161
}
6262

6363
fn index_file(&self, name: &str) -> PathBuf {
64-
self.checkout_path.path()
64+
self.checkout_path
65+
.path()
6566
.join(self.relative_index_file(name))
6667
}
6768

@@ -75,7 +76,12 @@ impl Repository {
7576
}
7677
}
7778

78-
fn commit_and_push(&self, msg: &str, modified_file: &Path, credentials: Option<(&str, &str)>) -> CargoResult<()> {
79+
fn commit_and_push(
80+
&self,
81+
msg: &str,
82+
modified_file: &Path,
83+
credentials: Option<(&str, &str)>,
84+
) -> CargoResult<()> {
7985
// git add $file
8086
let mut index = self.repository.index()?;
8187
index.add_path(modified_file)?;
@@ -87,14 +93,19 @@ impl Repository {
8793
let head = self.repository.head()?;
8894
let parent = self.repository.find_commit(head.target().unwrap())?;
8995
let sig = self.repository.signature()?;
90-
self.repository.commit(Some("HEAD"), &sig, &sig, &msg, &tree, &[&parent])?;
96+
self.repository
97+
.commit(Some("HEAD"), &sig, &sig, &msg, &tree, &[&parent])?;
9198

9299
// git push
93100
let mut ref_status = Ok(());
94101
{
95102
let mut origin = self.repository.find_remote("origin")?;
96103
let mut callbacks = git2::RemoteCallbacks::new();
97-
callbacks.credentials(|_, _, _| credentials.ok_or_else(|| git2::Error::from_str("no authentication set")).and_then(|(u, p)| git2::Cred::userpass_plaintext(u, p)));
104+
callbacks.credentials(|_, _, _| {
105+
credentials
106+
.ok_or_else(|| git2::Error::from_str("no authentication set"))
107+
.and_then(|(u, p)| git2::Cred::userpass_plaintext(u, p))
108+
});
98109
callbacks.push_update_reference(|refname, status| {
99110
assert_eq!(refname, "refs/heads/master");
100111
if let Some(s) = status {
@@ -138,9 +149,7 @@ impl Job for AddCrate {
138149
}
139150

140151
pub fn add_crate(conn: &PgConnection, krate: Crate) -> CargoResult<()> {
141-
AddCrate { krate }
142-
.enqueue(conn)
143-
.map_err(Into::into)
152+
AddCrate { krate }.enqueue(conn).map_err(Into::into)
144153
}
145154

146155
#[derive(Serialize, Deserialize)]
@@ -169,7 +178,8 @@ impl Job for Yank {
169178
}
170179
git_crate.yanked = Some(self.yanked);
171180
Ok(serde_json::to_string(&git_crate)?)
172-
}).collect::<CargoResult<Vec<String>>>();
181+
})
182+
.collect::<CargoResult<Vec<String>>>();
173183
let new = new?.join("\n") + "\n";
174184
fs::write(&dst, new.as_bytes())?;
175185

@@ -190,8 +200,17 @@ impl Job for Yank {
190200
/// file, deserlialise the crate from JSON, change the yank boolean to
191201
/// `true` or `false`, write all the lines back out, and commit and
192202
/// push the changes.
193-
pub fn yank(conn: &PgConnection, krate: String, version: semver::Version, yanked: bool) -> CargoResult<()> {
194-
Yank { krate, version: version.to_string(), yanked }
195-
.enqueue(conn)
196-
.map_err(Into::into)
203+
pub fn yank(
204+
conn: &PgConnection,
205+
krate: String,
206+
version: semver::Version,
207+
yanked: bool,
208+
) -> CargoResult<()> {
209+
Yank {
210+
krate,
211+
version: version.to_string(),
212+
yanked,
213+
}
214+
.enqueue(conn)
215+
.map_err(Into::into)
197216
}

src/middleware/run_pending_background_jobs.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use crate::background::Runner;
2-
use crate::background_jobs::*;
31
use super::app::RequestApp;
42
use super::prelude::*;
3+
use crate::background::Runner;
4+
use crate::background_jobs::*;
55

66
pub struct RunPendingBackgroundJobs;
77

@@ -22,7 +22,9 @@ impl Middleware for RunPendingBackgroundJobs {
2222
let runner = job_runner(config);
2323

2424
runner.run_all_pending_jobs().expect("Could not run jobs");
25-
runner.assert_no_failed_jobs().expect("Could not determine if jobs failed");
25+
runner
26+
.assert_no_failed_jobs()
27+
.expect("Could not determine if jobs failed");
2628
res
2729
}
2830
}

0 commit comments

Comments
 (0)