Skip to content

Fix warnings & clippy lints. Deny warnings on test #992

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 1 commit into from
Jan 28, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ use std::sync::RwLock;
use std::thread;
use std::time::Instant;

type Generator = fn() -> Result<Box<Any>, Box<Error>>;
type CacheItem = (Box<dyn Any + Send + Sync>, Instant);
type Generator = fn() -> Result<Box<dyn Any>, Box<dyn Error>>;

const CACHE_TTL_SECS: u64 = 120;

lazy_static! {
static ref CACHE: RwLock<HashMap<Generator, (Box<Any + Send + Sync>, Instant)>> =
RwLock::new(HashMap::new());
static ref CACHE: RwLock<HashMap<Generator, CacheItem>> = RwLock::new(HashMap::new());
}

pub fn get<T>(generator: Generator) -> Result<T, Box<Error>>
pub fn get<T>(generator: Generator) -> Result<T, Box<dyn Error>>
where
T: Send + Sync + Clone + 'static,
{
Expand All @@ -41,7 +41,7 @@ where
})
}

fn update_cache<T>(generator: Generator) -> Result<T, Box<Error>>
fn update_cache<T>(generator: Generator) -> Result<T, Box<dyn Error>>
where
T: Send + Sync + Clone + 'static,
{
Expand Down Expand Up @@ -70,7 +70,7 @@ mod tests {
fn test_cache_basic() {
static GENERATOR_CALLED: AtomicBool = AtomicBool::new(false);

fn generator() -> Result<Box<Any>, Box<Error>> {
fn generator() -> Result<Box<dyn Any>, Box<dyn Error>> {
GENERATOR_CALLED.store(true, Ordering::SeqCst);
Ok(Box::new("hello world"))
}
Expand All @@ -90,7 +90,7 @@ mod tests {
fn test_cache_refresh() {
static GENERATOR_CALLED: AtomicBool = AtomicBool::new(false);

fn generator() -> Result<Box<Any>, Box<Error>> {
fn generator() -> Result<Box<dyn Any>, Box<dyn Error>> {
GENERATOR_CALLED.store(true, Ordering::SeqCst);
thread::sleep(Duration::from_millis(100));
Ok(Box::new("hello world"))
Expand Down Expand Up @@ -125,7 +125,7 @@ mod tests {
fn test_errors_skip_cache() {
static GENERATOR_CALLED: AtomicBool = AtomicBool::new(false);

fn generator() -> Result<Box<Any>, Box<Error>> {
fn generator() -> Result<Box<dyn Any>, Box<dyn Error>> {
GENERATOR_CALLED.store(true, Ordering::SeqCst);
Err("an error".into())
}
Expand Down
2 changes: 1 addition & 1 deletion src/caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ where
fn cached(self, directives: Vec<CacheDirective>) -> Cached<Self> {
Cached {
inner: self,
directives: directives,
directives,
}
}
}
6 changes: 6 additions & 0 deletions src/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ pub struct TeamHelper {

impl TeamHelper {
pub fn new() -> Self {
Self::default()
}
}

impl Default for TeamHelper {
fn default() -> Self {
Self {
i18n: create_loader(),
}
Expand Down
22 changes: 9 additions & 13 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![feature(proc_macro_hygiene, decl_macro)]
#![cfg_attr(test, deny(warnings))]

#[macro_use]
extern crate lazy_static;
Expand Down Expand Up @@ -38,9 +39,7 @@ use production::User;
use std::collections::hash_map::DefaultHasher;
use std::env;
use std::fs;
use std::fs::File;
use std::hash::Hasher;
use std::io::prelude::*;
use std::path::{Path, PathBuf};

use rand::seq::SliceRandom;
Expand Down Expand Up @@ -323,15 +322,13 @@ fn compile_sass(filename: &str) -> String {
let scss_file = format!("./src/styles/{}.scss", filename);

let css = compile_file(&scss_file, Options::default())
.expect(&format!("couldn't compile sass: {}", &scss_file));
.unwrap_or_else(|_| panic!("couldn't compile sass: {}", &scss_file));

let css_sha = format!("{}_{}", filename, hash_css(&css));
let css_file = format!("./static/styles/{}.css", css_sha);

let mut file =
File::create(&css_file).expect(&format!("couldn't make css file: {}", &css_file));
file.write_all(&css.into_bytes())
.expect(&format!("couldn't write css file: {}", &css_file));
fs::write(&css_file, css.into_bytes())
.unwrap_or_else(|_| panic!("couldn't write css file: {}", &css_file));

String::from(&css_file[1..])
}
Expand Down Expand Up @@ -377,10 +374,9 @@ fn render_index(lang: String) -> Template {

let page = "index".to_string();
let data = IndexData {
rust_version: rust_version::rust_version().unwrap_or(String::new()),
rust_release_post: rust_version::rust_release_post().map_or(String::new(), |v| {
format!("https://blog.rust-lang.org/{}", v)
}),
rust_version: rust_version::rust_version().unwrap_or_else(String::new),
rust_release_post: rust_version::rust_release_post()
.map_or_else(String::new, |v| format!("https://blog.rust-lang.org/{}", v)),
};
let context = Context::new(page.clone(), "", true, data, lang);
Template::render(page, &context)
Expand Down Expand Up @@ -466,9 +462,9 @@ fn render_team(
}

fn render_subject(category: Category, subject: String, lang: String) -> Template {
let page = format!("{}/{}", category.name(), subject.as_str()).to_string();
let page = format!("{}/{}", category.name(), subject.as_str());
let title_id = format!("{}-{}-page-title", category.name(), subject);
let context = Context::new(subject.clone(), &title_id, false, (), lang);
let context = Context::new(subject, &title_id, false, (), lang);

Template::render(page, &context)
}
Expand Down
7 changes: 3 additions & 4 deletions src/rust_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@ use toml;
static MANIFEST_URL: &str = "https://static.rust-lang.org/dist/channel-rust-stable.toml";
static RELEASES_FEED_URL: &str = "https://blog.rust-lang.org/releases.json";

fn fetch_rust_version() -> Result<Box<Any>, Box<Error>> {
fn fetch_rust_version() -> Result<Box<dyn Any>, Box<dyn Error>> {
let manifest = reqwest::get(MANIFEST_URL)?.text()?.parse::<toml::Value>()?;
let rust_version = manifest["pkg"]["rust"]["version"].as_str().unwrap();
let version: String =
rust_version[..rust_version.find(' ').unwrap_or(rust_version.len())].to_string();
let version: String = rust_version.split(' ').next().unwrap().to_owned();
Ok(Box::new(version))
}

fn fetch_rust_release_post() -> Result<Box<Any>, Box<Error>> {
fn fetch_rust_release_post() -> Result<Box<dyn Any>, Box<dyn Error>> {
let releases = reqwest::get(RELEASES_FEED_URL)?
.text()?
.parse::<serde_json::Value>()?;
Expand Down
15 changes: 7 additions & 8 deletions src/teams.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct Data {
}

impl Data {
fn load() -> Result<Self, Box<Error>> {
fn load() -> Result<Self, Box<dyn Error>> {
Ok(Data {
teams: crate::cache::get::<Vec<Team>>(get_teams)?,
})
Expand All @@ -42,7 +42,7 @@ impl Data {
Data { teams }
}

fn index_data(self) -> Result<IndexData, Box<Error>> {
fn index_data(self) -> Result<IndexData, Box<dyn Error>> {
let mut data = IndexData::default();

self.teams
Expand Down Expand Up @@ -72,14 +72,13 @@ impl Data {
Ok(data)
}

pub fn page_data(self, section: &str, team_name: &str) -> Result<PageData, Box<Error>> {
pub fn page_data(self, section: &str, team_name: &str) -> Result<PageData, Box<dyn Error>> {
// Find the main team first
let main_team = self
.teams
.iter()
.filter(|team| team.website_data.as_ref().map(|ws| ws.page.as_str()) == Some(team_name))
.filter(|team| kind_to_str(team.kind) == section)
.next()
.find(|team| kind_to_str(team.kind) == section)
.cloned()
.ok_or(TeamNotFound)?;

Expand Down Expand Up @@ -109,15 +108,15 @@ impl Data {
}
}

pub fn index_data() -> Result<IndexData, Box<Error>> {
pub fn index_data() -> Result<IndexData, Box<dyn Error>> {
Data::load()?.index_data()
}

pub fn page_data(section: &str, team_name: &str) -> Result<PageData, Box<Error>> {
pub fn page_data(section: &str, team_name: &str) -> Result<PageData, Box<dyn Error>> {
Data::load()?.page_data(section, team_name)
}

fn get_teams() -> Result<Box<Any>, Box<Error>> {
fn get_teams() -> Result<Box<dyn Any>, Box<dyn Error>> {
let resp: Teams = reqwest::get(&format!("{}/teams.json", BASE_URL))?
.error_for_status()?
.json()?;
Expand Down