Skip to content

cargo fmt #771

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

Merged
merged 14 commits into from
Jun 13, 2017
9 changes: 9 additions & 0 deletions .rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
fn_args_layout = "Block"
array_layout = "Block"
control_style = "Rfc"
where_style = "Rfc"
generics_indent = "Block"
fn_call_style = "Block"
combine_control_expr = true
fn_args_paren_newline = false
max_width = 160
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ sudo: false
dist: trusty

cache:
cargo: true
directories:
- $HOME/.yarn-cache

Expand All @@ -17,6 +18,8 @@ install:
- yarn
- yarn run bower install
- cargo install diesel_cli --debug --no-default-features --features postgres && export PATH=$HOME/.cargo/bin:$PATH
- cargo install rustfmt
- rustfmt --version

before_script:
- diesel database setup
Expand All @@ -34,6 +37,9 @@ addons:

matrix:
include:
- rust: stable
script:
- cargo fmt -- --write-mode=diff
- rust: stable
script:
- cargo build
Expand Down
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ A volunteer will get back to you as soon as possible.

Welcome! We love contributions! Crates.io is an [Ember](https://emberjs.com/)
frontend with a Rust backend, and there are many tasks appropriate for a
variety of skill levels.
variety of skill levels.

The Rust backend uses rustfmt for code formatting, so ensure that you run it prior
to submitting a pull request, and that your version of rustfmt is the latst version.
More information on setting up and running rustfmt can be found
[here](https://github.com/rust-lang-nursery/rustfmt/blob/master/README.md).

Please see [docs/CONTRIBUTING.md](https://github.com/rust-lang/crates.io/blob/master/docs/CONTRIBUTING.md) for ideas about what to work on and how to set up a development
environment.
Expand Down
6 changes: 2 additions & 4 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,8 @@ fn main() {
if env::var("PROFILE") == Ok("debug".into()) {
let _ = dotenv();
if let Ok(database_url) = env::var("TEST_DATABASE_URL") {
let connection = PgConnection::establish(&database_url)
.expect("Could not connect to TEST_DATABASE_URL");
run_pending_migrations(&connection)
.expect("Error running migrations");
let connection = PgConnection::establish(&database_url).expect("Could not connect to TEST_DATABASE_URL");
run_pending_migrations(&connection).expect("Error running migrations");
}
}
}
10 changes: 4 additions & 6 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct App {

/// The `AppMiddleware` injects an `App` instance into the `Request` extensions
pub struct AppMiddleware {
app: Arc<App>
app: Arc<App>,
}

impl App {
Expand Down Expand Up @@ -105,13 +105,12 @@ impl AppMiddleware {
}

impl Middleware for AppMiddleware {
fn before(&self, req: &mut Request) -> Result<(), Box<Error+Send>> {
fn before(&self, req: &mut Request) -> Result<(), Box<Error + Send>> {
req.mut_extensions().insert(self.app.clone());
Ok(())
}

fn after(&self, req: &mut Request, res: Result<Response, Box<Error+Send>>)
-> Result<Response, Box<Error+Send>> {
fn after(&self, req: &mut Request, res: Result<Response, Box<Error + Send>>) -> Result<Response, Box<Error + Send>> {
req.mut_extensions().pop::<Arc<App>>().unwrap();
res
}
Expand All @@ -124,7 +123,6 @@ pub trait RequestApp {

impl<T: Request + ?Sized> RequestApp for T {
fn app(&self) -> &Arc<App> {
self.extensions().find::<Arc<App>>()
.expect("Missing app")
self.extensions().find::<Arc<App>>().expect("Missing app")
}
}
53 changes: 27 additions & 26 deletions src/badge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,35 @@ use std::collections::HashMap;
pub enum Badge {
#[serde(rename = "travis-ci")]
TravisCi {
repository: String, branch: Option<String>,
repository: String,
branch: Option<String>,
},
#[serde(rename = "appveyor")]
Appveyor {
repository: String, branch: Option<String>, service: Option<String>,
repository: String,
branch: Option<String>,
service: Option<String>,
},
#[serde(rename = "gitlab")]
GitLab {
repository: String, branch: Option<String>,
},
#[serde(rename = "is-it-maintained-issue-resolution")]
IsItMaintainedIssueResolution {
repository: String,
branch: Option<String>,
},
#[serde(rename = "is-it-maintained-issue-resolution")]
IsItMaintainedIssueResolution { repository: String },
#[serde(rename = "is-it-maintained-open-issues")]
IsItMaintainedOpenIssues {
repository: String,
},
IsItMaintainedOpenIssues { repository: String },
#[serde(rename = "codecov")]
Codecov {
repository: String, branch: Option<String>, service: Option<String>,
repository: String,
branch: Option<String>,
service: Option<String>,
},
#[serde(rename = "coveralls")]
Coveralls {
repository: String, branch: Option<String>, service: Option<String>,
repository: String,
branch: Option<String>,
service: Option<String>,
},
}

Expand All @@ -51,8 +55,7 @@ impl Queryable<badges::SqlType, Pg> for Badge {

fn build((_, badge_type, attributes): Self::Row) -> Self {
let json = json!({"badge_type": badge_type, "attributes": attributes});
serde_json::from_value(json)
.expect("Invalid CI badge in the database")
serde_json::from_value(json).expect("Invalid CI badge in the database")
}
}

Expand All @@ -63,24 +66,21 @@ impl Badge {

pub fn badge_type(&self) -> &'static str {
match *self {
Badge::TravisCi {..} => "travis-ci",
Badge::Appveyor {..} => "appveyor",
Badge::GitLab{..} => "gitlab",
Badge::IsItMaintainedIssueResolution{..} => "is-it-maintained-issue-resolution",
Badge::IsItMaintainedOpenIssues{..} => "is-it-maintained-open-issues",
Badge::Codecov{..} => "codecov",
Badge::Coveralls{..} => "coveralls",
Badge::TravisCi { .. } => "travis-ci",
Badge::Appveyor { .. } => "appveyor",
Badge::GitLab { .. } => "gitlab",
Badge::IsItMaintainedIssueResolution { .. } => "is-it-maintained-issue-resolution",
Badge::IsItMaintainedOpenIssues { .. } => "is-it-maintained-open-issues",
Badge::Codecov { .. } => "codecov",
Badge::Coveralls { .. } => "coveralls",
}
}

pub fn update_crate<'a>(conn: &PgConnection,
krate: &Crate,
badges: Option<&'a HashMap<String, HashMap<String, String>>>)
-> CargoResult<Vec<&'a str>> {
pub fn update_crate<'a>(conn: &PgConnection, krate: &Crate, badges: Option<&'a HashMap<String, HashMap<String, String>>>) -> CargoResult<Vec<&'a str>> {
use diesel::{insert, delete};

#[derive(Insertable)]
#[table_name="badges"]
#[table_name = "badges"]
struct NewBadge<'a> {
crate_id: i32,
badge_type: &'a str,
Expand Down Expand Up @@ -108,7 +108,8 @@ impl Badge {
}

conn.transaction(|| {
delete(badges::table.filter(badges::crate_id.eq(krate.id))).execute(conn)?;
delete(badges::table.filter(badges::crate_id.eq(krate.id)))
.execute(conn)?;
insert(&new_badges).into(badges::table).execute(conn)?;
Ok(invalid_badges)
})
Expand Down
85 changes: 55 additions & 30 deletions src/bin/delete-crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,80 +30,105 @@ fn main() {

fn delete(tx: &postgres::transaction::Transaction) {
let name = match env::args().nth(1) {
None => { println!("needs a crate-name argument"); return }
None => {
println!("needs a crate-name argument");
return;
}
Some(s) => s,
};

let krate = Crate::find_by_name(tx, &name).unwrap();
print!("Are you sure you want to delete {} ({}) [y/N]: ", name, krate.id);
print!(
"Are you sure you want to delete {} ({}) [y/N]: ",
name,
krate.id
);
io::stdout().flush().unwrap();
let mut line = String::new();
io::stdin().read_line(&mut line).unwrap();
if !line.starts_with("y") { return }
if !line.starts_with("y") {
return;
}

let versions = krate.versions(tx).unwrap();

for v in versions.iter() {
println!("deleting version {} ({})", v.num, v.id);
let n = tx.execute("DELETE FROM version_downloads WHERE version_id = $1",
&[&v.id]).unwrap();
let n = tx.execute(
"DELETE FROM version_downloads WHERE version_id = $1",
&[&v.id],
).unwrap();
println!(" {} download records deleted", n);
let n = tx.execute("DELETE FROM version_authors WHERE version_id = $1",
&[&v.id]).unwrap();
let n = tx.execute(
"DELETE FROM version_authors WHERE version_id = $1",
&[&v.id],
).unwrap();
println!(" {} author records deleted", n);
let n = tx.execute("DELETE FROM dependencies WHERE version_id = $1",
&[&v.id]).unwrap();
let n = tx.execute("DELETE FROM dependencies WHERE version_id = $1", &[&v.id])
.unwrap();
println!(" {} dependencies deleted", n);
tx.execute("DELETE FROM versions WHERE id = $1",
&[&v.id]).unwrap();
tx.execute("DELETE FROM versions WHERE id = $1", &[&v.id])
.unwrap();
}

println!("deleting follows");
let n = tx.execute("DELETE FROM follows WHERE crate_id = $1",
&[&krate.id]).unwrap();
let n = tx.execute("DELETE FROM follows WHERE crate_id = $1", &[&krate.id])
.unwrap();
println!(" {} deleted", n);

println!("deleting crate download records");
let n = tx.execute("DELETE FROM crate_downloads WHERE crate_id = $1",
&[&krate.id]).unwrap();
let n = tx.execute(
"DELETE FROM crate_downloads WHERE crate_id = $1",
&[&krate.id],
).unwrap();
println!(" {} deleted", n);

println!("deleting crate owners");
let n = tx.execute("DELETE FROM crate_owners WHERE crate_id = $1",
&[&krate.id]).unwrap();
let n = tx.execute("DELETE FROM crate_owners WHERE crate_id = $1", &[&krate.id])
.unwrap();
println!(" {} deleted", n);

println!("disabling reserved crate name trigger");
let _ = tx.execute("ALTER TABLE crates DISABLE TRIGGER trigger_ensure_crate_name_not_reserved;",
&[]).unwrap();
let _ = tx.execute(
"ALTER TABLE crates DISABLE TRIGGER trigger_ensure_crate_name_not_reserved;",
&[],
).unwrap();

println!("deleting crate keyword connections");
let n = tx.execute("DELETE FROM crates_keywords WHERE crate_id = $1",
&[&krate.id]).unwrap();
let n = tx.execute(
"DELETE FROM crates_keywords WHERE crate_id = $1",
&[&krate.id],
).unwrap();
println!(" {} deleted", n);

println!("deleting crate category connections");
let n = tx.execute("DELETE FROM crates_categories WHERE crate_id = $1",
&[&krate.id]).unwrap();
let n = tx.execute(
"DELETE FROM crates_categories WHERE crate_id = $1",
&[&krate.id],
).unwrap();
println!(" {} deleted", n);

println!("enabling reserved crate name trigger");
let _ = tx.execute("ALTER TABLE crates ENABLE TRIGGER trigger_ensure_crate_name_not_reserved;",
&[]).unwrap();
let _ = tx.execute(
"ALTER TABLE crates ENABLE TRIGGER trigger_ensure_crate_name_not_reserved;",
&[],
).unwrap();

println!("deleting crate badges");
let n = tx.execute("DELETE FROM badges WHERE crate_id = $1",
&[&krate.id]).unwrap();
let n = tx.execute("DELETE FROM badges WHERE crate_id = $1", &[&krate.id])
.unwrap();
println!(" {} deleted", n);

println!("deleting the crate");
let n = tx.execute("DELETE FROM crates WHERE id = $1",
&[&krate.id]).unwrap();
let n = tx.execute("DELETE FROM crates WHERE id = $1", &[&krate.id])
.unwrap();
println!(" {} deleted", n);

print!("commit? [y/N]: ");
io::stdout().flush().unwrap();
let mut line = String::new();
io::stdin().read_line(&mut line).unwrap();
if !line.starts_with("y") { panic!("aborting transaction"); }
if !line.starts_with("y") {
panic!("aborting transaction");
}
}
Loading