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
//! Optical surface linking structure.

use crate::{
    err::Error,
    geom::{Mesh, Surface},
    ord::{Link, Set},
};

/// Optical surface.
pub struct SurfaceLinker {
    /// Mesh.
    mesh: Mesh,
    /// Attribute name.
    attr: String,
}

impl SurfaceLinker {
    /// Construct a new instance.
    #[inline]
    #[must_use]
    pub const fn new(mesh: Mesh, attr: String) -> Self {
        Self { mesh, attr }
    }
}

impl<'a, T: 'a> Link<'a, T> for SurfaceLinker {
    type Inst = Surface<'a, T>;

    #[inline]
    fn requires(&self) -> Vec<String> {
        vec![self.attr.clone()]
    }

    #[inline]
    fn link(self, attrs: &'a Set<T>) -> Result<Self::Inst, Error> {
        let attr = self.attr;
        Ok(Surface::new(
            self.mesh,
            attrs
                .get(&attr)
                .unwrap_or_else(|| panic!("Failed to link surface-attribute key: {}", attr)),
        ))
    }
}