Skip to content

Commit

Permalink
Merge branch 'master' into use-clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
MathNya authored Nov 6, 2023
2 parents b92d730 + 8ca2b9a commit 844652a
Show file tree
Hide file tree
Showing 13 changed files with 508 additions and 0 deletions.
10 changes: 10 additions & 0 deletions USS.code-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"folders": [
{
"path": "../umya-spreadsheet"
},
],
"settings": {
"debug.allowBreakpointsEverywhere": true
}
}
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@
//! let _ = writer::xlsx::write(&book, path);
//! ```
#![allow(warnings)]
#![allow(clippy::all)]

extern crate chrono;
extern crate fancy_regex;
extern crate hashbrown;
Expand Down
15 changes: 15 additions & 0 deletions src/reader/xlsx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub(crate) mod drawing;
mod rels;
mod shared_strings;
mod styles;
pub(crate) mod table;
mod theme;
mod vba_project_bin;
pub(crate) mod vml_drawing;
Expand Down Expand Up @@ -194,6 +195,20 @@ pub(crate) fn raw_to_deserialize_by_worksheet(
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" => {
comment::read(worksheet, relationship.get_raw_file()).unwrap();
}
// table
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/table" => {
table::read(
worksheet,
relationship.get_raw_file()
)
.unwrap();
}
_ => {}
}
// comment
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments" => {
comment::read(worksheet, relationship.get_raw_file()).unwrap();
}
_ => {}
}
}
Expand Down
124 changes: 124 additions & 0 deletions src/reader/xlsx/table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
use super::driver::*;
use super::XlsxError;
use quick_xml::events::Event;
use quick_xml::Reader;
use std::result;
use structs::raw::RawFile;
use structs::Comment;
use structs::Worksheet;
use structs::{Table, TableColumn, TableStyleInfo};

pub(crate) fn read(
worksheet: &mut Worksheet,
table_file: &RawFile,
) -> result::Result<(), XlsxError> {
let data = std::io::Cursor::new(table_file.get_file_data());
let mut reader = Reader::from_reader(data);
reader.trim_text(false);
let mut buf = Vec::new();
let mut table = Table::default();
loop {
match reader.read_event_into(&mut buf) {
Ok(Event::Empty(ref e)) => match e.name().into_inner() {
b"tableColumn" => {
let mut table_column = TableColumn::default();
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) => match attr.key.0 {
b"name" => {
table_column.set_name(get_attribute_value(attr)?);
}
_ => {}
},
_ => {}
}
}
// add column to table (if it has a name)
if !table_column.get_name().is_empty() {
table.add_column(table_column);
}
}
b"tableStyleInfo" => {
let mut name = String::new();
let mut show_first_col = false;
let mut show_last_col = false;
let mut show_row_stripes = false;
let mut show_col_stripes = false;
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) => {
let attr_val = get_attribute_value(attr)?;
match attr.key.0 {
b"name" => {
name = attr_val;
}
b"showFirstColumn" => {
show_first_col = attr_val == "1";
}
b"showLastColumn" => {
show_first_col = attr_val == "1";
}
b"showRowStripes" => {
show_row_stripes = attr_val == "1";
}
b"showColumnStripes" => {
show_col_stripes = attr_val == "1";
}
_ => {}
}
}
_ => {}
}
}
if !name.is_empty() {
table.set_style_info(Some(TableStyleInfo::new(
&name,
show_first_col,
show_last_col,
show_row_stripes,
show_col_stripes,
)));
}
}
_ => (),
},
Ok(Event::Start(ref e)) => match e.name().into_inner() {
b"table" => {
for a in e.attributes().with_checks(false) {
match a {
Ok(ref attr) => {
let attr_val = get_attribute_value(attr)?;
match attr.key.0 {
b"displayName" => {
table.set_display_name(&attr_val);
}
b"name" => {
table.set_name(&attr_val);
}
b"ref" => {
let area_coords: Vec<&str> = attr_val.split(':').collect();
if area_coords.len() == 2 {
table.set_area((area_coords[0], area_coords[1]));
}
}
_ => {}
}
}
_ => {}
}
}
}
_ => (),
},
Ok(Event::Eof) => break,
Err(e) => panic!("Error at position {}: {:?}", reader.buffer_position(), e),
_ => (),
}
buf.clear();
}
// add the table to the sheet (if a few sanity checks pass)
if table.is_ok() {
worksheet.add_table(table);
}
Ok(())
}
3 changes: 3 additions & 0 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,9 @@ pub use self::icon_set::*;
mod formula;
pub use self::formula::*;

