use lightningcss::error::{
MinifyErrorKind,
ParserError,
PrinterErrorKind,
};
use std::{
error::Error,
fmt,
};
#[derive(Debug, Clone)]
pub enum GuffError {
#[cfg(feature = "bin")]
Browser(String),
#[cfg(feature = "bin")]
Cli(String),
Css(String),
#[cfg(feature = "bin")]
NoSource,
PathExt,
PathUtf8,
Scss(String),
SourceFileName,
SourceInvalid,
#[cfg(feature = "bin")]
Write,
#[cfg(feature = "bin")]
PrintHelp,
#[cfg(feature = "bin")]
PrintVersion,
}
impl Error for GuffError {}
impl fmt::Display for GuffError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Css(s) | Self::Scss(s) => write!(f, "{} {s}", self.as_str()),
#[cfg(feature = "bin")]
Self::Browser(s) | Self::Cli(s) => write!(f, "{} {s}", self.as_str()),
_ => f.write_str(self.as_str()),
}
}
}
impl From<Box<grass::Error>> for GuffError {
#[inline]
fn from(err: Box<grass::Error>) -> Self { Self::Scss(err.to_string()) }
}
macro_rules! from_parcel {
($($ty:ty),+) => ($(
impl From<lightningcss::error::Error<$ty>> for GuffError {
#[inline]
fn from(err: lightningcss::error::Error<$ty>) -> Self {
Self::Css(err.kind.to_string())
}
}
)+);
}
from_parcel!(MinifyErrorKind, ParserError<'_>, PrinterErrorKind);
impl GuffError {
#[must_use]
pub const fn as_str(&self) -> &'static str {
match self {
#[cfg(feature = "bin")]
Self::Browser(_) => "Invalid browser:",
#[cfg(feature = "bin")]
Self::Cli(_) => "Invalid/unknown option:",
Self::Css(_) => "Unable to parse CSS:",
#[cfg(feature = "bin")]
Self::NoSource => "An SCSS/CSS source is required.",
Self::PathExt => "Paths must contain a .css, .sass, or .scss extension.",
Self::PathUtf8 => "Paths must be valid UTF-8.",
Self::Scss(_) => "Unable to parse SCSS:",
Self::SourceFileName => "File paths must be valid UTF-8.",
Self::SourceInvalid => "Invalid/unreadable SCSS/CSS source.",
#[cfg(feature = "bin")]
Self::Write => "The output could not be saved to disk.",
#[cfg(feature = "bin")]
Self::PrintHelp | Self::PrintVersion => "",
}
}
}