-
Notifications
You must be signed in to change notification settings - Fork 156
/
auto.rs
163 lines (150 loc) · 5.82 KB
/
auto.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! A module to convert file extension to reader
use crate::errors::Error;
use crate::vba::VbaProject;
use crate::{
open_workbook, open_workbook_from_rs, Data, DataRef, Metadata, Ods, Range, Reader, ReaderRef,
Xls, Xlsb, Xlsx,
};
use std::borrow::Cow;
use std::fs::File;
use std::io::BufReader;
use std::path::Path;
/// A wrapper over all sheets when the file type is not known at static time
pub enum Sheets<RS> {
/// Xls reader
Xls(Xls<RS>),
/// Xlsx reader
Xlsx(Xlsx<RS>),
/// Xlsb reader
Xlsb(Xlsb<RS>),
/// Ods reader
Ods(Ods<RS>),
}
/// Opens a workbook and define the file type at runtime.
///
/// Whenever possible use the statically known `open_workbook` function instead
pub fn open_workbook_auto<P>(path: P) -> Result<Sheets<BufReader<File>>, Error>
where
P: AsRef<Path>,
{
let path = path.as_ref();
Ok(match path.extension().and_then(|e| e.to_str()) {
Some("xls") | Some("xla") => Sheets::Xls(open_workbook(path).map_err(Error::Xls)?),
Some("xlsx") | Some("xlsm") | Some("xlam") => {
Sheets::Xlsx(open_workbook(path).map_err(Error::Xlsx)?)
}
Some("xlsb") => Sheets::Xlsb(open_workbook(path).map_err(Error::Xlsb)?),
Some("ods") => Sheets::Ods(open_workbook(path).map_err(Error::Ods)?),
_ => {
if let Ok(ret) = open_workbook::<Xls<_>, _>(path) {
return Ok(Sheets::Xls(ret));
} else if let Ok(ret) = open_workbook::<Xlsx<_>, _>(path) {
return Ok(Sheets::Xlsx(ret));
} else if let Ok(ret) = open_workbook::<Xlsb<_>, _>(path) {
return Ok(Sheets::Xlsb(ret));
} else if let Ok(ret) = open_workbook::<Ods<_>, _>(path) {
return Ok(Sheets::Ods(ret));
} else {
return Err(Error::Msg("Cannot detect file format"));
};
}
})
}
/// Opens a workbook from the given bytes.
///
/// Whenever possible use the statically known `open_workbook_from_rs` function instead
pub fn open_workbook_auto_from_rs<RS>(data: RS) -> Result<Sheets<RS>, Error>
where
RS: std::io::Read + std::io::Seek + Clone,
{
if let Ok(ret) = open_workbook_from_rs::<Xls<RS>, RS>(data.clone()) {
Ok(Sheets::Xls(ret))
} else if let Ok(ret) = open_workbook_from_rs::<Xlsx<RS>, RS>(data.clone()) {
Ok(Sheets::Xlsx(ret))
} else if let Ok(ret) = open_workbook_from_rs::<Xlsb<RS>, RS>(data.clone()) {
Ok(Sheets::Xlsb(ret))
} else if let Ok(ret) = open_workbook_from_rs::<Ods<RS>, RS>(data) {
Ok(Sheets::Ods(ret))
} else {
Err(Error::Msg("Cannot detect file format"))
}
}
impl<RS> Reader<RS> for Sheets<RS>
where
RS: std::io::Read + std::io::Seek,
{
type Error = Error;
/// Creates a new instance.
fn new(_reader: RS) -> Result<Self, Self::Error> {
Err(Error::Msg("Sheets must be created from a Path"))
}
/// Gets `VbaProject`
fn vba_project(&mut self) -> Option<Result<Cow<'_, VbaProject>, Self::Error>> {
match *self {
Sheets::Xls(ref mut e) => e.vba_project().map(|vba| vba.map_err(Error::Xls)),
Sheets::Xlsx(ref mut e) => e.vba_project().map(|vba| vba.map_err(Error::Xlsx)),
Sheets::Xlsb(ref mut e) => e.vba_project().map(|vba| vba.map_err(Error::Xlsb)),
Sheets::Ods(ref mut e) => e.vba_project().map(|vba| vba.map_err(Error::Ods)),
}
}
/// Initialize
fn metadata(&self) -> &Metadata {
match *self {
Sheets::Xls(ref e) => e.metadata(),
Sheets::Xlsx(ref e) => e.metadata(),
Sheets::Xlsb(ref e) => e.metadata(),
Sheets::Ods(ref e) => e.metadata(),
}
}
/// Read worksheet data in corresponding worksheet path
fn worksheet_range(&mut self, name: &str) -> Result<Range<Data>, Self::Error> {
match *self {
Sheets::Xls(ref mut e) => e.worksheet_range(name).map_err(Error::Xls),
Sheets::Xlsx(ref mut e) => e.worksheet_range(name).map_err(Error::Xlsx),
Sheets::Xlsb(ref mut e) => e.worksheet_range(name).map_err(Error::Xlsb),
Sheets::Ods(ref mut e) => e.worksheet_range(name).map_err(Error::Ods),
}
}
/// Read worksheet formula in corresponding worksheet path
fn worksheet_formula(&mut self, name: &str) -> Result<Range<String>, Self::Error> {
match *self {
Sheets::Xls(ref mut e) => e.worksheet_formula(name).map_err(Error::Xls),
Sheets::Xlsx(ref mut e) => e.worksheet_formula(name).map_err(Error::Xlsx),
Sheets::Xlsb(ref mut e) => e.worksheet_formula(name).map_err(Error::Xlsb),
Sheets::Ods(ref mut e) => e.worksheet_formula(name).map_err(Error::Ods),
}
}
fn worksheets(&mut self) -> Vec<(String, Range<Data>)> {
match *self {
Sheets::Xls(ref mut e) => e.worksheets(),
Sheets::Xlsx(ref mut e) => e.worksheets(),
Sheets::Xlsb(ref mut e) => e.worksheets(),
Sheets::Ods(ref mut e) => e.worksheets(),
}
}
#[cfg(feature = "picture")]
fn pictures(&self) -> Option<Vec<(String, Vec<u8>)>> {
match *self {
Sheets::Xls(ref e) => e.pictures(),
Sheets::Xlsx(ref e) => e.pictures(),
Sheets::Xlsb(ref e) => e.pictures(),
Sheets::Ods(ref e) => e.pictures(),
}
}
}
impl<RS> ReaderRef<RS> for Sheets<RS>
where
RS: std::io::Read + std::io::Seek,
{
fn worksheet_range_ref<'a>(
&'a mut self,
name: &str,
) -> Result<Range<DataRef<'a>>, Self::Error> {
match *self {
Sheets::Xlsx(ref mut e) => e.worksheet_range_ref(name).map_err(Error::Xlsx),
Sheets::Xlsb(ref mut e) => e.worksheet_range_ref(name).map_err(Error::Xlsb),
Sheets::Xls(_) => unimplemented!(),
Sheets::Ods(_) => unimplemented!(),
}
}
}