string_art/
line_config.rs

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
use std::ops::{Deref, DerefMut};

use serde::{Deserialize, Serialize};

use crate::{
    line_selector::{self, LineItemSelector, LineSelector}, verboser::Verboser, AsLab, Image
};
    
#[derive(Clone, Copy, Serialize, Deserialize)]
pub struct LineItemConfig {
    pub color_idx: usize,
    pub cap: usize,
}

impl LineItemConfig {
    pub fn new(color_idx: usize, cap: usize) -> Self {
        LineItemConfig { color_idx, cap }
    }
}

#[derive(Copy, Clone, Serialize, Deserialize)]
pub struct LineGroupConfig<C = Vec<LineItemConfig>>(C);

impl<C> LineGroupConfig<C> {
    pub fn new(items: C) -> Self {
        LineGroupConfig(items)
    }
}   
impl<C> Deref for LineGroupConfig<C>{
    type Target = C;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<C> DerefMut for LineGroupConfig<C>{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

#[derive(Copy, Clone, Serialize, Deserialize)]
pub struct LineConfig<G = Vec<LineGroupConfig>, C = Vec<LineItemConfig>> {
    groups: G,
    _panthom: std::marker::PhantomData<C>,
}

impl<G, C> LineConfig<G, C>{
    pub fn new(groups: G) -> Self{
        Self{
            groups,
            _panthom: std::marker::PhantomData,
        }
    }
}

impl<G, C> Deref for LineConfig<G, C>{
    type Target = G;

    fn deref(&self) -> &Self::Target {
        &self.groups
    }
}

impl<G, C> DerefMut for LineConfig<G, C>{
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.groups
    }
}

impl From<&LineSelector> for LineConfig{
    fn from(value: &LineSelector) -> Self {
        LineConfig::new(value.iter().map(|group| {
            LineGroupConfig::new(group.iter().map(|item| {
                LineItemConfig::new(item.color_idx(), item.cap())
            }).collect())
        }).collect())   
    }
}

unsafe impl<S, G, C> line_selector::Builder<S> for LineConfig<G, C>
where
    G: AsRef<[LineGroupConfig<C>]>,
    C: AsRef<[LineItemConfig]>,
{
    fn build_line_selector(
        &self,
        _: &Image<S>,
        palette: &[impl AsLab<S>],
        _: &mut impl Verboser,
    ) -> Result<LineSelector, line_selector::Error> {
        self.groups
            .as_ref()
            .iter()
            .map(|group| {
                group
                    .0
                    .as_ref()
                    .iter()
                    .map(|item| {
                        if item.color_idx >= palette.len() {
                            Err(line_selector::Error)
                        } else {
                            Ok(LineItemSelector::new(item.color_idx, 0, item.cap))
                        }
                    })
                    .collect()
            })
            .collect()
    }
}

impl From<LineItemConfig> for LineItemSelector {
    fn from(value: LineItemConfig) -> Self {
        LineItemSelector::new(value.color_idx, 0, value.cap)
    }
}