use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum OutputFormat {
#[default]
Png,
Pdf,
}
impl std::fmt::Display for OutputFormat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
OutputFormat::Png => write!(f, "png"),
OutputFormat::Pdf => write!(f, "pdf"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum ColorScheme {
#[default]
Light,
Dark,
}
impl From<ColorScheme> for blitz_traits::shell::ColorScheme {
fn from(scheme: ColorScheme) -> Self {
match scheme {
ColorScheme::Light => blitz_traits::shell::ColorScheme::Light,
ColorScheme::Dark => blitz_traits::shell::ColorScheme::Dark,
}
}
}
#[derive(Debug, Clone)]
pub struct Config {
pub width: u32,
pub height: u32,
pub scale: f32,
pub format: OutputFormat,
pub color_scheme: ColorScheme,
pub auto_height: bool,
pub background: [u8; 4],
}
impl Default for Config {
fn default() -> Self {
Self {
width: 800,
height: 600,
scale: 1.0,
format: OutputFormat::Png,
color_scheme: ColorScheme::Light,
auto_height: false,
background: [255, 255, 255, 255], }
}
}
impl Config {
pub fn new() -> Self {
Self::default()
}
pub fn width(mut self, width: u32) -> Self {
self.width = width;
self
}
pub fn height(mut self, height: u32) -> Self {
self.height = height;
self
}
pub fn size(mut self, width: u32, height: u32) -> Self {
self.width = width;
self.height = height;
self
}
pub fn scale(mut self, scale: f32) -> Self {
self.scale = scale;
self
}
pub fn format(mut self, format: OutputFormat) -> Self {
self.format = format;
self
}
pub fn color_scheme(mut self, scheme: ColorScheme) -> Self {
self.color_scheme = scheme;
self
}
pub fn auto_height(mut self, auto: bool) -> Self {
self.auto_height = auto;
self
}
pub fn background(mut self, rgba: [u8; 4]) -> Self {
self.background = rgba;
self
}
pub fn transparent(self) -> Self {
self.background([0, 0, 0, 0])
}
pub const MIN_DIMENSION: u32 = 16;
pub fn validate(&self) -> Result<()> {
if self.width < Self::MIN_DIMENSION {
return Err(Error::InvalidConfig(format!(
"width must be at least {} pixels",
Self::MIN_DIMENSION
)));
}
if self.height < Self::MIN_DIMENSION {
return Err(Error::InvalidConfig(format!(
"height must be at least {} pixels",
Self::MIN_DIMENSION
)));
}
if self.scale <= 0.0 {
return Err(Error::InvalidConfig(
"scale must be greater than 0".to_string(),
));
}
if !self.scale.is_finite() {
return Err(Error::InvalidConfig(
"scale must be a finite number".to_string(),
));
}
Ok(())
}
}