umya_spreadsheet/structs/
range.rs

1use super::ColumnReference;
2use super::RowReference;
3use crate::helper::coordinate::*;
4use crate::traits::AdjustmentCoordinate;
5use crate::traits::AdjustmentValue;
6
7#[derive(Clone, Default, Debug)]
8pub struct Range {
9    start_col: Option<ColumnReference>,
10    start_row: Option<RowReference>,
11    end_col: Option<ColumnReference>,
12    end_row: Option<RowReference>,
13}
14
15impl Range {
16    pub fn set_range<S: Into<String>>(&mut self, value: S) -> &mut Self {
17        let org_value = value.into();
18        let coordinate_collection: Vec<&str> = org_value.split(':').collect();
19
20        assert!(
21            matches!(coordinate_collection.len(), 1 | 2),
22            "Non-standard coordinate"
23        );
24
25        let (
26            row,         //
27            col,         //
28            is_lock_col, //
29            is_lock_row,
30        ) = index_from_coordinate(coordinate_collection[0]);
31        if let Some(v) = row {
32            let mut coordinate_start_col = ColumnReference::default();
33            coordinate_start_col.set_num(v);
34            coordinate_start_col.set_is_lock(is_lock_col.unwrap());
35            self.start_col = Some(coordinate_start_col);
36        };
37        if let Some(v) = col {
38            let mut coordinate_start_row = RowReference::default();
39            coordinate_start_row.set_num(v);
40            coordinate_start_row.set_is_lock(is_lock_row.unwrap());
41            self.start_row = Some(coordinate_start_row);
42        }
43
44        if coordinate_collection.len() == 2 {
45            let (
46                row,         //
47                col,         //
48                is_lock_col, //
49                is_lock_row,
50            ) = index_from_coordinate(coordinate_collection[1]);
51            if let Some(v) = row {
52                let mut coordinate_end_col = ColumnReference::default();
53                coordinate_end_col.set_num(v);
54                coordinate_end_col.set_is_lock(is_lock_col.unwrap());
55                self.end_col = Some(coordinate_end_col);
56            };
57            if let Some(v) = col {
58                let mut coordinate_end_row = RowReference::default();
59                coordinate_end_row.set_num(v);
60                coordinate_end_row.set_is_lock(is_lock_row.unwrap());
61                self.end_row = Some(coordinate_end_row);
62            }
63        }
64        self
65    }
66
67    #[inline]
68    pub fn get_range(&self) -> String {
69        let mut result = self.get_coordinate_start();
70        if self.end_col.is_some() || self.end_row.is_some() {
71            result = format!("{}:{}", result, &self.get_coordinate_end());
72        }
73        result
74    }
75
76    #[inline]
77    pub fn get_coordinate_start_col(&self) -> Option<&ColumnReference> {
78        self.start_col.as_ref()
79    }
80
81    #[inline]
82    pub fn get_coordinate_start_col_mut(&mut self) -> Option<&mut ColumnReference> {
83        self.start_col.as_mut()
84    }
85
86    #[inline]
87    pub fn get_coordinate_start_row(&self) -> Option<&RowReference> {
88        self.start_row.as_ref()
89    }
90
91    #[inline]
92    pub fn get_coordinate_start_row_mut(&mut self) -> Option<&mut RowReference> {
93        self.start_row.as_mut()
94    }
95
96    #[inline]
97    pub fn get_coordinate_end_col(&self) -> Option<&ColumnReference> {
98        self.end_col.as_ref()
99    }
100
101    #[inline]
102    pub fn get_coordinate_end_col_mut(&mut self) -> Option<&mut ColumnReference> {
103        self.end_col.as_mut()
104    }
105
106    #[inline]
107    pub fn get_coordinate_end_row(&self) -> Option<&RowReference> {
108        self.end_row.as_ref()
109    }
110
111    #[inline]
112    pub fn get_coordinate_end_row_mut(&mut self) -> Option<&mut RowReference> {
113        self.end_row.as_mut()
114    }
115
116    pub(crate) fn get_coordinate_start(&self) -> String {
117        let mut coordinate_str = "".into();
118        if let Some(v) = &self.start_col {
119            coordinate_str = v.get_coordinate();
120        };
121        if let Some(v) = &self.start_row {
122            coordinate_str = format!("{}{}", coordinate_str, v.get_coordinate());
123        };
124        coordinate_str
125    }
126
127    pub(crate) fn get_coordinate_end(&self) -> String {
128        let mut coordinate_str = "".into();
129        if let Some(v) = &self.end_col {
130            coordinate_str = v.get_coordinate();
131        };
132        if let Some(v) = &self.end_row {
133            coordinate_str = format!("{}{}", coordinate_str, v.get_coordinate());
134        };
135        coordinate_str
136    }
137}
138impl AdjustmentCoordinate for Range {
139    #[inline]
140    fn adjustment_insert_coordinate(
141        &mut self,
142        root_col_num: &u32,
143        offset_col_num: &u32,
144        root_row_num: &u32,
145        offset_row_num: &u32,
146    ) {
147        if let Some(v) = &mut self.start_col {
148            v.adjustment_insert_value(root_col_num, offset_col_num);
149        }
150        if let Some(v) = &mut self.start_row {
151            v.adjustment_insert_value(root_row_num, offset_row_num);
152        }
153        if let Some(v) = &mut self.end_col {
154            v.adjustment_insert_value(root_col_num, offset_col_num);
155        }
156        if let Some(v) = &mut self.end_row {
157            v.adjustment_insert_value(root_row_num, offset_row_num);
158        }
159    }
160
161    #[inline]
162    fn adjustment_remove_coordinate(
163        &mut self,
164        root_col_num: &u32,
165        offset_col_num: &u32,
166        root_row_num: &u32,
167        offset_row_num: &u32,
168    ) {
169        if let Some(v) = &mut self.start_col {
170            v.adjustment_remove_value(root_col_num, offset_col_num);
171        }
172        if let Some(v) = &mut self.start_row {
173            v.adjustment_remove_value(root_row_num, offset_row_num);
174        }
175        if let Some(v) = &mut self.end_col {
176            v.adjustment_remove_value(root_col_num, offset_col_num);
177        }
178        if let Some(v) = &mut self.end_row {
179            v.adjustment_remove_value(root_row_num, offset_row_num);
180        }
181    }
182
183    #[inline]
184    fn is_remove_coordinate(
185        &self,
186        root_col_num: &u32,
187        offset_col_num: &u32,
188        root_row_num: &u32,
189        offset_row_num: &u32,
190    ) -> bool {
191        let start_col_result = match &self.start_col {
192            Some(v) => v.is_remove_value(root_col_num, offset_col_num),
193            None => false,
194        };
195        let start_row_result = match &self.start_row {
196            Some(v) => v.is_remove_value(root_row_num, offset_row_num),
197            None => false,
198        };
199        let end_col_result = match &self.end_col {
200            Some(v) => v.is_remove_value(root_col_num, offset_col_num),
201            None => false,
202        };
203        let end_row_result = match &self.end_row {
204            Some(v) => v.is_remove_value(root_row_num, offset_row_num),
205            None => false,
206        };
207        start_col_result && start_row_result && end_col_result && end_row_result
208    }
209}