appcore-filemaker 0.1.0-beta.1

Deterministic declarative document, canvas, and dataset compiler for AppCore
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
// =============================================================================
//        #######
//     ###       ###     F: style.rs
//    ##   ## ##   ##    P: AppCore-Runtime
//         ## ##
//                       C: 2026/08/30 05:00:00 by dnettoRaw
//    ##   ## ##   ##    U: 2026/08/30 05:00:00 by dnettoRaw
//      ###########      S: 1.0.2-rc
// =============================================================================

//! Defines bounded style contracts and behavior for this crate.

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use crate::{ErrorCode, FileMakerError, Result, Unit};

/// Format-neutral color retained until export.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(tag = "space", rename_all = "snake_case")]
pub enum Color {
    /// Eight-bit RGB.
    Rgb {
        /// Red channel.
        r: u8,
        /// Green channel.
        g: u8,
        /// Blue channel.
        b: u8,
    },
    /// Eight-bit RGB with alpha.
    Rgba {
        /// Red channel.
        r: u8,
        /// Green channel.
        g: u8,
        /// Blue channel.
        b: u8,
        /// Alpha channel.
        a: u8,
    },
    /// Eight-bit grayscale.
    Gray {
        /// Gray channel.
        value: u8,
    },
    /// CMYK channels in millionths.
    Cmyk {
        /// Cyan.
        c: u32,
        /// Magenta.
        m: u32,
        /// Yellow.
        y: u32,
        /// Black.
        k: u32,
    },
}

impl Color {
    /// Parses hex, stable named colors, and integer `rgb`, `rgba`, `gray`, or
    /// millionth-channel `cmyk` functions.
    pub fn parse(source: &str) -> Result<Self> {
        match source {
            "black" => return Ok(Self::Rgb { r: 0, g: 0, b: 0 }),
            "white" => {
                return Ok(Self::Rgb {
                    r: 255,
                    g: 255,
                    b: 255,
                })
            }
            "red" => return Ok(Self::Rgb { r: 255, g: 0, b: 0 }),
            "green" => return Ok(Self::Rgb { r: 0, g: 128, b: 0 }),
            "blue" => return Ok(Self::Rgb { r: 0, g: 0, b: 255 }),
            "transparent" => {
                return Ok(Self::Rgba {
                    r: 0,
                    g: 0,
                    b: 0,
                    a: 0,
                })
            }
            _ => {
                if source.ends_with(')') {
                    return parse_function_color(source);
                }
            }
        }
        let hex = source
            .strip_prefix('#')
            .ok_or_else(|| style_error("invalid color syntax"))?;
        match hex.len() {
            3 => Ok(Self::Rgb {
                r: duplicate_nibble(hex, 0)?,
                g: duplicate_nibble(hex, 1)?,
                b: duplicate_nibble(hex, 2)?,
            }),
            6 => Ok(Self::Rgb {
                r: hex_byte(hex, 0)?,
                g: hex_byte(hex, 2)?,
                b: hex_byte(hex, 4)?,
            }),
            8 => Ok(Self::Rgba {
                r: hex_byte(hex, 0)?,
                g: hex_byte(hex, 2)?,
                b: hex_byte(hex, 4)?,
                a: hex_byte(hex, 6)?,
            }),
            _ => Err(style_error("hex color requires 3, 6, or 8 digits")),
        }
    }

    /// Validates channel ranges not enforced by their storage type.
    pub fn validate(self) -> Result<()> {
        if let Self::Cmyk { c, m, y, k } = self {
            if [c, m, y, k].iter().any(|channel| *channel > 1_000_000) {
                return Err(style_error("CMYK channels must be at most 1000000"));
            }
        }
        Ok(())
    }

    /// Converts to RGBA for exporters that do not preserve CMYK.
    #[must_use]
    pub fn to_rgba(self) -> [u8; 4] {
        match self {
            Self::Rgb { r, g, b } => [r, g, b, 255],
            Self::Rgba { r, g, b, a } => [r, g, b, a],
            Self::Gray { value } => [value, value, value, 255],
            Self::Cmyk { c, m, y, k } => {
                let convert = |channel: u32| {
                    let combined = channel.saturating_add(k).min(1_000_000);
                    ((1_000_000 - combined) * 255 / 1_000_000) as u8
                };
                [convert(c), convert(m), convert(y), 255]
            }
        }
    }
}

