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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
use super::ColumnReference;
use super::RowReference;
use helper::coordinate::*;

#[derive(Clone, Default, Debug)]
pub struct Range {
    coordinate_start_col: Option<ColumnReference>,
    coordinate_start_row: Option<RowReference>,
    coordinate_end_col: Option<ColumnReference>,
    coordinate_end_row: Option<RowReference>,
}
impl Range {
    pub fn set_range<S: Into<String>>(&mut self, value: S) -> &mut Self {
        let org_value = value.into();
        let coordinate_collection: Vec<&str> = org_value.split(':').collect();

        if coordinate_collection.is_empty() || coordinate_collection.len() > 2 {
            panic!("Non-standard coordinate");
        }

        if coordinate_collection.len() == 1 || coordinate_collection.len() == 2 {
            let coordinate_str = coordinate_collection[0].to_string();
            let (
                row,         //
                col,         //
                is_lock_col, //
                is_lock_row,
            ) = index_from_coordinate(coordinate_str);
            match row {
                Some(v) => {
                    let mut coordinate_start_col = ColumnReference::default();
                    coordinate_start_col.set_num(v);
                    coordinate_start_col.set_is_lock(is_lock_col.unwrap());
                    self.coordinate_start_col = Some(coordinate_start_col);
                }
                None => {}
            };
            match col {
                Some(v) => {
                    let mut coordinate_start_row = RowReference::default();
                    coordinate_start_row.set_num(v);
                    coordinate_start_row.set_is_lock(is_lock_row.unwrap());
                    self.coordinate_start_row = Some(coordinate_start_row);
                }
                None => {}
            }
        }

        if coordinate_collection.len() == 2 {
            let coordinate_str = coordinate_collection[1].to_string();
            let (
                row,         //
                col,         //
                is_lock_col, //
                is_lock_row,
            ) = index_from_coordinate(coordinate_str);
            match row {
                Some(v) => {
                    let mut coordinate_end_col = ColumnReference::default();
                    coordinate_end_col.set_num(v);
                    coordinate_end_col.set_is_lock(is_lock_col.unwrap());
                    self.coordinate_end_col = Some(coordinate_end_col);
                }
                None => {}
            };
            match col {
                Some(v) => {
                    let mut coordinate_end_row = RowReference::default();
                    coordinate_end_row.set_num(v);
                    coordinate_end_row.set_is_lock(is_lock_row.unwrap());
                    self.coordinate_end_row = Some(coordinate_end_row);
                }
                None => {}
            }
        }
        self
    }

    pub fn get_range(&self) -> String {
        let mut result = self.get_coordinate_start();
        if self.coordinate_end_col.is_some() || self.coordinate_end_row.is_some() {
            result = format!("{}:{}", result, &self.get_coordinate_end());
        }
        result
    }

    pub fn get_coordinate_start_col(&self) -> &Option<ColumnReference> {
        &self.coordinate_start_col
    }

    pub fn get_coordinate_start_col_mut(&mut self) -> &mut Option<ColumnReference> {
        &mut self.coordinate_start_col
    }

    pub fn get_coordinate_start_row(&self) -> &Option<RowReference> {
        &self.coordinate_start_row
    }

    pub fn get_coordinate_start_row_mut(&mut self) -> &mut Option<RowReference> {
        &mut self.coordinate_start_row
    }

    pub fn get_coordinate_end_col(&self) -> &Option<ColumnReference> {
        &self.coordinate_end_col
    }

    pub fn get_coordinate_end_col_mut(&mut self) -> &mut Option<ColumnReference> {
        &mut self.coordinate_end_col
    }

    pub fn get_coordinate_end_row(&self) -> &Option<RowReference> {
        &self.coordinate_end_row
    }

    pub fn get_coordinate_end_row_mut(&mut self) -> &mut Option<RowReference> {
        &mut self.coordinate_end_row
    }

    pub(crate) fn get_coordinate_start(&self) -> String {
        let mut coordinate_str = "".into();
        match &self.coordinate_start_col {
            Some(v) => {
                coordinate_str = v.get_coordinate();
            }
            None => {}
        };
        match &self.coordinate_start_row {
            Some(v) => {
                coordinate_str = format!("{}{}", coordinate_str, v.get_coordinate());
            }
            None => {}
        };
        coordinate_str
    }

    pub(crate) fn get_coordinate_end(&self) -> String {
        let mut coordinate_str = "".into();
        match &self.coordinate_end_col {
            Some(v) => {
                coordinate_str = v.get_coordinate();
            }
            None => {}
        };
        match &self.coordinate_end_row {
            Some(v) => {
                coordinate_str = format!("{}{}", coordinate_str, v.get_coordinate());
            }
            None => {}
        };
        coordinate_str
    }

    pub(crate) fn adjustment_insert_coordinate(
        &mut self,
        root_col_num: &u32,
        offset_col_num: &u32,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) {
        match &mut self.coordinate_start_col {
            Some(v) => {
                v.adjustment_insert_coordinate(root_col_num, offset_col_num);
            }
            None => {}
        }
        match &mut self.coordinate_start_row {
            Some(v) => {
                v.adjustment_insert_coordinate(root_row_num, offset_row_num);
            }
            None => {}
        }
        match &mut self.coordinate_end_col {
            Some(v) => {
                v.adjustment_insert_coordinate(root_col_num, offset_col_num);
            }
            None => {}
        }
        match &mut self.coordinate_end_row {
            Some(v) => {
                v.adjustment_insert_coordinate(root_row_num, offset_row_num);
            }
            None => {}
        }
    }

    pub(crate) fn adjustment_remove_coordinate(
        &mut self,
        root_col_num: &u32,
        offset_col_num: &u32,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) {
        match &mut self.coordinate_start_col {
            Some(v) => {
                v.adjustment_remove_coordinate(root_col_num, offset_col_num);
            }
            None => {}
        }
        match &mut self.coordinate_start_row {
            Some(v) => {
                v.adjustment_remove_coordinate(root_row_num, offset_row_num);
            }
            None => {}
        }
        match &mut self.coordinate_end_col {
            Some(v) => {
                v.adjustment_remove_coordinate(root_col_num, offset_col_num);
            }
            None => {}
        }
        match &mut self.coordinate_end_row {
            Some(v) => {
                v.adjustment_remove_coordinate(root_row_num, offset_row_num);
            }
            None => {}
        }
    }

    pub(crate) fn is_remove(
        &self,
        root_col_num: &u32,
        offset_col_num: &u32,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) -> bool {
        let start_col_result = match &self.coordinate_start_col {
            Some(v) => v.is_remove(root_col_num, offset_col_num),
            None => false,
        };
        let start_row_result = match &self.coordinate_start_row {
            Some(v) => v.is_remove(root_row_num, offset_row_num),
            None => false,
        };
        let end_col_result = match &self.coordinate_end_col {
            Some(v) => v.is_remove(root_col_num, offset_col_num),
            None => false,
        };
        let end_row_result = match &self.coordinate_end_row {
            Some(v) => v.is_remove(root_row_num, offset_row_num),
            None => false,
        };
        start_col_result && start_row_result && end_col_result && end_row_result
    }
}