topcoat_font/resolver.rs
1use std::fmt::Write;
2
3use crate::Font;
4
5/// Function that formats the URL at which a [`Font`]'s CSS is hosted by the
6/// router into a `dyn Write`.
7pub type ResolveFontRouteFn = dyn Fn(Font, &mut dyn Write) -> std::fmt::Result + Send + Sync;
8
9/// Function registered with the app context that resolves a [`Font`] to its
10/// hosted stylesheet URL.
11///
12/// Registered by [`RouterBuilderFontExt`](crate::RouterBuilderFontExt)
13/// when a font is added to the router, and read when a [`Font`] is used as an
14/// attribute value in the `view!` macro.
15pub struct FontResolver {
16 resolve_fn: Box<ResolveFontRouteFn>,
17}
18
19impl FontResolver {
20 /// Builds a resolver from a callback.
21 #[must_use]
22 pub fn new(resolve_fn: Box<ResolveFontRouteFn>) -> Self {
23 Self { resolve_fn }
24 }
25
26 /// Invokes the underlying callback.
27 ///
28 /// # Errors
29 ///
30 /// Propagates errors of the registered [`ResolveFontRouteFn`].
31 pub fn resolve(&self, font: Font, write: &mut dyn Write) -> std::fmt::Result {
32 (self.resolve_fn)(font, write)
33 }
34}