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
use super::ColumnReference;
use super::RowReference;
use helper::coordinate::*;
use traits::AdjustmentCoordinate;
use traits::AdjustmentValue;

#[derive(Clone, Default, Debug)]
pub struct Range {
    start_col: Option<ColumnReference>,
    start_row: Option<RowReference>,
    end_col: Option<ColumnReference>,
    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();

        assert!(
            matches!(coordinate_collection.len(), 1 | 2),
            "Non-standard coordinate"
        );

        let (
            row,         //
            col,         //
            is_lock_col, //
            is_lock_row,
        ) = index_from_coordinate(coordinate_collection[0]);
        if let Some(v) = row {
            let mut coordinate_start_col = ColumnReference::default();
            coordinate_start_col.set_num(v);
            coordinate_start_col.set_is_lock(is_lock_col.unwrap());
            self.start_col = Some(coordinate_start_col);
        };
        if let Some(v) = col {
            let mut coordinate_start_row = RowReference::default();
            coordinate_start_row.set_num(v);
            coordinate_start_row.set_is_lock(is_lock_row.unwrap());
            self.start_row = Some(coordinate_start_row);
        }

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

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

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

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

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

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

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

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

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

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

    pub(crate) fn get_coordinate_start(&self) -> String {
        let mut coordinate_str = "".into();
        if let Some(v) = &self.start_col {
            coordinate_str = v.get_coordinate();
        };
        if let Some(v) = &self.start_row {
            coordinate_str = format!("{}{}", coordinate_str, v.get_coordinate());
        };
        coordinate_str
    }

    pub(crate) fn get_coordinate_end(&self) -> String {
        let mut coordinate_str = "".into();
        if let Some(v) = &self.end_col {
            coordinate_str = v.get_coordinate();
        };
        if let Some(v) = &self.end_row {
            coordinate_str = format!("{}{}", coordinate_str, v.get_coordinate());
        };
        coordinate_str
    }
}
impl AdjustmentCoordinate for Range {
    fn adjustment_insert_coordinate(
        &mut self,
        root_col_num: &u32,
        offset_col_num: &u32,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) {
        if let Some(v) = &mut self.start_col {
            v.adjustment_insert_value(root_col_num, offset_col_num);
        }
        if let Some(v) = &mut self.start_row {
            v.adjustment_insert_value(root_row_num, offset_row_num);
        }
        if let Some(v) = &mut self.end_col {
            v.adjustment_insert_value(root_col_num, offset_col_num);
        }
        if let Some(v) = &mut self.end_row {
            v.adjustment_insert_value(root_row_num, offset_row_num);
        }
    }

    fn adjustment_remove_coordinate(
        &mut self,
        root_col_num: &u32,
        offset_col_num: &u32,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) {
        if let Some(v) = &mut self.start_col {
            v.adjustment_remove_value(root_col_num, offset_col_num);
        }
        if let Some(v) = &mut self.start_row {
            v.adjustment_remove_value(root_row_num, offset_row_num);
        }
        if let Some(v) = &mut self.end_col {
            v.adjustment_remove_value(root_col_num, offset_col_num);
        }
        if let Some(v) = &mut self.end_row {
            v.adjustment_remove_value(root_row_num, offset_row_num);
        }
    }

    fn is_remove_coordinate(
        &self,
        root_col_num: &u32,
        offset_col_num: &u32,
        root_row_num: &u32,
        offset_row_num: &u32,
    ) -> bool {
        let start_col_result = match &self.start_col {
            Some(v) => v.is_remove_value(root_col_num, offset_col_num),
            None => false,
        };
        let start_row_result = match &self.start_row {
            Some(v) => v.is_remove_value(root_row_num, offset_row_num),
            None => false,
        };
        let end_col_result = match &self.end_col {
            Some(v) => v.is_remove_value(root_col_num, offset_col_num),
            None => false,
        };
        let end_row_result = match &self.end_row {
            Some(v) => v.is_remove_value(root_row_num, offset_row_num),
            None => false,
        };
        start_col_result && start_row_result && end_col_result && end_row_result
    }
}