Skip to content

Commit 50d6035

Browse files
move fontawesome icons check at compile-time
1 parent 7cb5bb0 commit 50d6035

18 files changed

+138
-139
lines changed

src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ pub use self::registry_api::RegistryApi;
1313
pub use self::storage::{AsyncStorage, Storage};
1414
pub use self::web::{start_background_metrics_webserver, start_web_server};
1515

16+
pub(crate) use font_awesome_as_a_crate as f_a;
17+
1618
mod build_queue;
1719
pub mod cdn;
1820
mod config;

src/web/page/mod.rs

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,29 @@ pub(crate) mod web_page;
33

44
pub(crate) use templates::TemplateData;
55

6-
use serde::Serialize;
6+
use crate::f_a::IconStr;
7+
use serde::ser::{Serialize, SerializeStruct, Serializer};
78

8-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize)]
9+
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
910
pub(crate) struct GlobalAlert {
1011
pub(crate) url: &'static str,
1112
pub(crate) text: &'static str,
1213
pub(crate) css_class: &'static str,
13-
pub(crate) fa_icon: &'static str,
14+
pub(crate) fa_icon: crate::f_a::icons::IconTriangleExclamation,
15+
}
16+
17+
impl Serialize for GlobalAlert {
18+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
19+
where
20+
S: Serializer,
21+
{
22+
let mut s = serializer.serialize_struct("GlobalAlert", 4)?;
23+
s.serialize_field("url", &self.url)?;
24+
s.serialize_field("text", &self.text)?;
25+
s.serialize_field("css_class", &self.css_class)?;
26+
s.serialize_field("fa_icon", &self.fa_icon.icon_name())?;
27+
s.end()
28+
}
1429
}
1530

1631
#[cfg(test)]
@@ -24,14 +39,14 @@ mod tera_tests {
2439
url: "http://www.hasthelargehadroncolliderdestroyedtheworldyet.com/",
2540
text: "THE WORLD WILL SOON END",
2641
css_class: "THE END IS NEAR",
27-
fa_icon: "https://gph.is/1uOvmqR",
42+
fa_icon: crate::f_a::icons::IconTriangleExclamation,
2843
};
2944

3045
let correct_json = json!({
3146
"url": "http://www.hasthelargehadroncolliderdestroyedtheworldyet.com/",
3247
"text": "THE WORLD WILL SOON END",
3348
"css_class": "THE END IS NEAR",
34-
"fa_icon": "https://gph.is/1uOvmqR"
49+
"fa_icon": "triangle-exclamation"
3550
});
3651

3752
assert_eq!(correct_json, serde_json::to_value(alert).unwrap());

src/web/page/templates.rs

Lines changed: 44 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::error::Result;
22
use crate::web::rustdoc::RustdocPage;
3-
use anyhow::{anyhow, Context};
3+
use anyhow::Context;
44
use rinja::Template;
5-
use std::{fmt, sync::Arc};
5+
use std::sync::Arc;
66
use tracing::trace;
77

88
#[derive(Template)]
@@ -88,7 +88,6 @@ impl TemplateData {
8888
}
8989

