winbrew_engines\windows/
font.rs

1use anyhow::{Context, Result};
2use std::path::Path;
3
4use tracing::warn;
5
6use crate::core::fs::cleanup_path;
7use crate::models::catalog::package::CatalogInstaller;
8use crate::models::install::engine::{EngineInstallReceipt, EngineKind};
9use crate::models::install::installed::InstalledPackage;
10
11#[cfg(windows)]
12use crate::windows_dep::fonts::{install_user_font, remove_user_font};
13
14/// Install a per-user font by copying the downloaded font file into the
15/// Windows user fonts directory.
16pub(crate) fn install(
17    _installer: &CatalogInstaller,
18    download_path: &Path,
19    _install_dir: &Path,
20    package_name: &str,
21) -> Result<EngineInstallReceipt> {
22    #[cfg(not(windows))]
23    {
24        let _ = (_installer, download_path, package_name);
25        anyhow::bail!("font installation is only supported on Windows")
26    }
27
28    #[cfg(windows)]
29    {
30        let installed_path = install_user_font(download_path)
31            .with_context(|| format!("failed to install font package for {package_name}"))?;
32
33        Ok(EngineInstallReceipt::new(
34            EngineKind::Font,
35            installed_path.to_string_lossy().into_owned(),
36            None,
37        ))
38    }
39}
40
41/// Remove a per-user font file and its backing filesystem registration.
42pub(crate) fn remove(package: &InstalledPackage) -> Result<()> {
43    #[cfg(not(windows))]
44    {
45        let _ = package;
46        anyhow::bail!("font removal is only supported on Windows")
47    }
48
49    #[cfg(windows)]
50    {
51        if let Err(err) = remove_user_font(Path::new(&package.install_dir)) {
52            warn!(
53                package = package.name.as_str(),
54                error = %err,
55                "font removal helper reported an error; continuing with filesystem cleanup"
56            );
57        }
58
59        let _ = cleanup_path(Path::new(&package.install_dir));
60
61        Ok(())
62    }
63}