use std::fmt;
use std::path::PathBuf;
#[cfg(windows)]
mod ico;
#[cfg(windows)]
mod resources;
#[cfg(windows)]
mod swap;
#[cfg(windows)]
mod win;
#[cfg(windows)]
pub use swap::{current_icon, init, set_icon};
pub trait Icon {
fn name(&self) -> &'static str;
fn bytes(&self) -> &'static [u8];
}
#[macro_export]
macro_rules! include_icons {
() => {
include!(concat!(env!("OUT_DIR"), "/alt_icons.rs"));
};
}
#[derive(Debug)]
pub enum Error {
SwapInProgress,
PathNotWritable(PathBuf),
MalformedIcon(&'static str),
Windows(&'static str, u32),
Io(std::io::Error),
UnsupportedPlatform,
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::SwapInProgress => {
write!(
f,
"another process is already changing this executable's icon"
)
}
Error::PathNotWritable(dir) => {
write!(f, "cannot write to {}", dir.display())
}
Error::MalformedIcon(why) => write!(f, "malformed icon: {why}"),
Error::Windows(call, code) => write!(f, "{call} failed with error {code}"),
Error::Io(err) => write!(f, "{err}"),
Error::UnsupportedPlatform => write!(f, "alt-icons only works on Windows"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(err) => Some(err),
_ => None,
}
}
}
impl From<std::io::Error> for Error {
fn from(err: std::io::Error) -> Self {
Error::Io(err)
}
}
#[cfg(not(windows))]
mod stubs {
use super::{Error, Icon};
pub fn init() -> Result<(), Error> {
Ok(())
}
pub fn set_icon<I: Icon>(_icon: I) -> Result<(), Error> {
Err(Error::UnsupportedPlatform)
}
pub fn current_icon() -> Result<Option<String>, Error> {
Ok(None)
}
}
#[cfg(not(windows))]
pub use stubs::{current_icon, init, set_icon};