Skip to main content

tree_table/api/
data_get_borrowed.rs

1use alloc::string::ToString;
2use alloc::{string::String, vec, vec::Vec};
3
4use crate::args::{GetDataIidsArgs, GetDataSpanArgs, GetDataYxArgs};
5use crate::{CellKey, SpanKey, Table, Val};
6
7impl Table {
8    /// Gets row data using an array of row ids or positions.
9    pub fn get_data_iids<'a>(
10        &'a self,
11        kwargs: Option<GetDataIidsArgs>,
12    ) -> Result<Vec<(String, Vec<(String, &'a Val)>)>, String> {
13        let empty_val = &self.opts.empty_val;
14        let mut result: Vec<(String, Vec<(String, &'a Val)>)> = Vec::new();
15        match kwargs {
16            Some(kwargs) => {
17                if kwargs.ignore_invalid {
18                    for (row_iid, cols) in kwargs.rows.into_iter() {
19                        let Ok(rn) = Self::acr_pos(&self.grid.index.iid_to_row, &row_iid) else {
20                            continue;
21                        };
22                        let mut row_result: Vec<(String, &'a Val)> = Vec::new();
23                        for col_iid in cols.into_iter() {
24                            let Ok(cn) = Self::acr_pos(&self.grid.header.iid_to_col, &col_iid)
25                            else {
26                                continue;
27                            };
28                            row_result.push((
29                                col_iid,
30                                Self::acr_tcell_val(&self.grid.index.cells, rn, cn)
31                                    .unwrap_or(empty_val),
32                            ));
33                        }
34                        result.push((row_iid, row_result));
35                    }
36                } else {
37                    for (row_iid, cols) in kwargs.rows.into_iter() {
38                        let rn = Self::acr_pos(&self.grid.index.iid_to_row, &row_iid)?;
39                        let mut row_result: Vec<(String, &'a Val)> = Vec::new();
40                        for col_iid in cols.into_iter() {
41                            let cn = Self::acr_pos(&self.grid.header.iid_to_col, &col_iid)?;
42                            row_result.push((
43                                col_iid,
44                                Self::acr_tcell_val(&self.grid.index.cells, rn, cn)
45                                    .unwrap_or(empty_val),
46                            ));
47                        }
48                        result.push((row_iid, row_result));
49                    }
50                }
51            }
52            None => {
53                let num_cols = Self::acr_total_cols(&self.grid.header.cells);
54                for (rn, icell) in self.grid.index.cells.iter().enumerate() {
55                    let mut row_result: Vec<(String, &'a Val)> = Vec::new();
56                    for cn in 0..num_cols {
57                        row_result.push((
58                            Self::acr_borrow_ciid(&self.grid.header.cells, cn)?.to_string(),
59                            Self::acr_tcell_val(&self.grid.index.cells, rn, cn)
60                                .unwrap_or(empty_val),
61                        ))
62                    }
63                    result.push((icell.iid.to_string(), row_result));
64                }
65            }
66        }
67        Ok(result)
68    }
69
70    /// Gets table cell data using cell coordinates.
71    pub fn get_data_yx<'a>(
72        &'a self,
73        kwargs: Option<GetDataYxArgs>,
74    ) -> Result<Vec<(CellKey, &'a Val)>, String> {
75        let empty_val = &self.opts.empty_val;
76        let mut result: Vec<(CellKey, &'a Val)> = Vec::new();
77        if let Some(kwargs) = kwargs {
78            for key in kwargs.coords.into_iter() {
79                let rn = key.0 as usize;
80                let cn = key.1 as usize;
81                result.push((
82                    key,
83                    Self::acr_tcell_val(&self.grid.index.cells, rn, cn).unwrap_or(empty_val),
84                ));
85            }
86        } else {
87            let num_rows = Self::acr_total_rows(&self.grid.index.cells);
88            let num_cols = Self::acr_total_cols(&self.grid.header.cells);
89            for rn in 0..num_rows {
90                for cn in 0..num_cols {
91                    result.push((
92                        CellKey(rn as u64, cn as u64),
93                        Self::acr_tcell_val(&self.grid.index.cells, rn, cn).unwrap_or(empty_val),
94                    ));
95                }
96            }
97        }
98        Ok(result)
99    }
100
101    /// Gets data as a 2d array using a key.
102    pub fn get_data_span<'a>(
103        &'a self,
104        kwargs: Option<GetDataSpanArgs>,
105    ) -> Result<Vec<Vec<&'a Val>>, String> {
106        let kwargs = kwargs.unwrap_or(GetDataSpanArgs {
107            key: SpanKey::None,
108            index: true,
109            header: true,
110            transpose: false,
111            table: true,
112        });
113        let empty_val = &self.opts.empty_val;
114        let mut result: Vec<Vec<&'a Val>> = Vec::new();
115        let span = self.cr_key_to_span(kwargs.key)?;
116        let (r_start, r_end) = span.rows_start_end(&self.grid);
117        let (c_start, c_end) = span.cols_start_end(&self.grid);
118
119        // Sub-arrays are columns
120        if kwargs.transpose {
121            if kwargs.index {
122                let mut index_row = if kwargs.header {
123                    vec![empty_val]
124                } else {
125                    vec![]
126                };
127                for r in r_start..r_end {
128                    index_row.push(Self::acr_icell_val(&self.grid.index.cells, r)?);
129                }
130                result.push(index_row);
131            }
132            if kwargs.header || kwargs.table {
133                for col in c_start..c_end {
134                    let mut col_data: Vec<&'a Val> = Vec::new();
135                    if kwargs.header {
136                        col_data.push(Self::acr_hcell_val(&self.grid.header.cells, col)?);
137                    }
138                    if kwargs.table {
139                        col_data.extend((r_start..r_end).map(|r| {
140                            Self::acr_tcell_val(&self.grid.index.cells, r, col).unwrap_or(empty_val)
141                        }));
142                    }
143                    result.push(col_data);
144                }
145            }
146        // Sub-arrays are rows
147        } else {
148            if kwargs.header {
149                let mut header_row: Vec<&'a Val> = if kwargs.index {
150                    vec![empty_val]
151                } else {
152                    vec![]
153                };
154                for c in c_start..c_end {
155                    header_row.push(Self::acr_hcell_val(&self.grid.header.cells, c)?);
156                }
157                result.push(header_row);
158            }
159            if kwargs.index || kwargs.table {
160                for row in r_start..r_end {
161                    let mut row_data: Vec<&'a Val> = Vec::new();
162                    if kwargs.index {
163                        row_data.push(Self::acr_icell_val(&self.grid.index.cells, row)?);
164                    }
165                    if kwargs.table {
166                        row_data.extend((c_start..c_end).map(|c| {
167                            Self::acr_tcell_val(&self.grid.index.cells, row, c).unwrap_or(empty_val)
168                        }));
169                    }
170                    result.push(row_data);
171                }
172            }
173        }
174        Ok(result)
175    }
176}