Skip to content

Commit

Permalink
some improvements/optimizations (#174)
Browse files Browse the repository at this point in the history
* some improvements/optimisations

* fix for linting
  • Loading branch information
agentjill authored Feb 6, 2024
1 parent a5c9f11 commit 804d832
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 327 deletions.
7 changes: 5 additions & 2 deletions src/helper/const_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ pub(crate) const DRAWINGML_MAIN_NS: &str = "http://schemas.openxmlformats.org/dr
pub(crate) const DRAWINGS_NS: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/drawing";
pub(crate) const ENCRYPTION_NS: &str = "http://schemas.microsoft.com/office/2006/encryption";
pub(crate) const EXCEL_NS: &str = "urn:schemas-microsoft-com:office:excel";
pub(crate) const HYPERLINK_NS: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/hyperlink";
pub(crate) const IMAGE_NS: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
pub(crate) const MC_NS: &str = "http://schemas.openxmlformats.org/markup-compatibility/2006";
pub(crate) const OFCDOC_NS: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument";
pub(crate) const OFFICE_NS: &str = "urn:schemas-microsoft-com:office:office";
pub(crate) const OLE_OBJECT_NS: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/oleObject";
pub(crate) const PASSWORD_NS: &str =
Expand Down Expand Up @@ -56,10 +58,11 @@ pub(crate) const TABLE_NS: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/table";
pub(crate) const THEME_NS: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/theme";
pub(crate) const VML_DRAWING_NS: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing";
pub(crate) const VBA_PROJECT_NS: &str =
"http://schemas.microsoft.com/office/2006/relationships/vbaProject";
pub(crate) const VML_NS: &str = "urn:schemas-microsoft-com:vml";
pub(crate) const VML_DRAWING_NS: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/vmlDrawing";
pub(crate) const VTYPES_NS: &str =
"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes";
pub(crate) const WORKSHEET_NS: &str =
Expand Down
8 changes: 2 additions & 6 deletions src/reader/xlsx/doc_props_app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,10 @@ pub(crate) fn read<R: io::Read + io::Seek>(
Ok(Event::End(ref e)) => {
match e.name().into_inner() {
b"Manager" => {
spreadsheet
.get_properties_mut()
.set_manager(string_value.clone());
spreadsheet.get_properties_mut().set_manager(&string_value);
}
b"Company" => {
spreadsheet
.get_properties_mut()
.set_company(string_value.clone());
spreadsheet.get_properties_mut().set_company(&string_value);
}
_ => (),
}
Expand Down
10 changes: 5 additions & 5 deletions src/structs/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,21 @@ impl Address {
if self.sheet_name.is_empty() {
return range;
}
let mut with_space_char = String::from("");
let mut with_space_char = "";
let mut sheet_name = self.sheet_name.clone();
if sheet_name.contains(char::is_whitespace) {
with_space_char = String::from("'");
with_space_char = "'";
}
if is_ptn2 {
if sheet_name.contains("!") {
with_space_char = String::from("'");
with_space_char = "'";
}
if sheet_name.contains("'") {
with_space_char = String::from("'");
with_space_char = "'";
sheet_name = sheet_name.replace("'", "''");
}
if sheet_name.contains(r#"""#) {
with_space_char = String::from("'");
with_space_char = "'";
}
}
format!(
Expand Down
14 changes: 7 additions & 7 deletions src/structs/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,9 @@ impl Cell {
}

pub(crate) fn set_obj(&mut self, cell: Self) -> &mut Self {
self.cell_value = cell.get_cell_value().clone();
self.style = cell.get_style().clone();
self.hyperlink = cell.get_hyperlink().cloned();
self.cell_value = cell.cell_value;
self.style = cell.style;
self.hyperlink = cell.hyperlink;
self
}

Expand Down Expand Up @@ -310,11 +310,11 @@ impl Cell {
}
Ok(Event::End(ref e)) => match e.name().into_inner() {
b"f" => {
self.set_formula(string_value.clone());
self.set_formula(&string_value);
}
b"v" => {
if type_value == "str" {
self.set_value_str(string_value.clone());
self.set_value_str(&string_value);
}
if type_value == "s" {
let index = string_value.parse::<usize>().unwrap();
Expand All @@ -329,12 +329,12 @@ impl Cell {
} else if type_value == "e" {
self.set_error();
} else if type_value.is_empty() || type_value == "n" {
self.set_value_crate(string_value.clone());
self.set_value_crate(&string_value);
};
}
b"is" => {
if type_value == "inlineStr" {
self.set_value_crate(string_value.clone());
self.set_value_crate(&string_value);
}
}
b"c" => return,
Expand Down
21 changes: 13 additions & 8 deletions src/structs/cells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,18 @@ impl Cells {
}

pub(crate) fn rebuild_map(&mut self) {
let mut rebuild: HashMap<(u32, u32), Cell> = HashMap::new();
for ((_, _), cell) in self.get_collection_to_hashmap_mut() {
let col_num = cell.get_coordinate().get_col_num();
let row_num = cell.get_coordinate().get_row_num();
let k = (row_num.to_owned(), col_num.to_owned());
rebuild.insert(k, cell.clone());
}
self.map = rebuild;
self.map = self
.get_collection_to_hashmap_mut()
.iter_mut()
.map(|(_, cell)| {
(
(
*cell.get_coordinate().get_row_num(),
*cell.get_coordinate().get_col_num(),
),
std::mem::take(cell),
)
})
.collect()
}
}
2 changes: 1 addition & 1 deletion src/structs/numbering_formats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl NumberingFormats {
}

pub(crate) fn write_to(&self, writer: &mut Writer<Cursor<Vec<u8>>>) {
let formats_to_write: HashMap<_,_> = self
let formats_to_write: HashMap<_, _> = self
.numbering_format
.iter()
.filter(|(k, v)| !*v.get_is_build_in())
Expand Down
11 changes: 5 additions & 6 deletions src/structs/rows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,10 @@ impl Rows {
}

pub(crate) fn rebuild_map(&mut self) {
let mut rebuild: HashMap<u32, Row> = HashMap::new();
for (_, row) in self.get_row_dimensions_to_hashmap_mut() {
let row_num = row.get_row_num();
rebuild.insert(row_num.to_owned(), row.clone());
}
self.rows = rebuild;
self.rows = self
.get_row_dimensions_to_hashmap_mut()
.iter_mut()
.map(|(_, row)| (*row.get_row_num(), std::mem::take(row)))
.collect();
}
}
111 changes: 44 additions & 67 deletions src/structs/spreadsheet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,12 +363,9 @@ impl Spreadsheet {
}

pub(crate) fn find_sheet_index_by_name(&self, sheet_name: &str) -> Option<usize> {
for (result, sheet) in self.work_sheet_collection.iter().enumerate() {
if sheet.get_name() == sheet_name {
return Some(result);
}
}
None
self.work_sheet_collection
.iter()
.position(|sheet| sheet.get_name() == sheet_name)
}

/// Get Work Sheet.
Expand All @@ -377,13 +374,12 @@ impl Spreadsheet {
/// # Return value
/// * `Option<&Worksheet>`.
pub fn get_sheet(&self, index: &usize) -> Option<&Worksheet> {
match self.work_sheet_collection.get(*index) {
Some(v) => {
self.work_sheet_collection
.get(*index)
.map(|v| {
assert!(v.is_deserialized(),"This Worksheet is Not Deserialized. Please exec to read_sheet(&mut self, index: usize);");
Some(v)
}
None => None,
}
v
})
}

/// Get Work Sheet.
Expand All @@ -392,23 +388,21 @@ impl Spreadsheet {
/// # Return value
/// * `Option<&Worksheet>.
pub fn get_sheet_by_name(&self, sheet_name: &str) -> Option<&Worksheet> {
match self.find_sheet_index_by_name(sheet_name) {
Some(index) => {
return self.get_sheet(&index);
}
None => None,
}
self.find_sheet_index_by_name(sheet_name)
.and_then(|index| self.get_sheet(&index))
}

pub fn get_lazy_read_sheet_cells(&self, index: &usize) -> Result<Cells, &'static str> {
let shared_string_table = self.get_shared_string_table();
match self.work_sheet_collection.get(*index) {
Some(v) => Ok(v.get_cell_collection_stream(
&shared_string_table.read().unwrap(),
self.get_stylesheet(),
)),
None => Err("Not found."),
}
self.work_sheet_collection
.get(*index)
.map(|v| {
v.get_cell_collection_stream(
&shared_string_table.read().unwrap(),
self.get_stylesheet(),
)
})
.ok_or("Not found.")
}

/// Get Work Sheet in mutable.
Expand All @@ -420,13 +414,10 @@ impl Spreadsheet {
let theme = self.get_theme().clone();
let shared_string_table = self.get_shared_string_table();
let stylesheet = self.get_stylesheet().clone();
match self.work_sheet_collection.get_mut(*index) {
Some(v) => {
raw_to_deserialize_by_worksheet(v, &theme, shared_string_table, &stylesheet);
Some(v)
}
None => None,
}
self.work_sheet_collection.get_mut(*index).map(|v| {
raw_to_deserialize_by_worksheet(v, &theme, shared_string_table, &stylesheet);
v
})
}

/// Get Work Sheet in mutable.
Expand All @@ -435,12 +426,8 @@ impl Spreadsheet {
/// # Return value
/// * `Option<&mut Worksheet>`.
pub fn get_sheet_by_name_mut(&mut self, sheet_name: &str) -> Option<&mut Worksheet> {
match self.find_sheet_index_by_name(sheet_name) {
Some(index) => {
return self.get_sheet_mut(&index);
}
None => None,
}
self.find_sheet_index_by_name(sheet_name)
.and_then(move |index| self.get_sheet_mut(&index))
}

pub fn set_active_sheet(&mut self, index: u32) -> &mut Self {
Expand Down Expand Up @@ -471,9 +458,7 @@ impl Spreadsheet {
/// * `Result<&mut Worksheet, &'static str>` - OK:added work sheet. Err:Error.
pub fn add_sheet(&mut self, value: Worksheet) -> Result<&mut Worksheet, &'static str> {
let title = value.get_name();
if let Err(e) = Spreadsheet::check_sheet_name(self, title) {
return Err(e);
}
Spreadsheet::check_sheet_name(self, title)?;
self.work_sheet_collection.push(value);
Ok(self.work_sheet_collection.last_mut().unwrap())
}
Expand Down Expand Up @@ -517,9 +502,7 @@ impl Spreadsheet {
sheet_title: S,
) -> Result<&mut Worksheet, &'static str> {
let v = sheet_title.into();
if let Err(e) = Spreadsheet::check_sheet_name(self, &v) {
return Err(e);
}
Spreadsheet::check_sheet_name(self, &v)?;
let sheet_id = (self.work_sheet_collection.len() + 1).to_string();
Ok(Spreadsheet::add_new_sheet_crate(self, sheet_id, v))
}
Expand Down Expand Up @@ -558,27 +541,26 @@ impl Spreadsheet {
sheet_name: S,
) -> Result<(), &'static str> {
let sheet_name_str = sheet_name.into();
if let Err(e) = Spreadsheet::check_sheet_name(self, sheet_name_str.as_ref()) {
return Err(e);
}
match self.work_sheet_collection.get_mut(index) {
Some(sheet) => {
Spreadsheet::check_sheet_name(self, sheet_name_str.as_ref())?;
self.work_sheet_collection
.get_mut(index)
.map(|sheet| {
sheet.set_name(sheet_name_str);
Ok(())
}
None => Err("sheet not found."),
}
})
.ok_or("sheet not found.")
}

/// (This method is crate only.)
/// Check for duplicate sheet name.
pub(crate) fn check_sheet_name(&self, value: &str) -> Result<(), &'static str> {
for work_sheet in &self.work_sheet_collection {
if value == work_sheet.get_name() {
return Err("name duplicate.");
}
match self
.work_sheet_collection
.iter()
.any(|work_sheet| value == work_sheet.get_name())
{
true => Err("name duplicate."),
false => Ok(()),
}
Ok(())
}

/// (This method is crate only.)
Expand Down Expand Up @@ -654,15 +636,10 @@ impl Spreadsheet {
}

pub(crate) fn update_pivot_caches(&mut self, key: String, value: String) -> &mut Self {
let mut result: Vec<(String, String, String)> = Vec::new();
for (val1, val2, val3) in &self.pivot_caches {
let mut result_value = &value;
if val1 != &key {
result_value = val3;
}
result.push((val1.to_string(), val2.to_string(), result_value.to_string()));
}
self.pivot_caches = result;
self.pivot_caches.iter_mut().for_each(|(val1, _, val3)| {
let result_value = if val1 == &key { &value } else { &val3 };
*val3 = result_value.to_string();
});
self
}

Expand Down
Loading

0 comments on commit 804d832

Please sign in to comment.