logisheets_controller/file_loader2/
styles.rs

1use crate::style_manager::{xf_manager::CtrlXf, StyleManager};
2use logisheets_base::StyleId;
3use logisheets_workbook::prelude::*;
4use std::collections::HashMap;
5
6#[allow(dead_code)]
7pub struct StyleLoader<'a> {
8    manager: &'a mut StyleManager,
9    part: &'a StylesheetPart,
10    xf_cache: HashMap<u32, StyleId>,
11    cell_xf_cache: HashMap<u32, StyleId>,
12}
13
14impl<'a> StyleLoader<'a> {
15    pub fn new(manager: &'a mut StyleManager, part: &'a StylesheetPart) -> Self {
16        StyleLoader {
17            manager,
18            part,
19            xf_cache: HashMap::new(),
20            cell_xf_cache: HashMap::new(),
21        }
22    }
23
24    pub fn load_xf(&mut self, idx: u32) -> StyleId {
25        if let Some(cache) = self.cell_xf_cache.get(&idx) {
26            return cache.clone();
27        }
28        let style_id = {
29            if self.part.cell_xfs.is_none() {
30                return 0;
31            }
32            let xf = self.part.cell_xfs.as_ref().unwrap().xfs.get(idx as usize);
33            if xf.is_none() {
34                return 0;
35            }
36            let xf = xf.unwrap();
37            let font_id = if let Some(i) = xf.font_id {
38                if let Some(fonts) = &self.part.fonts {
39                    if let Some(f) = fonts.fonts.get(i as usize) {
40                        self.manager.font_manager.get_id(f)
41                    } else {
42                        0
43                    }
44                } else {
45                    0
46                }
47            } else {
48                0
49            };
50            let fill_id = if let Some(i) = xf.fill_id {
51                if let Some(fills) = &self.part.fills {
52                    if let Some(f) = fills.fills.get(i as usize) {
53                        self.manager.fill_manager.get_id(f)
54                    } else {
55                        0
56                    }
57                } else {
58                    0
59                }
60            } else {
61                0
62            };
63            let num_fmt_id = if let Some(i) = xf.num_fmt_id {
64                if let Some(num_fmts) = &self.part.num_fmts {
65                    let pos = num_fmts.num_fmts.iter().position(|fmt| fmt.num_fmt_id == i);
66                    if let Some(p) = pos {
67                        let f = num_fmts.num_fmts.get(p).unwrap();
68                        self.manager.num_fmt_manager.get_id(&f.format_code)
69                    } else {
70                        i
71                    }
72                } else {
73                    i
74                }
75            } else {
76                0
77            };
78            let border_id = if let Some(i) = xf.border_id {
79                if let Some(borders) = &self.part.borders {
80                    if let Some(f) = borders.borders.get(i as usize) {
81                        self.manager.border_manager.get_id(f)
82                    } else {
83                        0
84                    }
85                } else {
86                    0
87                }
88            } else {
89                0
90            };
91            let ctrl_xf = CtrlXf {
92                alignment: xf.alignment.clone(),
93                protection: xf.protction.clone(),
94                font_id: Some(font_id),
95                border_id: Some(border_id),
96                fill_id: Some(fill_id),
97                num_fmt_id: Some(num_fmt_id),
98                apply_number_format: xf.apply_number_format,
99                apply_font: xf.apply_font,
100                apply_fill: xf.apply_fill,
101                apply_border: xf.apply_border,
102                apply_alignment: xf.apply_alignment,
103                apply_protection: xf.apply_protection,
104            };
105            self.manager.cell_xfs_manager.get_id(&ctrl_xf)
106        };
107        self.cell_xf_cache.insert(idx, style_id);
108        style_id
109    }
110}