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
//! Attribute builder.

use palette::{Gradient, LinSrgba};
use serde::Deserialize;
use std::collections::HashMap;

use crate::render::Attribute;

/// Attribute builder.
#[derive(Deserialize)]
#[non_exhaustive]
pub enum AttributeBuilder {
    /// Opaque coloured surface.
    Opaque(String),
    /// Partially reflective mirror, absorption fraction.
    Mirror(String, f64),
    /// Partially transparent, absorption fraction.
    Transparent(String, f64),
    /// Refractive, absorption fraction, inside and outside refractive indices.
    Refractive(String, f64, [f64; 2]),
    /// Luminous surface, brightness multiplier.
    Luminous(String, f64),
    /// Switchable condition, conditional value.
    Switchable([String; 2], f64),
}

impl AttributeBuilder {
    /// Get the names of the `Gradient`s used.
    #[inline]
    #[must_use]
    pub fn used_gradient_names(&self) -> Vec<String> {
        match *self {
            Self::Opaque(ref grad)
            | Self::Mirror(ref grad, ..)
            | Self::Transparent(ref grad, ..)
            | Self::Refractive(ref grad, ..)
            | Self::Luminous(ref grad, ..) => vec![grad.clone()],
            Self::Switchable(ref grads, ..) => grads.clone().to_vec(),
        }
    }
}

impl<'a> AttributeBuilder {
    /// Build the `Attribute`.
    #[inline]
    #[must_use]
    pub fn build(self, grads: &'a HashMap<String, Gradient<LinSrgba>>) -> Attribute<'a> {
        match self {
            Self::Opaque(ref grad) => Attribute::Opaque(
                grads
                    .get(grad)
                    .unwrap_or_else(|| panic!("Failed to link attribute-gradient key: {}", grad)),
            ),
            Self::Mirror(ref grad, abs_frac) => Attribute::Mirror(
                grads
                    .get(grad)
                    .unwrap_or_else(|| panic!("Failed to link attribute-gradient key: {}", grad)),
                abs_frac,
            ),
            Self::Transparent(ref grad, abs_frac) => Attribute::Transparent(
                grads
                    .get(grad)
                    .unwrap_or_else(|| panic!("Failed to link attribute-gradient key: {}", grad)),
                abs_frac,
            ),
            Self::Refractive(ref grad, abs_frac, ref_indices) => Attribute::Refractive(
                grads
                    .get(grad)
                    .unwrap_or_else(|| panic!("Failed to link attribute-gradient key: {}", grad)),
                abs_frac,
                ref_indices,
            ),
            Self::Luminous(ref grad, bright_mult) => Attribute::Luminous(
                grads
                    .get(grad)
                    .unwrap_or_else(|| panic!("Failed to link attribute-gradient key: {}", grad)),
                bright_mult,
            ),
            Self::Switchable([ref grad_a, ref grad_b], x) => Attribute::Switchable(
                [
                    grads.get(grad_a).unwrap_or_else(|| {
                        panic!("Failed to link attribute-gradient key: {}", grad_a)
                    }),
                    grads.get(grad_b).unwrap_or_else(|| {
                        panic!("Failed to link attribute-gradient key: {}", grad_b)
                    }),
                ],
                x,
            ),
        }
    }
}