use crate::text::{Alignment, Baseline, LineHeight};
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[non_exhaustive]
pub struct TextStyle {
pub alignment: Alignment,
pub baseline: Baseline,
pub line_height: LineHeight,
}
impl TextStyle {
pub const fn with_baseline(baseline: Baseline) -> Self {
TextStyleBuilder::new().baseline(baseline).build()
}
pub const fn with_alignment(alignment: Alignment) -> Self {
TextStyleBuilder::new().alignment(alignment).build()
}
}
impl Default for TextStyle {
fn default() -> Self {
TextStyleBuilder::new().build()
}
}
#[derive(Debug, Clone, Copy, PartialEq, PartialOrd)]
pub struct TextStyleBuilder {
style: TextStyle,
}
impl TextStyleBuilder {
pub const fn new() -> Self {
Self {
style: TextStyle {
alignment: Alignment::Left,
baseline: Baseline::Alphabetic,
line_height: LineHeight::Percent(100),
},
}
}
}
impl TextStyleBuilder {
pub const fn alignment(mut self, alignment: Alignment) -> Self {
self.style.alignment = alignment;
self
}
pub const fn baseline(mut self, baseline: Baseline) -> Self {
self.style.baseline = baseline;
self
}
pub const fn line_height(mut self, line_height: LineHeight) -> Self {
self.style.line_height = line_height;
self
}
pub const fn build(self) -> TextStyle {
self.style
}
}
impl From<&TextStyle> for TextStyleBuilder {
fn from(style: &TextStyle) -> Self {
Self { style: *style }
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn builder() {
let text_style = TextStyleBuilder::new()
.alignment(Alignment::Right)
.baseline(Baseline::Top)
.line_height(LineHeight::Pixels(123))
.build();
assert_eq!(text_style.alignment, Alignment::Right);
assert_eq!(text_style.baseline, Baseline::Top);
assert_eq!(text_style.line_height, LineHeight::Pixels(123));
}
#[test]
fn builder_default() {
let text_style = TextStyleBuilder::new().build();
assert_eq!(text_style.alignment, Alignment::Left);
assert_eq!(text_style.baseline, Baseline::Alphabetic);
assert_eq!(text_style.line_height, LineHeight::Percent(100));
}
}