umya_spreadsheet/structs/
coordinate.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, Eq, Ord, PartialEq, PartialOrd)]
8pub struct Coordinate {
9    column: ColumnReference,
10    row: RowReference,
11}
12
13impl ToString for Coordinate {
14    #[inline]
15    fn to_string(&self) -> String {
16        coordinate_from_index_with_lock(
17            self.column.get_num(),
18            self.row.get_num(),
19            self.column.get_is_lock(),
20            self.row.get_is_lock(),
21        )
22    }
23}
24
25impl Coordinate {
26    #[inline]
27    pub fn get_col_num(&self) -> &u32 {
28        self.column.get_num()
29    }
30
31    #[inline]
32    pub fn set_col_num(&mut self, value: u32) -> &mut Self {
33        self.column.set_num(value);
34        self
35    }
36
37    #[inline]
38    pub(crate) fn offset_col_num(&mut self, value: i32) -> &mut Self {
39        self.column.offset_num(value);
40        self
41    }
42
43    #[inline]
44    pub fn get_row_num(&self) -> &u32 {
45        self.row.get_num()
46    }
47
48    #[inline]
49    pub fn set_row_num(&mut self, value: u32) -> &mut Self {
50        self.row.set_num(value);
51        self
52    }
53
54    #[inline]
55    pub(crate) fn offset_row_num(&mut self, value: i32) -> &mut Self {
56        self.row.offset_num(value);
57        self
58    }
59
60    #[inline]
61    pub fn get_is_lock_col(&self) -> &bool {
62        self.column.get_is_lock()
63    }
64
65    #[inline]
66    pub fn set_is_lock_col(&mut self, value: bool) -> &mut Self {
67        self.column.set_is_lock(value);
68        self
69    }
70
71    #[inline]
72    pub fn get_is_lock_row(&self) -> &bool {
73        self.row.get_is_lock()
74    }
75
76    #[inline]
77    pub fn set_is_lock_row(&mut self, value: bool) -> &mut Self {
78        self.row.set_is_lock(value);
79        self
80    }
81
82    /// Change coordinates
83    /// Formula is not updated.
84    #[inline]
85    pub fn set_coordinate<S: AsRef<str>>(&mut self, value: S) -> &mut Self {
86        let (c, r, cl, rl) = index_from_coordinate(value.as_ref());
87
88        self.column.set_num(c.unwrap());
89        self.row.set_num(r.unwrap());
90        self.column.set_is_lock(cl.unwrap());
91        self.row.set_is_lock(rl.unwrap());
92
93        self
94    }
95
96    #[inline]
97    pub fn get_coordinate(&self) -> String {
98        coordinate_from_index_with_lock(
99            self.column.get_num(),
100            self.row.get_num(),
101            self.column.get_is_lock(),
102            self.row.get_is_lock(),
103        )
104    }
105}
106impl AdjustmentCoordinate for Coordinate {
107    #[inline]
108    fn adjustment_insert_coordinate(
109        &mut self,
110        root_col_num: &u32,
111        offset_col_num: &u32,
112        root_row_num: &u32,
113        offset_row_num: &u32,
114    ) {
115        self.column
116            .adjustment_insert_value(root_col_num, offset_col_num);
117        self.row
118            .adjustment_insert_value(root_row_num, offset_row_num);
119    }
120
121    #[inline]
122    fn adjustment_remove_coordinate(
123        &mut self,
124        root_col_num: &u32,
125        offset_col_num: &u32,
126        root_row_num: &u32,
127        offset_row_num: &u32,
128    ) {
129        self.column
130            .adjustment_remove_value(root_col_num, offset_col_num);
131        self.row
132            .adjustment_remove_value(root_row_num, offset_row_num);
133    }
134
135    #[inline]
136    fn is_remove_coordinate(
137        &self,
138        root_col_num: &u32,
139        offset_col_num: &u32,
140        root_row_num: &u32,
141        offset_row_num: &u32,
142    ) -> bool {
143        self.column.is_remove_value(root_col_num, offset_col_num)
144            || self.row.is_remove_value(root_row_num, offset_row_num)
145    }
146}