/// Partial style layer.
#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)]
pub struct Style {
    /// Fill.
    pub fill: Option<Color>,
    /// Stroke.
    pub stroke: Option<Color>,
    /// Stroke width.
    pub stroke_width: Option<Unit>,
    /// Opacity in millionths.
    pub opacity: Option<u32>,
    /// Explicit font asset name.
    pub font: Option<String>,
    /// Font size.
    pub font_size: Option<Unit>,
    /// Text foreground.
    pub color: Option<Color>,
}

/// Conditional partial style retained until typed data binding.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ElementStyleRule {
    /// Deterministic boolean expression.
    pub when: String,
    /// Partial style applied when the expression is truthy.
    pub style: Style,
}

impl Style {
    /// Validates field ranges.
    pub fn validate(&self) -> Result<()> {
        for color in [self.fill, self.stroke, self.color].into_iter().flatten() {
            color.validate()?;
        }
        if self.opacity.is_some_and(|value| value > 1_000_000)
            || self.stroke_width.is_some_and(|value| value < Unit::ZERO)
            || self.font_size.is_some_and(|value| value <= Unit::ZERO)
        {
            return Err(style_error("style range is invalid"));
        }
        Ok(())
    }

    pub(crate) fn overlay(&mut self, next: &Self) {
        if next.fill.is_some() {
            self.fill = next.fill;
        }
        if next.stroke.is_some() {
            self.stroke = next.stroke;
        }
        if next.stroke_width.is_some() {
            self.stroke_width = next.stroke_width;
        }
        if next.opacity.is_some() {
            self.opacity = next.opacity;
        }
        if next.font.is_some() {
            self.font.clone_from(&next.font);
        }
        if next.font_size.is_some() {
            self.font_size = next.font_size;
        }
        if next.color.is_some() {
            self.color = next.color;
        }
    }
}

fn parse_function_color(source: &str) -> Result<Color> {
    let (name, values) = source
        .split_once('(')
        .ok_or_else(|| style_error("invalid functional color syntax"))?;
    let values = values
        .strip_suffix(')')
        .ok_or_else(|| style_error("invalid functional color syntax"))?
        .split(',')
        .map(|value| {
            value
                .trim()
                .parse::<u32>()
                .map_err(|_| style_error("functional color channels must be unsigned integers"))
        })
        .collect::<Result<Vec<_>>>()?;
    match (name, values.as_slice()) {
        ("rgb", [r, g, b]) => Ok(Color::Rgb {
            r: channel_u8(*r)?,
            g: channel_u8(*g)?,
            b: channel_u8(*b)?,
        }),
        ("rgba", [r, g, b, a]) => Ok(Color::Rgba {
            r: channel_u8(*r)?,
            g: channel_u8(*g)?,
            b: channel_u8(*b)?,
            a: channel_u8(*a)?,
        }),
        ("gray", [value]) => Ok(Color::Gray {
            value: channel_u8(*value)?,
        }),
        ("cmyk", [c, m, y, k]) if [c, m, y, k].iter().all(|value| **value <= 1_000_000) => {
            Ok(Color::Cmyk {
                c: *c,
                m: *m,
                y: *y,
                k: *k,
            })
        }
        ("cmyk", _) => Err(style_error("CMYK requires four channels up to 1000000")),
        _ => Err(style_error("unsupported functional color syntax")),
    }
}

fn channel_u8(value: u32) -> Result<u8> {
    u8::try_from(value).map_err(|_| style_error("RGB/Gray channels must be at most 255"))
}

/// Fully resolved style.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct ComputedStyle {
    /// Fill.
    pub fill: Option<Color>,
    /// Stroke.
    pub stroke: Option<Color>,
    /// Stroke width.
    pub stroke_width: Unit,
    /// Opacity in millionths.
    pub opacity: u32,
    /// Explicit font asset name.
    pub font: Option<String>,
    /// Font size.
    pub font_size: Unit,
    /// Text foreground.
    pub color: Color,
}

