lichtung_plot/theme/
value.rs1use crate::font::canonical::FontStack;
4use crate::Result;
5
6use super::{
7 AnnotationTheme, AxisTheme, ColorScales, CompositionTheme, LegendTheme, MarkTheme,
8 ResolvedTheme, SurfaceTheme, TypographyTheme,
9};
10
11#[derive(Clone, Debug, PartialEq)]
38pub struct Theme {
39 pub(crate) surfaces: SurfaceTheme,
40 pub(crate) typography: TypographyTheme,
41 pub(crate) axes: AxisTheme,
42 pub(crate) legend: LegendTheme,
43 pub(crate) composition: CompositionTheme,
44 pub(crate) marks: MarkTheme,
45 pub(crate) annotations: AnnotationTheme,
46 pub(crate) color_scales: ColorScales,
47}
48
49#[derive(Clone, Debug)]
51pub struct ThemeBuilder {
52 value: Theme,
53}
54
55macro_rules! components {
56 ($($field:ident: $ty:ty => $get:ident),+ $(,)?) => {
57 impl Theme {
58 $(
59 #[doc = concat!("Borrow the complete `", stringify!($field), "` component.")]
60 pub fn $field(&self) -> &$ty { &self.$field }
61 )+
62 }
63 impl ThemeBuilder {
64 $(
65 #[doc = concat!("Borrow the current complete `", stringify!($field), "` component.")]
66 pub fn $get(&self) -> &$ty { self.value.$field() }
67 #[doc = concat!("Replace the complete `", stringify!($field), "` construction input.")]
68 #[must_use]
69 pub fn $field(mut self, value: $ty) -> Self {
70 self.value.$field = value;
71 self
72 }
73 )+
74 }
75 };
76}
77
78components!(surfaces: SurfaceTheme => get_surfaces,
79 typography: TypographyTheme => get_typography, axes: AxisTheme => get_axes,
80 legend: LegendTheme => get_legend, composition: CompositionTheme => get_composition,
81 marks: MarkTheme => get_marks, annotations: AnnotationTheme => get_annotations,
82 color_scales: ColorScales => get_color_scales);
83
84impl Theme {
85 #[expect(
86 clippy::too_many_arguments,
87 reason = "the complete constructor retains eight independently owned components"
88 )]
89 pub fn new(
91 surfaces: SurfaceTheme,
92 typography: TypographyTheme,
93 axes: AxisTheme,
94 legend: LegendTheme,
95 composition: CompositionTheme,
96 marks: MarkTheme,
97 annotations: AnnotationTheme,
98 color_scales: ColorScales,
99 ) -> Result<Self> {
100 let value = Self {
101 surfaces,
102 typography,
103 axes,
104 legend,
105 composition,
106 marks,
107 annotations,
108 color_scales,
109 };
110 value.validate()?;
111 Ok(value)
112 }
113
114 pub(crate) fn validate(&self) -> Result<()> {
115 self.typography().validate()?;
116 self.axes().validate()?;
117 self.legend().validate_in()?;
118 self.marks().validate()?;
119 self.annotations().validate()
120 }
121
122 pub fn publication() -> Self {
124 super::resolved::publication_theme().expect("validated publication constructor")
125 }
126
127 pub fn nature() -> Self {
129 super::resolved::nature_theme().expect("validated nature constructor")
130 }
131
132 pub fn science() -> Self {
134 super::resolved::science_theme().expect("validated science constructor")
135 }
136
137 pub fn chi() -> Self {
139 super::resolved::chi_theme().expect("validated CHI constructor")
140 }
141
142 pub(crate) fn resolve(&self) -> Result<ResolvedTheme> {
143 ResolvedTheme::new(self.clone())
144 }
145
146 pub fn customize(&self) -> ThemeBuilder {
148 ThemeBuilder::new(self.clone())
149 }
150}
151
152impl ThemeBuilder {
153 pub fn new(value: Theme) -> Self {
155 Self { value }
156 }
157
158 pub fn font_stack(mut self, stack: FontStack) -> Result<Self> {
160 self.value.typography = self.value.typography.with_font_stack(stack)?;
161 Ok(self)
162 }
163
164 pub fn build(self) -> Result<Theme> {
166 self.value.validate()?;
167 Ok(self.value)
168 }
169}