Skip to content

Commit 3f01de5

Browse files
committed
Fix warnings & clippy lints. Deny warnings on test
1 parent fea5cb9 commit 3f01de5

File tree

6 files changed

+34
-34
lines changed

6 files changed

+34
-34
lines changed

src/cache.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ use std::sync::RwLock;
55
use std::thread;
66
use std::time::Instant;
77

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

1011
const CACHE_TTL_SECS: u64 = 120;
1112

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

17-
pub fn get<T>(generator: Generator) -> Result<T, Box<Error>>
17+
pub fn get<T>(generator: Generator) -> Result<T, Box<dyn Error>>
1818
where
1919
T: Send + Sync + Clone + 'static,
2020
{
@@ -41,7 +41,7 @@ where
4141
})
4242
}
4343

44-
fn update_cache<T>(generator: Generator) -> Result<T, Box<Error>>
44+
fn update_cache<T>(generator: Generator) -> Result<T, Box<dyn Error>>
4545
where
4646
T: Send + Sync + Clone + 'static,
4747
{
@@ -70,7 +70,7 @@ mod tests {
7070
fn test_cache_basic() {
7171
static GENERATOR_CALLED: AtomicBool = AtomicBool::new(false);
7272

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

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

128-
fn generator() -> Result<Box<Any>, Box<Error>> {
128+
fn generator() -> Result<Box<dyn Any>, Box<dyn Error>> {
129129
GENERATOR_CALLED.store(true, Ordering::SeqCst);
130130
Err("an error".into())
131131
}

src/caching.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ where
3434
fn cached(self, directives: Vec<CacheDirective>) -> Cached<Self> {
3535
Cached {
3636
inner: self,
37-
directives: directives,
37+
directives,
3838
}
3939
}
4040
}

src/i18n.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ pub struct TeamHelper {
8787

8888
impl TeamHelper {
8989
pub fn new() -> Self {
90+
Self::default()
91+
}
92+
}
93+
94+
impl Default for TeamHelper {
95+
fn default() -> Self {
9096
Self {
9197
i18n: create_loader(),
9298
}

src/main.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![feature(proc_macro_hygiene, decl_macro)]
2+
#![cfg_attr(test, deny(warnings))]
23

34
#[macro_use]
45
extern crate lazy_static;
@@ -38,9 +39,7 @@ use production::User;
3839
use std::collections::hash_map::DefaultHasher;
3940
use std::env;
4041
use std::fs;
41-
use std::fs::File;
4242
use std::hash::Hasher;
43-
use std::io::prelude::*;
4443
use std::path::{Path, PathBuf};
4544

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

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

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

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

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

378375
let page = "index".to_string();
379376
let data = IndexData {
380-
rust_version: rust_version::rust_version().unwrap_or(String::new()),
381-
rust_release_post: rust_version::rust_release_post().map_or(String::new(), |v| {
382-
format!("https://blog.rust-lang.org/{}", v)
383-
}),
377+
rust_version: rust_version::rust_version().unwrap_or_else(String::new),
378+
rust_release_post: rust_version::rust_release_post()
379+
.map_or_else(String::new, |v| format!("https://blog.rust-lang.org/{}", v)),
384380
};
385381
let context = Context::new(page.clone(), "", true, data, lang);
386382
Template::render(page, &context)
@@ -466,9 +462,9 @@ fn render_team(
466462
}
467463

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

473469
Template::render(page, &context)
474470
}

src/rust_version.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ use toml;
77
static MANIFEST_URL: &str = "https://static.rust-lang.org/dist/channel-rust-stable.toml";
88
static RELEASES_FEED_URL: &str = "https://blog.rust-lang.org/releases.json";
99

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

18-
fn fetch_rust_release_post() -> Result<Box<Any>, Box<Error>> {
17+
fn fetch_rust_release_post() -> Result<Box<dyn Any>, Box<dyn Error>> {
1918
let releases = reqwest::get(RELEASES_FEED_URL)?
2019
.text()?
2120
.parse::<serde_json::Value>()?;

src/teams.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct Data {
3131
}
3232

3333
impl Data {
34-
fn load() -> Result<Self, Box<Error>> {
34+
fn load() -> Result<Self, Box<dyn Error>> {
3535
Ok(Data {
3636
teams: crate::cache::get::<Vec<Team>>(get_teams)?,
3737
})
@@ -42,7 +42,7 @@ impl Data {
4242
Data { teams }
4343
}
4444

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

4848
self.teams
@@ -72,14 +72,13 @@ impl Data {
7272
Ok(data)
7373
}
7474

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

@@ -109,15 +108,15 @@ impl Data {
109108
}
110109
}
111110

112-
pub fn index_data() -> Result<IndexData, Box<Error>> {
111+
pub fn index_data() -> Result<IndexData, Box<dyn Error>> {
113112
Data::load()?.index_data()
114113
}
115114

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

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

0 commit comments

Comments
 (0)