Skip to content

Issue #574 - Initial run of rustfmt on the whole codebase. #581

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@
npm-debug.log
testem.log
.env
# rustfmt backup files
*.bk
37 changes: 23 additions & 14 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,29 @@ pub struct App {

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

impl App {
pub fn new(config: &Config) -> App {
let mut github = oauth2::Config::new(
&config.gh_client_id,
&config.gh_client_secret,
"https://github.com/login/oauth/authorize",
"https://github.com/login/oauth/access_token",
);
let mut github = oauth2::Config::new(&config.gh_client_id,
&config.gh_client_secret,
"https://github.com/login/oauth/authorize",
"https://github.com/login/oauth/access_token");

github.scopes.push(String::from("read:org"));

let db_config = r2d2::Config::builder()
.pool_size(if config.env == ::Env::Production {10} else {1})
.helper_threads(if config.env == ::Env::Production {3} else {1})
.pool_size(if config.env == ::Env::Production {
10
} else {
1
})
.helper_threads(if config.env == ::Env::Production {
3
} else {
1
})
.build();

let repo = git2::Repository::open(&config.git_repo_checkout).unwrap();
Expand All @@ -73,7 +79,7 @@ impl App {
if let Some(ref proxy) = self.s3_proxy {
handle.proxy(proxy).unwrap();
}
return handle
return handle;
}
}

Expand All @@ -84,13 +90,15 @@ 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 @@ -103,7 +111,8 @@ pub trait RequestApp {

impl<'a> RequestApp for Request + 'a {
fn app(&self) -> &Arc<App> {
self.extensions().find::<Arc<App>>()
self.extensions()
.find::<Arc<App>>()
.expect("Missing app")
}
}
148 changes: 67 additions & 81 deletions src/badge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ use rustc_serialize::json::Json;
#[derive(Debug, PartialEq, Clone)]
pub enum Badge {
TravisCi {
repository: String, branch: Option<String>,
repository: String,
branch: Option<String>,
},
Appveyor {
repository: String, branch: Option<String>, service: Option<String>,
repository: String,
branch: Option<String>,
service: Option<String>,
},
GitLab {
repository: String, branch: Option<String>,
repository: String,
branch: Option<String>,
},
}

Expand All @@ -35,57 +39,51 @@ impl Model for Badge {
"travis-ci" => {
Badge::TravisCi {
branch: attributes.get("branch")
.and_then(Json::as_string)
.map(str::to_string),
.and_then(Json::as_string)
.map(str::to_string),
repository: attributes.get("repository")
.and_then(Json::as_string)
.map(str::to_string)
.expect("Invalid TravisCi badge \
without repository in the \
database"),
.and_then(Json::as_string)
.map(str::to_string)
.expect("Invalid TravisCi badge without repository in the database"),
}
},
}
"appveyor" => {
Badge::Appveyor {
service: attributes.get("service")
.and_then(Json::as_string)
.map(str::to_string),
.and_then(Json::as_string)
.map(str::to_string),
branch: attributes.get("branch")
.and_then(Json::as_string)
.map(str::to_string),
.and_then(Json::as_string)
.map(str::to_string),
repository: attributes.get("repository")
.and_then(Json::as_string)
.map(str::to_string)
.expect("Invalid Appveyor badge \
without repository in the \
database"),
.and_then(Json::as_string)
.map(str::to_string)
.expect("Invalid Appveyor badge without repository in the database"),
}
},
}
"gitlab" => {
Badge::GitLab {
branch: attributes.get("branch")
.and_then(Json::as_string)
.map(str::to_string),
.and_then(Json::as_string)
.map(str::to_string),
repository: attributes.get("repository")
.and_then(Json::as_string)
.map(str::to_string)
.expect("Invalid GitLab badge \
without repository in the \
database"),
.and_then(Json::as_string)
.map(str::to_string)
.expect("Invalid GitLab badge without repository in the database"),
}
},
}
_ => {
panic!("Unknown badge type {} in the database", badge_type);
},
}
}
} else {
panic!(
"badge attributes {:?} in the database was not a JSON object",
attributes
);
panic!("badge attributes {:?} in the database was not a JSON object",
attributes);
}
}
fn table_name(_: Option<Badge>) -> &'static str { "badges" }
fn table_name(_: Option<Badge>) -> &'static str {
"badges"
}
}

