Skip to main content

umya_spreadsheet/structs/
address.rs

1use super::Range;
2use crate::{
3    helper::{
4        address::split_address,
5        coordinate::index_from_coordinate,
6        utils::compile_regex,
7    },
8    traits::{
9        AdjustmentCoordinate,
10        AdjustmentCoordinateWithSheet,
11    },
12};
13
14#[derive(Clone, Default, Debug)]
15pub struct Address {
16    sheet_name: Box<str>,
17    range:      Range,
18}
19
20impl Address {
21    #[inline]
22    #[must_use]
23    pub fn sheet_name(&self) -> &str {
24        &self.sheet_name
25    }
26
27    #[inline]
28    #[must_use]
29    #[deprecated(since = "3.0.0", note = "Use sheet_name()")]
30    pub fn get_sheet_name(&self) -> &str {
31        self.sheet_name()
32    }
33
34    #[inline]
35    pub fn set_sheet_name<S: Into<String>>(&mut self, value: S) -> &mut Self {
36        self.sheet_name = value.into().into_boxed_str();
37        self
38    }
39
40    #[inline]
41    #[must_use]
42    pub fn range(&self) -> &Range {
43        &self.range
44    }
45
46    #[inline]
47    #[must_use]
48    #[deprecated(since = "3.0.0", note = "Use range()")]
49    pub fn get_range(&self) -> &Range {
50        self.range()
51    }
52
53    #[inline]
54    pub fn range_mut(&mut self) -> &mut Range {
55        &mut self.range
56    }
57
58    #[inline]
59    #[deprecated(since = "3.0.0", note = "Use range_mut()")]
60    pub fn get_range_mut(&mut self) -> &mut Range {
61        self.range_mut()
62    }
63
64    #[inline]
65    pub fn set_range(&mut self, value: Range) -> &mut Self {
66        self.range = value;
67        self
68    }
69
70    pub fn set_address<S: Into<String>>(&mut self, value: S) -> &mut Self {
71        let org_value = value.into();
72        let (sheet_name, range) = split_address(&org_value);
73        self.range.set_range(range);
74        if !sheet_name.is_empty() {
75            self.sheet_name = sheet_name.into();
76        }
77        self
78    }
79
80    #[inline]
81    #[must_use]
82    pub fn address(&self) -> String {
83        self.address_crate(false)
84    }
85
86    #[inline]
87    #[must_use]
88    #[deprecated(since = "3.0.0", note = "Use address()")]
89    pub fn get_address(&self) -> String {
90        self.address()
91    }
92
93    #[inline]
94    pub(crate) fn address_ptn2(&self) -> String {
95        self.address_crate(true)
96    }
97
98    #[inline]
99    #[deprecated(since = "3.0.0", note = "Use address_ptn2()")]
100    pub(crate) fn get_address_ptn2(&self) -> String {
101        self.address_ptn2()
102    }
103
104    pub(crate) fn address_crate(&self, is_ptn2: bool) -> String {
105        let range = self.range.range();
106        if self.sheet_name.is_empty() {
107            return range;
108        }
109        let mut with_space_char = "";
110        let mut sheet_name = self.sheet_name.clone();
111        if sheet_name.contains(char::is_whitespace) {
112            with_space_char = "'";
113        }
114        if is_ptn2 {
115            if sheet_name.contains('!') {
116                with_space_char = "'";
117            }
118            if sheet_name.contains('\'') {
119                with_space_char = "'";
120                sheet_name = sheet_name.replace('\'', "''").into_boxed_str();
121            }
122            if sheet_name.contains('"') {
123                with_space_char = "'";
124            }
125            if with_space_char.is_empty()
126                && compile_regex!(r"[^0-9a-zA-Z]")
127                    .is_match(&sheet_name)
128                    .unwrap_or(false)
129            {
130                with_space_char = "'";
131            }
132            if with_space_char.is_empty()
133                && (None, None, None, None) != index_from_coordinate(&sheet_name)
134            {
135                with_space_char = "'";
136            }
137        }
138        format!("{with_space_char}{sheet_name}{with_space_char}!{range}")
139    }
140
141    #[deprecated(since = "3.0.0", note = "Use address_crate()")]
142    pub(crate) fn get_address_crate(&self, is_ptn2: bool) -> String {
143        self.address_crate(is_ptn2)
144    }
145}
146impl AdjustmentCoordinateWithSheet for Address {
147    #[inline]
148    fn adjustment_insert_coordinate_with_sheet(
149        &mut self,
150        sheet_name: &str,
151        root_col_num: u32,
152        offset_col_num: u32,
153        root_row_num: u32,
154        offset_row_num: u32,
155    ) {
156        if &*self.sheet_name == sheet_name {
157            self.range.adjustment_insert_coordinate(
158                root_col_num,
159                offset_col_num,
160                root_row_num,
161                offset_row_num,
162            );
163        }
164    }
165
166    #[inline]
167    fn adjustment_remove_coordinate_with_sheet(
168        &mut self,
169        sheet_name: &str,
170        root_col_num: u32,
171        offset_col_num: u32,
172        root_row_num: u32,
173        offset_row_num: u32,
174    ) {
175        if &*self.sheet_name == sheet_name {
176            self.range.adjustment_remove_coordinate(
177                root_col_num,
178                offset_col_num,
179                root_row_num,
180                offset_row_num,
181            );
182        }
183    }
184
185    #[inline]
186    fn is_remove_coordinate_with_sheet(
187        &self,
188        sheet_name: &str,
189        root_col_num: u32,
190        offset_col_num: u32,
191        root_row_num: u32,
192        offset_row_num: u32,
193    ) -> bool {
194        &*self.sheet_name == sheet_name
195            && self.range.is_remove_coordinate(
196                root_col_num,
197                offset_col_num,
198                root_row_num,
199                offset_row_num,
200            )
201    }
202}