impl ComputedStyle {
    /// Validates the resolved style before it crosses an exporter boundary.
    pub fn validate(&self) -> Result<()> {
        for color in [self.fill, self.stroke, Some(self.color)]
            .into_iter()
            .flatten()
        {
            color.validate()?;
        }
        if self.opacity > 1_000_000
            || self.stroke_width < Unit::ZERO
            || self.font_size <= Unit::ZERO
            || self
                .font
                .as_ref()
                .is_some_and(|font| font.is_empty() || font.len() > 128)
        {
            return Err(style_error("computed style range is invalid"));
        }
        Ok(())
    }
}

/// Named layers for the normative style cascade.
#[derive(Clone, Debug, Default)]
pub struct StyleCascade {
    /// Engine defaults.
    pub defaults: Style,
    /// Active theme.
    pub theme: Style,
    /// Template-level style.
    pub template: Style,
    /// Component layer.
    pub component: Style,
    /// Data-rule layer.
    pub data_rule: Style,
    /// Runtime override layer.
    pub runtime: Style,
    /// Export-specific override layer.
    pub export: Style,
}

impl StyleCascade {
    /// Computes defaults → theme → template → component → data → runtime → export.
    pub fn compute(&self) -> Result<ComputedStyle> {
        let mut merged = Style::default();
        for layer in [
            &self.defaults,
            &self.theme,
            &self.template,
            &self.component,
            &self.data_rule,
            &self.runtime,
            &self.export,
        ] {
            layer.validate()?;
            merged.overlay(layer);
        }
        Ok(ComputedStyle {
            fill: merged.fill,
            stroke: merged.stroke,
            stroke_width: merged.stroke_width.unwrap_or(Unit::ZERO),
            opacity: merged.opacity.unwrap_or(1_000_000),
            font: merged.font,
            font_size: merged.font_size.unwrap_or(Unit::points(12)?),
            color: merged.color.unwrap_or(Color::Rgb { r: 0, g: 0, b: 0 }),
        })
    }
}

/// Resolves `$token` references with bounded parent traversal.
pub fn resolve_token(
    token: &str,
    themes: &BTreeMap<String, BTreeMap<String, String>>,
    active_theme: &str,
    max_depth: usize,
) -> Result<String> {
    let key = token
        .strip_prefix('$')
        .ok_or_else(|| style_error("token must begin with `$`"))?;
    let mut current = active_theme;
    for _ in 0..max_depth {
        let theme = themes
            .get(current)
            .ok_or_else(|| style_error("theme was not found"))?;
        if let Some(value) = theme.get(key) {
            return Ok(value.clone());
        }
        current = theme.get("$extends").map_or("", String::as_str);
        if current.is_empty() {
            break;
        }
    }
    Err(style_error(format!("token `{token}` was not resolved")))
}

fn duplicate_nibble(hex: &str, offset: usize) -> Result<u8> {
    let value = u8::from_str_radix(&hex[offset..=offset], 16)
        .map_err(|_| style_error("invalid hex color digit"))?;
    Ok(value * 17)
}

fn hex_byte(hex: &str, offset: usize) -> Result<u8> {
    u8::from_str_radix(&hex[offset..offset + 2], 16)
        .map_err(|_| style_error("invalid hex color digit"))
}

fn style_error(message: impl Into<String>) -> FileMakerError {
    FileMakerError::new(ErrorCode::SchemaField, message)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn runtime_layer_overrides_template_layer() {
        let cascade = StyleCascade {
            template: Style {
                fill: Some(Color::parse("#ff0000").unwrap()),
                ..Style::default()
            },
            runtime: Style {
                fill: Some(Color::parse("blue").unwrap()),
                ..Style::default()
            },
            ..StyleCascade::default()
        };
        assert_eq!(
            cascade.compute().unwrap().fill,
            Some(Color::Rgb { r: 0, g: 0, b: 255 })
        );
    }
}