impl Badge {
Expand All @@ -98,16 +96,17 @@ impl Badge {

pub fn badge_type(&self) -> &'static str {
match *self {
Badge::TravisCi {..} => "travis-ci",
Badge::Appveyor {..} => "appveyor",
Badge::GitLab{..} => "gitlab",
Badge::TravisCi { .. } => "travis-ci",
Badge::Appveyor { .. } => "appveyor",
Badge::GitLab { .. } => "gitlab",
}
}

pub fn json_attributes(self) -> Json {
Json::Object(self.attributes().into_iter().map(|(k, v)| {
(k, Json::String(v))
}).collect())
Json::Object(self.attributes()
.into_iter()
.map(|(k, v)| (k, Json::String(v)))
.collect())
}

fn attributes(self) -> HashMap<String, String> {
Expand All @@ -117,36 +116,24 @@ impl Badge {
Badge::TravisCi { branch, repository } => {
attributes.insert(String::from("repository"), repository);
if let Some(branch) = branch {
attributes.insert(
String::from("branch"),
branch
);
attributes.insert(String::from("branch"), branch);
}
},
}
Badge::Appveyor { service, branch, repository } => {
attributes.insert(String::from("repository"), repository);
if let Some(branch) = branch {
attributes.insert(
String::from("branch"),
branch
);
attributes.insert(String::from("branch"), branch);
}
if let Some(service) = service {
attributes.insert(
String::from("service"),
service
);
attributes.insert(String::from("service"), service);
}
},
}
Badge::GitLab { branch, repository } => {
attributes.insert(String::from("repository"), repository);
if let Some(branch) = branch {
attributes.insert(
String::from("branch"),
branch
);
attributes.insert(String::from("branch"), branch);
}
},
}
}

attributes
Expand All @@ -162,40 +149,39 @@ impl Badge {
Ok(Badge::TravisCi {
repository: repository.to_string(),
branch: attributes.get("branch")
.map(String::to_string),
.map(String::to_string),
})
},
}
None => Err(badge_type.to_string()),
}
},
}
"appveyor" => {
match attributes.get("repository") {
Some(repository) => {
Ok(Badge::Appveyor {
repository: repository.to_string(),
branch: attributes.get("branch")
.map(String::to_string),
.map(String::to_string),
service: attributes.get("service")
.map(String::to_string),

.map(String::to_string),
})
},
}
None => Err(badge_type.to_string()),
}
},
}
"gitlab" => {
match attributes.get("repository") {
Some(repository) => {
Ok(Badge::GitLab {
repository: repository.to_string(),
branch: attributes.get("branch")
.map(String::to_string),
.map(String::to_string),
})
},
}
None => Err(badge_type.to_string()),
}
},
_ => Err(badge_type.to_string()),
}
_ => Err(badge_type.to_string()),
}
}

Expand All @@ -206,26 +192,26 @@ impl Badge {

let mut invalid_badges = vec![];

let badges: Vec<_> = badges.iter().filter_map(|(k, v)| {
Badge::from_attributes(k, v).map_err(|invalid_badge| {
invalid_badges.push(invalid_badge)
}).ok()
}).collect();
let badges: Vec<_> = badges.iter()
.filter_map(|(k, v)| {
Badge::from_attributes(k, v)
.map_err(|invalid_badge| invalid_badges.push(invalid_badge))
.ok()
})
.collect();

conn.execute("\
DELETE FROM badges \
WHERE crate_id = $1;",
&[&krate.id]
)?;
&[&krate.id])?;

for badge in badges {
conn.execute("\
INSERT INTO badges (crate_id, badge_type, attributes) \
VALUES ($1, $2, $3) \
ON CONFLICT (crate_id, badge_type) DO UPDATE \
SET attributes = EXCLUDED.attributes;",
&[&krate.id, &badge.badge_type(), &badge.json_attributes()]
)?;
&[&krate.id, &badge.badge_type(), &badge.json_attributes()])?;
}
Ok(invalid_badges)
}
Expand Down
Loading