mod table;
pub use self::table::*;

mod data_validation_values;
pub use self::data_validation_values::*;

Expand Down
179 changes: 179 additions & 0 deletions src/structs/table.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
use quick_xml::{
events::{BytesStart, Event},
Reader,
};

use super::coordinate::*;
use crate::helper::coordinate::*;
//use reader::driver::*;

#[derive(Clone, Default, Debug)]
pub struct Table {
name: String,
area: (Coordinate, Coordinate),
display_name: String,
columns: Vec<TableColumn>,
style_info: Option<TableStyleInfo>,
}
impl Table {
pub fn new<T>(name: &str, area: (T, T)) -> Self
where
T: Into<CellCoordinates>,
{
let coord_beg = Self::cell_coord_to_coord(area.0);
let coord_end = Self::cell_coord_to_coord(area.1);
let name = name.to_string();
Self {
area: (coord_beg, coord_end),
name: name.clone(),
display_name: name,
columns: Vec::<TableColumn>::default(),
style_info: None,
}
}

pub fn is_ok(&self) -> bool {
if self.name.is_empty() || self.display_name.is_empty() {
return false;
}
if self.area.0.get_col_num() == &0
|| self.area.0.get_row_num() == &0
|| self.area.1.get_col_num() == &0
|| self.area.1.get_row_num() == &0
|| self.area.0.get_col_num() > self.area.1.get_col_num()
|| self.area.0.get_row_num() > self.area.1.get_row_num()
{
return false;
}
return true;
}

pub fn get_name(&self) -> &String {
&self.name
}

pub fn set_name(&mut self, name: &str) {
self.name = name.to_string();
if self.display_name.is_empty() {
self.display_name = name.to_string();
}
}

pub fn get_display_name(&self) -> &String {
&self.display_name
}

pub fn set_display_name(&mut self, display_name: &str) {
self.display_name = display_name.to_string();
}

pub fn get_area(&self) -> &(Coordinate, Coordinate) {
&self.area
}

pub fn set_area<T>(&mut self, area: (T, T))
where
T: Into<CellCoordinates>,
{
let coord_beg = Self::cell_coord_to_coord(area.0);
let coord_end = Self::cell_coord_to_coord(area.1);
self.area = (coord_beg, coord_end);
}

pub fn add_column(&mut self, col: TableColumn) {
self.columns.push(col);
}

pub fn get_columns(&self) -> &Vec<TableColumn> {
&self.columns
}

pub fn has_style_info(&self) -> bool {
self.style_info.is_some()
}

pub fn get_style_info(&self) -> Option<&TableStyleInfo> {
self.style_info.as_ref()
}

pub fn set_style_info(&mut self, style_info: Option<TableStyleInfo>) {
self.style_info = style_info;
}

fn cell_coord_to_coord<T>(cc: T) -> Coordinate
where
T: Into<CellCoordinates>,
{
let cell_coord: CellCoordinates = cc.into();
let mut coord: Coordinate = Default::default();
coord.set_col_num(cell_coord.col);
coord.set_row_num(cell_coord.row);
coord
}
}

#[derive(Clone, Default, Debug)]
pub struct TableColumn {
name: String,
}
impl TableColumn {
pub fn new(name: &str) -> Self {
Self {
name: name.to_string(),
}
}

pub fn get_name(&self) -> &String {
&self.name
}

pub fn set_name(&mut self, name: String) {
self.name = name;
}
}

#[derive(Clone, Default, Debug)]
pub struct TableStyleInfo {
name: String,
show_first_col: bool,
show_last_col: bool,
show_row_stripes: bool,
show_col_stripes: bool,
}
impl TableStyleInfo {
pub fn new(
name: &str,
show_first_col: bool,
show_last_col: bool,
show_row_stripes: bool,
show_col_stripes: bool,
) -> Self {
Self {
name: name.to_string(),
show_first_col,
show_last_col,
show_row_stripes,
show_col_stripes,
}
}

pub fn get_name(&self) -> &String {
&self.name
}

pub fn is_show_first_col(&self) -> bool {
self.show_first_col
}

pub fn is_show_last_col(&self) -> bool {
self.show_last_col
}

pub fn is_show_row_stripes(&self) -> bool {
self.show_row_stripes
}

pub fn is_show_col_stripes(&self) -> bool {
self.show_col_stripes
}
}
Loading

0 comments on commit 844652a

Please sign in to comment.