9090
pub mod filters {
91-
use super::IconType;
9291
use chrono::{DateTime, Utc};
9392
use rinja::filters::Safe;
9493
use std::borrow::Cow;
@@ -201,16 +200,31 @@ pub mod filters {
201200
Ok(unindented)
202201
}
203202

204-
pub fn fas(value: &str, fw: bool, spin: bool, extra: &str) -> rinja::Result<Safe<String>> {
205-
IconType::Strong.render(value, fw, spin, extra).map(Safe)
203+
pub fn fas<T: font_awesome_as_a_crate::Solid>(
204+
value: T,
205+
fw: bool,
206+
spin: bool,
207+
extra: &str,
208+
) -> rinja::Result<Safe<String>> {
209+
super::render_icon(value.icon_str(), fw, spin, extra)
206210
}
207211

208-
pub fn far(value: &str, fw: bool, spin: bool, extra: &str) -> rinja::Result<Safe<String>> {
209-
IconType::Regular.render(value, fw, spin, extra).map(Safe)
212+
pub fn far<T: font_awesome_as_a_crate::Regular>(
213+
value: T,
214+
fw: bool,
215+
spin: bool,
216+
extra: &str,
217+
) -> rinja::Result<Safe<String>> {
218+
super::render_icon(value.icon_str(), fw, spin, extra)
210219
}
211220

212-
pub fn fab(value: &str, fw: bool, spin: bool, extra: &str) -> rinja::Result<Safe<String>> {
213-
IconType::Brand.render(value, fw, spin, extra).map(Safe)
221+
pub fn fab<T: font_awesome_as_a_crate::Brands>(
222+
value: T,
223+
fw: bool,
224+
spin: bool,
225+
extra: &str,
226+
) -> rinja::Result<Safe<String>> {
227+
super::render_icon(value.icon_str(), fw, spin, extra)
214228
}
215229

216230
pub fn highlight(code: impl std::fmt::Display, lang: &str) -> rinja::Result<Safe<String>> {
@@ -241,59 +255,27 @@ pub mod filters {
241255
}
242256
}
243257

244-
enum IconType {
245-
Strong,
246-
Regular,
247-
Brand,
248-
}
249-
250-
impl fmt::Display for IconType {
251-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
252-
let icon = match self {
253-
Self::Strong => "solid",
254-
Self::Regular => "regular",
255-
Self::Brand => "brands",
256-
};
257-
258-
f.write_str(icon)
258+
fn render_icon(
259+
icon_str: &str,
260+
fw: bool,
261+
spin: bool,
262+
extra: &str,
263+
) -> rinja::Result<rinja::filters::Safe<String>> {
264+
let mut classes = vec!["fa-svg"];
265+
if fw {
266+
classes.push("fa-svg-fw");
259267
}
260-
}
261-
262-
impl IconType {
263-
fn render(self, icon_name: &str, fw: bool, spin: bool, extra: &str) -> rinja::Result<String> {
264-
let type_ = match self {
265-
IconType::Strong => font_awesome_as_a_crate::Type::Solid,
266-
IconType::Regular => font_awesome_as_a_crate::Type::Regular,
267-
IconType::Brand => font_awesome_as_a_crate::Type::Brands,
268-
};
269-
270-
let icon_file_string = font_awesome_as_a_crate::svg(type_, icon_name).map_err(|err| {
271-
rinja::Error::Custom(
272-
anyhow!(err)
273-
.context(format!(
274-
"error trying to render icon with name \"{}\" and type \"{}\"",
275-
icon_name, type_,
276-
))
277-
.into(),
278-
)
279-
})?;
280-
281-
let mut classes = vec!["fa-svg"];
282-
if fw {
283-
classes.push("fa-svg-fw");
284-
}
285-
if spin {
286-
classes.push("fa-svg-spin");
287-
}
288-
if !extra.is_empty() {
289-
classes.push(extra);
290-
}
291-
let icon = format!(
292-
"\
293-
<span class=\"{class}\" aria-hidden=\"true\">{icon_file_string}</span>",
294-
class = classes.join(" "),
295-
);
296-
297-
Ok(icon)
268+
if spin {
269+
classes.push("fa-svg-spin");
298270
}
271+
if !extra.is_empty() {
272+
classes.push(extra);
273+
}
274+
let icon = format!(
275+
"\
276+
<span class=\"{class}\" aria-hidden=\"true\">{icon_str}</span>",
277+
class = classes.join(" "),
278+
);
279+
280+
Ok(rinja::filters::Safe(icon))
299281
}

templates/about-base.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,27 @@
1111
<h1 id="crate-title" class="no-description">Docs.rs documentation</h1>
1212
<div class="pure-menu pure-menu-horizontal">
1313
<ul class="pure-menu-list">
14-
{% set text = "circle-info"|fas(false, false, "") %}
14+
{% set text = crate::f_a::icons::IconCircleInfo|fas(false, false, "") %}
1515
{% set text = "{} <span class='title'>About</span>"|format(text) %}
1616
{% call macros::active_link(expected="index", href="/about", text=text) %}
1717

18-
{% set text = "fonticons"|fab(false, false, "") %}
18+
{% set text = crate::f_a::icons::IconFonticons|fab(false, false, "") %}
1919
{% set text = "{} <span class='title'>Badges</span>"|format(text) %}
2020
{% call macros::active_link(expected="badges", href="/about/badges", text=text) %}
2121

22-
{% set text = "gears"|fas(false, false, "") %}
22+
{% set text = crate::f_a::icons::IconGears|fas(false, false, "") %}
2323
{% set text = "{} <span class='title'>Builds</span>"|format(text) %}
2424
{% call macros::active_link(expected="builds", href="/about/builds", text=text) %}
2525

26-
{% set text = "table"|fas(false, false, "") %}
26+
{% set text = crate::f_a::icons::IconTable|fas(false, false, "") %}
2727
{% set text = "{} <span class='title'>Metadata</span>"|format(text) %}
2828
{% call macros::active_link(expected="metadata", href="/about/metadata", text=text) %}
2929

30-
{% set text = "road"|fas(false, false, "") %}
30+
{% set text = crate::f_a::icons::IconRoad|fas(false, false, "") %}
3131
{% set text = "{} <span class='title'>Shorthand URLs</span>"|format(text) %}
3232
{% call macros::active_link(expected="redirections", href="/about/redirections", text=text) %}
3333

34-
{% set text = "download"|fas(false, false, "") %}
34+
{% set text = crate::f_a::icons::IconDownload|fas(false, false, "") %}
3535
{% set text = "{} <span class='title'>Download</span>"|format(text) %}
3636
{% call macros::active_link(expected="download", href="/about/download", text=text) %}
3737
</ul>

templates/core/home.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
{%- block body -%}
1414
<div class="container landing">
15-
<h1 class="brand">{{ "cubes"|fas(false, false, "") }} Docs.rs</h1>
15+
<h1 class="brand">{{ crate::f_a::icons::IconCubes|fas(false, false, "") }} Docs.rs</h1>
1616

1717
<form action="/releases/search" method="GET" class="landing-search-form">
1818
<div>
@@ -36,7 +36,7 @@ <h1 class="brand">{{ "cubes"|fas(false, false, "") }} Docs.rs</h1>
3636
<strong>Recent Releases</strong>
3737
</a>
3838
<a href="/releases/feed" title="Atom feed">
39-
{{ "square-rss"|fas(false, false, "") }}
39+
{{ crate::f_a::icons::IconSquareRss|fas(false, false, "") }}
4040
</a>
4141
</div>
4242

templates/crate/build_details.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
<li>
3131
<a href="/crate/{{ metadata.name }}/{{ metadata.version }}/builds/{{ build_details.id }}/{{ filename }}" class="release">
3232
<div class="pure-g">
33-
<div class="pure-u-1 pure-u-sm-1-24 build">{{ "file-lines"|fas(false, false, "") }}</div>
33+
<div class="pure-u-1 pure-u-sm-1-24 build">{{ crate::f_a::icons::IconFileLines|fas(false, false, "") }}</div>
3434
<div class="pure-u-1 pure-u-sm-10-24">
3535
{% if current_filename.as_deref().unwrap_or_default() == filename.as_str() %}
3636
<b>{{ filename }}</b>

templates/crate/builds.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,13 @@
4444
<div class="pure-g">
4545
<div class="pure-u-1 pure-u-sm-1-24 build">
4646
{%- if build.build_status == "success" -%}
47-
{{ "check"|fas(false, false, "") }}
47+
{{ crate::f_a::icons::IconCheck|fas(false, false, "") }}
4848
{%- elif build.build_status == "failure" -%}
49-
{{ "triangle-exclamation"|fas(false, false, "") }}
49+
{{ crate::f_a::icons::IconTriangleExclamation|fas(false, false, "") }}
5050
{%- elif build.build_status == "in_progress" -%}
51-
{{ "gear"|fas(false, true, "") }}
51+
{{ crate::f_a::icons::IconGear|fas(false, true, "") }}
5252
{%- else -%}
53-
{{ "x"|fas(false, false, "") }}
53+
{{ crate::f_a::icons::IconX|fas(false, false, "") }}
5454
{%- endif -%}
5555
</div>
5656
<div class="pure-u-1 pure-u-sm-10-24">

templates/crate/details.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
{%- if let Some(homepage_url) = homepage_url -%}
4444
<li class="pure-menu-item">
4545
<a href="{{ homepage_url }}" class="pure-menu-link">
46-
{{ "house"|fas(false, false, "") }} Homepage
46+
{{ crate::f_a::icons::IconHouse|fas(false, false, "") }} Homepage
4747
</a>
4848
</li>
4949
{%- endif -%}
@@ -52,7 +52,7 @@
5252
{%- if let Some(documentation_url) = documentation_url -%}
5353
<li class="pure-menu-item">
5454
<a href="{{ documentation_url }}" title="Canonical documentation" class="pure-menu-link">
55-
{{ "file-lines"|far(false, false, "") }} Documentation
55+
{{ crate::f_a::icons::IconFileLines|far(false, false, "") }} Documentation
5656
</a>
5757
</li>
5858
{%- endif -%}
@@ -64,20 +64,20 @@
6464
{# If the repo link is for github or gitlab, show some stats #}
6565
{# TODO: add support for hosts besides github and gitlab (#35) #}
6666
{%- if let Some(repository_metadata) = repository_metadata -%}
67-
{{ "code-branch"|fas(false, false, "") }}
67+
{{ crate::f_a::icons::IconCodeBranch|fas(false, false, "") }}
6868
{% if let Some(name) = repository_metadata.name %}
6969
{{name}}
7070
{% else %}
7171
Repository
7272
{% endif %}
7373
<br>
74-
{{ "star"|fas(false, false, "left-margin") }} {{ repository_metadata.stars }}
75-
{{ "code-branch"|fas(false, false, "") }} {{ repository_metadata.forks }}
76-
{{ "circle-exclamation"|fas(false, false, "") }} {{ repository_metadata.issues }}
74+
{{ crate::f_a::icons::IconStar|fas(false, false, "left-margin") }} {{ repository_metadata.stars }}
75+
{{ crate::f_a::icons::IconCodeBranch|fas(false, false, "") }} {{ repository_metadata.forks }}
76+
{{ crate::f_a::icons::IconCircleExclamation|fas(false, false, "") }} {{ repository_metadata.issues }}
7777

7878
{# If the repo link is unknown, just show a normal link #}
7979
{%- else -%}
80-
{{ "code-branch"|fas(false, false, "") }} Repository
80+
{{ crate::f_a::icons::IconCodeBranch|fas(false, false, "") }} Repository
8181
{%- endif -%}
8282
</a>
8383
</li>
@@ -87,7 +87,7 @@
8787
<li class="pure-menu-item">
8888
<a href="https://crates.io/crates/{{ name }}" class="pure-menu-link"
8989
title="See {{ name }} on crates.io">
90-
{{ "cube"|fas(false, false, "") }} crates.io
90+
{{ crate::f_a::icons::IconCube|fas(false, false, "") }} crates.io
9191
</a>
9292
</li>
9393

@@ -160,7 +160,7 @@
160160
</div>
161161
{%- elif build_status == "in_progress" -%}
162162
<div class="info">
163-
{{ "gear"|fas(false, true, "") }}
163+
{{ crate::f_a::icons::IconGear|fas(false, true, "") }}
164164
Build is in progress, it will be available soon
165165
</div>
166166
{%- endif -%}

templates/crate/source.html

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@
2929
{# If we are displaying a file, we also add a button to hide the file sidebar #}
3030
{% if has_file_content %}
3131
<li class="pure-menu-item toggle-source">
32-
<button aria-label="Hide source sidebar" title="Hide source sidebar" aria-expanded="true"><span class="left">{{ "chevron-left"|fas(false, false, "") }}</span><span class="right">{{ "chevron-right"|fas(false, false, "") }}</span> <span class="text">Hide files</span></button>
32+
<button aria-label="Hide source sidebar" title="Hide source sidebar" aria-expanded="true"><span class="left">{{ crate::f_a::icons::IconChevronLeft|fas(false, false, "") }}</span><span class="right">{{ crate::f_a::icons::IconChevronRight|fas(false, false, "") }}</span> <span class="text">Hide files</span></button>
3333
</li>
3434
{% endif %}
3535
{# If this isn't the root folder, show a 'back' button #}
3636
{%- if show_parent_link -%}
3737
<li class="pure-menu-item">
38-
<a href="../" class="pure-menu-link">{{ "folder-open"|far(false, false, "") }} <span class="text">..</span></a>
38+
<a href="../" class="pure-menu-link">{{ crate::f_a::icons::IconFolderOpen|far(false, false, "") }} <span class="text">..</span></a>
3939
</li>
4040
{%- endif -%}
4141

@@ -48,23 +48,23 @@
4848
<a href="./{{ file.name }}{% if file.mime == "dir" %}/{% endif %}" class="pure-menu-link">
4949
{# Directories #}
5050
{%- if file.mime == "dir" -%}
51-
{{ "folder-open"|far(false, false, "") }}
51+
{{ crate::f_a::icons::IconFolderOpen|far(false, false, "") }}
5252

5353
{# Rust files #}
5454
{%- elif file.mime == "text/rust" -%}
55-
{{ "rust"|fab(false, false, "") }}
55+
{{ crate::f_a::icons::IconRust|fab(false, false, "") }}
5656

5757
{# Cargo.lock #}
5858
{%- elif file.mime == "text/plain" && file.name == "Cargo.lock" -%}
59-
{{ "lock"|fas(false, false, "") }}
59+
{{ crate::f_a::icons::IconLock|fas(false, false, "") }}
6060

6161
{# Markdown files #}
6262
{% elif file.mime == "text/markdown" %}
63-
{{ "markdown"|fab(false, false, "") }}
63+
{{ crate::f_a::icons::IconMarkdown|fab(false, false, "") }}
6464

6565
{# .gitignore #}
6666
{% elif file.mime == "text/plain" && file.name == ".gitignore" %}
67-
{{ "git-alt"|fab(false, false, "") }}
67+
{{ crate::f_a::icons::IconGitAlt|fab(false, false, "") }}
6868

6969
{#
7070
More ideas
@@ -86,11 +86,11 @@
8686

8787
{# Text files or files which mime starts with `text` #}
8888
{%- elif file.mime == "text/plain" || file.mime|split_first("/") == Some("text") -%}
89-
{{ "file-lines"|far(false, false, "") }}
89+
{{ crate::f_a::icons::IconFileLines|far(false, false, "") }}
9090

9191
{# Binary files and any unrecognized types #}
9292
{% else -%}
93-
{{ "file"|far(false, false, "") }}
93+
{{ crate::f_a::icons::IconFile|far(false, false, "") }}
9494
{%- endif -%}
9595

9696
<span class="text">{{ file.name }}</span>

0 commit comments

Comments
 (0)