use crate::paint::{Border, BorderStyle, Color, DEFAULT_BORDER_WIDTH};
use crate::style::{StyleError, box_values::unsupported, component::Component, grammar::Keyword};
use serde::Serialize;
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq, Serialize)]
pub(crate) enum ColumnFill {
Auto,
#[default]
Balance,
BalanceAll,
}
impl ColumnFill {
pub(crate) fn from_component(value: &Component) -> Result<Self, StyleError> {
match value.keyword() {
Some(Keyword::Auto) => Ok(Self::Auto),
Some(Keyword::Balance) => Ok(Self::Balance),
Some(Keyword::BalanceAll) => Ok(Self::BalanceAll),
_ => Err(unsupported(value)),
}
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
pub(crate) struct MulticolStyle {
pub count: Option<u32>,
pub width: Option<f32>,
pub fill: ColumnFill,
pub span_all: bool,
pub rule: Border,
}
impl Default for MulticolStyle {
fn default() -> Self {
Self {
count: None,
width: None,
fill: ColumnFill::Balance,
span_all: false,
rule: Border {
width: DEFAULT_BORDER_WIDTH,
style: BorderStyle::None,
color: Color::BLACK,
},
}
}
}
impl MulticolStyle {
pub(crate) fn is_multicol(&self) -> bool {
self.count.is_some() || self.width.is_some()
}
}
pub(crate) fn count(
value: &Component,
relative: &crate::style::units::Relative<'_>,
) -> Result<Option<u32>, StyleError> {
if value.keyword() == Some(Keyword::Auto) {
return Ok(None);
}
u32::try_from(crate::style::values::integer(value, relative)?)
.ok()
.filter(|count| *count > 0)
.map(Some)
.ok_or_else(|| format!("invalid column count: {value}").into())
}
pub(crate) fn width(
value: &Component,
relative: &crate::style::units::Relative<'_>,
) -> Result<Option<f32>, StyleError> {
if value.keyword() == Some(Keyword::Auto) {
return Ok(None);
}
let width = crate::style::values::length(value, relative)?;
if !width.is_finite() || width < 0.0 {
return Err(format!("invalid column width: {value}").into());
}
Ok(Some(width))
}