| 
1 | 1 | use crate::error::Result;  | 
2 | 2 | use crate::web::rustdoc::RustdocPage;  | 
3 |  | -use anyhow::{anyhow, Context};  | 
 | 3 | +use anyhow::Context;  | 
4 | 4 | use rinja::Template;  | 
5 |  | -use std::{fmt, sync::Arc};  | 
 | 5 | +use std::sync::Arc;  | 
6 | 6 | use tracing::trace;  | 
7 | 7 | 
 
  | 
8 | 8 | #[derive(Template)]  | 
@@ -88,7 +88,6 @@ impl TemplateData {  | 
88 | 88 | }  | 
89 | 89 | 
 
  | 
90 | 90 | pub mod filters {  | 
91 |  | -    use super::IconType;  | 
92 | 91 |     use chrono::{DateTime, Utc};  | 
93 | 92 |     use rinja::filters::Safe;  | 
94 | 93 |     use std::borrow::Cow;  | 
@@ -201,16 +200,31 @@ pub mod filters {  | 
201 | 200 |         Ok(unindented)  | 
202 | 201 |     }  | 
203 | 202 | 
 
  | 
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)  | 
206 | 210 |     }  | 
207 | 211 | 
 
  | 
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)  | 
210 | 219 |     }  | 
211 | 220 | 
 
  | 
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)  | 
214 | 228 |     }  | 
215 | 229 | 
 
  | 
216 | 230 |     pub fn highlight(code: impl std::fmt::Display, lang: &str) -> rinja::Result<Safe<String>> {  | 
@@ -241,59 +255,27 @@ pub mod filters {  | 
241 | 255 |     }  | 
242 | 256 | }  | 
243 | 257 | 
 
  | 
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_name: &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");  | 
259 | 267 |     }  | 
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");  | 
298 | 270 |     }  | 
 | 271 | +    if !extra.is_empty() {  | 
 | 272 | +        classes.push(extra);  | 
 | 273 | +    }  | 
 | 274 | +    let icon = format!(  | 
 | 275 | +        "\  | 
 | 276 | +<span class=\"{class}\" aria-hidden=\"true\">{icon_name}</span>",  | 
 | 277 | +        class = classes.join(" "),  | 
 | 278 | +    );  | 
 | 279 | + | 
 | 280 | +    Ok(rinja::filters::Safe(icon))  | 
299 | 281 | }  | 
0 commit comments