mod background;
mod colors;
mod ellipse;
mod icon;
mod polygon;
mod rectangle;
mod size_offset;
mod typography;
#[cfg(feature = "pyo3")]
use pyo3::prelude::*;
use serde::Deserialize;
use std::{fmt, num::NonZeroU32};
pub use background::Background;
pub use colors::{
ColorGradient, ColorKind, ConicalGradient, LinearGradient, Presets, RadialGradient, SolidColor,
Spread, TRANSPARENT,
};
pub use ellipse::{Arc, Ellipse};
pub use icon::Icon;
pub use polygon::{IrregularPolygonSides, Polygon, PolygonSides, RegularPolygonSides};
pub use rectangle::{Corners, Rectangle};
pub use size_offset::{HEIGHT, LayerOffset, Size, WIDTH};
pub use typography::{Font, Line, LineHeight, Typography, TypographyAlign, Weight};
#[cfg_attr(
feature = "pyo3",
pyclass(module = "img_gen", set_all, get_all, from_py_object)
)]
#[derive(Debug, Clone, Deserialize)]
pub struct Border {
pub width: NonZeroU32,
pub color: ColorKind,
}
pub const BORDER_WIDTH: NonZeroU32 = NonZeroU32::new(1).unwrap();
impl Default for Border {
fn default() -> Self {
Self {
width: BORDER_WIDTH,
color: ColorKind::default(),
}
}
}
#[cfg_attr(
feature = "pyo3",
pyclass(eq, eq_int, module = "img_gen", from_py_object)
)]
#[derive(Debug, PartialEq, Clone, Copy, Default, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PreserveAspect {
#[serde(alias = "true")]
#[default]
On,
#[serde(alias = "false")]
Off,
Width,
Height,
}
impl PreserveAspect {
pub fn from_string(value: &str) -> PreserveAspect {
match value.to_lowercase().as_str() {
"false" => PreserveAspect::Off,
"width" => PreserveAspect::Width,
"height" => PreserveAspect::Height,
_ => PreserveAspect::On,
}
}
}
impl fmt::Display for PreserveAspect {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PreserveAspect::On => write!(f, "true"),
PreserveAspect::Off => write!(f, "false"),
PreserveAspect::Width => write!(f, "width"),
PreserveAspect::Height => write!(f, "height"),
}
}
}
#[cfg(test)]
mod test {
use super::PreserveAspect;
#[test]
fn test_str() {
let str_values = ["true", "false", "width", "height"];
for str_val in str_values {
let val = PreserveAspect::from_string(str_val).to_string();
assert_eq!(val, str_val);
}
assert_eq!(PreserveAspect::from_string("val").to_string(), "true");
}
}