Skip to content

Commit daf5884

Browse files
author
robin
committed
reformat code
1 parent 140d78a commit daf5884

File tree

9 files changed

+164
-195
lines changed

9 files changed

+164
-195
lines changed

examples/readdocx.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,14 @@
1818
* IN THE SOFTWARE.
1919
*
2020
*/
21-
22-
2321
extern crate dotext;
2422

2523
use dotext::*;
2624

27-
use std::io::Read;
2825
use std::env;
26+
use std::io::Read;
2927

30-
31-
fn main(){
32-
28+
fn main() {
3329
if let Some(path) = env::args().nth(1) {
3430
let mut file = Docx::open(path).expect("Cannot open file");
3531
let mut isi = String::new();
@@ -38,9 +34,7 @@ fn main(){
3834
println!("----------BEGIN----------");
3935
println!("{}", isi);
4036
println!("----------EOF----------");
41-
}else{
37+
} else {
4238
println!("USAGE: readdocx [PATH-TO-DOCX-FILE]");
4339
}
44-
45-
4640
}

src/doc.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
1-
2-
3-
41
use zip::ZipArchive;
52

6-
use xml::reader::Reader;
73
use xml::events::Event;
4+
use xml::reader::Reader;
85

9-
use std::path::{Path, PathBuf};
6+
use std::clone::Clone;
107
use std::fs::File;
11-
use std::io::prelude::*;
128
use std::io;
13-
use std::clone::Clone;
9+
use std::io::prelude::*;
10+
use std::path::{Path, PathBuf};
1411
use zip::read::ZipFile;
1512

1613
pub trait HasKind {
17-
1814
// kind
1915
fn kind(&self) -> &'static str;
2016

@@ -30,18 +26,21 @@ pub trait OpenOfficeDoc<T>: Read + HasKind {
3026
fn open<P: AsRef<Path>>(path: P) -> io::Result<T>;
3127
}
3228

33-
34-
pub(crate) fn open_doc_read_data<P: AsRef<Path>>(path: P, content_name:&str, tags:&[&str]) -> io::Result<String> {
29+
pub(crate) fn open_doc_read_data<P: AsRef<Path>>(
30+
path: P,
31+
content_name: &str,
32+
tags: &[&str],
33+
) -> io::Result<String> {
3534
let file = File::open(path.as_ref())?;
3635
let mut archive = ZipArchive::new(file)?;
3736

3837
let mut xml_data = String::new();
3938

40-
for i in 0..archive.len(){
39+
for i in 0..archive.len() {
4140
let mut c_file = archive.by_index(i).unwrap();
4241
if c_file.name() == content_name {
4342
c_file.read_to_string(&mut xml_data);
44-
break
43+
break;
4544
}
4645
}
4746

@@ -53,30 +52,34 @@ pub(crate) fn open_doc_read_data<P: AsRef<Path>>(path: P, content_name:&str, tag
5352
if xml_data.len() > 0 {
5453
let mut to_read = false;
5554
loop {
56-
match xml_reader.read_event(&mut buf){
55+
match xml_reader.read_event(&mut buf) {
5756
Ok(Event::Start(ref e)) => {
5857
for tag in tags {
59-
if e.name() == tag.as_bytes(){
58+
if e.name() == tag.as_bytes() {
6059
to_read = true;
6160
if e.name() == b"text:p" {
6261
txt.push("\n\n".to_string());
6362
}
6463
break;
6564
}
6665
}
67-
},
66+
}
6867
Ok(Event::Text(e)) => {
6968
if to_read {
7069
txt.push(e.unescape_and_decode(&xml_reader).unwrap());
7170
to_read = false;
7271
}
73-
},
72+
}
7473
Ok(Event::Eof) => break,
75-
Err(e) => panic!("Error at position {}: {:?}", xml_reader.buffer_position(), e),
74+
Err(e) => panic!(
75+
"Error at position {}: {:?}",
76+
xml_reader.buffer_position(),
77+
e
78+
),
7679
_ => (),
7780
}
7881
}
7982
}
8083

8184
Ok(txt.join(""))
82-
}
85+
}

src/docx.rs

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
21
use zip::ZipArchive;
32

4-
use xml::reader::Reader;
53
use xml::events::Event;
4+
use xml::reader::Reader;
65

7-
use std::path::{Path, PathBuf};
6+
use std::clone::Clone;
87
use std::fs::File;
8+
use std::io;
99
use std::io::prelude::*;
1010
use std::io::Cursor;
11-
use std::io;
12-
use std::clone::Clone;
11+
use std::path::{Path, PathBuf};
1312
use zip::read::ZipFile;
1413

15-
use doc::{MsDoc, HasKind};
14+
use doc::{HasKind, MsDoc};
1615

1716
pub struct Docx {
1817
path: PathBuf,
19-
data: Cursor<String>
18+
data: Cursor<String>,
2019
}
2120

2221
impl HasKind for Docx {
@@ -30,18 +29,17 @@ impl HasKind for Docx {
3029
}
3130

3231
impl MsDoc<Docx> for Docx {
33-
3432
fn open<P: AsRef<Path>>(path: P) -> io::Result<Docx> {
3533
let file = File::open(path.as_ref())?;
3634
let mut archive = ZipArchive::new(file)?;
3735

3836
let mut xml_data = String::new();
3937

40-
for i in 0..archive.len(){
38+
for i in 0..archive.len() {
4139
let mut c_file = archive.by_index(i).unwrap();
4240
if c_file.name() == "word/document.xml" {
4341
c_file.read_to_string(&mut xml_data);
44-
break
42+
break;
4543
}
4644
}
4745

@@ -53,36 +51,36 @@ impl MsDoc<Docx> for Docx {
5351
if xml_data.len() > 0 {
5452
let mut to_read = false;
5553
loop {
56-
match xml_reader.read_event(&mut buf){
57-
Ok(Event::Start(ref e)) => {
58-
match e.name() {
59-
b"w:p" => {
60-
to_read = true;
61-
txt.push("\n\n".to_string());
62-
},
63-
b"w:t" => to_read = true,
64-
_ => (),
54+
match xml_reader.read_event(&mut buf) {
55+
Ok(Event::Start(ref e)) => match e.name() {
56+
b"w:p" => {
57+
to_read = true;
58+
txt.push("\n\n".to_string());
6559
}
60+
b"w:t" => to_read = true,
61+
_ => (),
6662
},
6763
Ok(Event::Text(e)) => {
6864
if to_read {
6965
txt.push(e.unescape_and_decode(&xml_reader).unwrap());
7066
to_read = false;
7167
}
72-
},
68+
}
7369
Ok(Event::Eof) => break, // exits the loop when reaching end of file
74-
Err(e) => panic!("Error at position {}: {:?}", xml_reader.buffer_position(), e),
70+
Err(e) => panic!(
71+
"Error at position {}: {:?}",
72+
xml_reader.buffer_position(),
73+
e
74+
),
7575
_ => (),
7676
}
7777
}
7878
}
7979

80-
Ok(
81-
Docx {
82-
path: path.as_ref().to_path_buf(),
83-
data: Cursor::new(txt.join(""))
84-
}
85-
)
80+
Ok(Docx {
81+
path: path.as_ref().to_path_buf(),
82+
data: Cursor::new(txt.join("")),
83+
})
8684
}
8785
}
8886

@@ -92,19 +90,18 @@ impl Read for Docx {
9290
}
9391
}
9492

95-
9693
#[cfg(test)]
9794
mod tests {
98-
use std::path::{Path, PathBuf};
9995
use super::*;
96+
use std::path::{Path, PathBuf};
10097

10198
#[test]
102-
fn instantiate(){
99+
fn instantiate() {
103100
let _ = Docx::open(Path::new("samples/filosofi-logo.docx"));
104101
}
105102

106103
#[test]
107-
fn read(){
104+
fn read() {
108105
let mut f = Docx::open(Path::new("samples/filosofi-logo.docx")).unwrap();
109106

110107
let mut data = String::new();

src/lib.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#![allow(unused_imports, dead_code, unused_must_use)]
22

3+
extern crate quick_xml as xml;
34
/**
45
* Copyright 2017 Robin Syihab. All rights reserved.
56
*
@@ -20,24 +21,20 @@
2021
* IN THE SOFTWARE.
2122
*
2223
*/
23-
24-
2524
extern crate zip;
26-
extern crate quick_xml as xml;
27-
2825

2926
pub mod doc;
3027
pub mod docx;
31-
pub mod xlsx;
32-
pub mod pptx;
33-
pub mod odt;
3428
pub mod odp;
3529
pub mod ods;
30+
pub mod odt;
31+
pub mod pptx;
32+
pub mod xlsx;
3633

3734
pub use doc::MsDoc;
3835
pub use docx::Docx;
39-
pub use xlsx::Xlsx;
40-
pub use pptx::Pptx;
41-
pub use odt::Odt;
4236
pub use odp::Odp;
4337
pub use ods::Ods;
38+
pub use odt::Odt;
39+
pub use pptx::Pptx;
40+
pub use xlsx::Xlsx;

src/odp.rs

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,22 @@
1-
21
use zip::ZipArchive;
32

4-
use xml::reader::Reader;
53
use xml::events::Event;
4+
use xml::reader::Reader;
65

7-
use std::path::{Path, PathBuf};
6+
use std::clone::Clone;
87
use std::fs::File;
8+
use std::io;
99
use std::io::prelude::*;
1010
use std::io::Cursor;
11-
use std::io;
12-
use std::clone::Clone;
11+
use std::path::{Path, PathBuf};
1312
use zip::read::ZipFile;
1413

1514
use doc;
16-
use doc::{OpenOfficeDoc, HasKind};
15+
use doc::{HasKind, OpenOfficeDoc};
1716

1817
pub struct Odp {
1918
path: PathBuf,
20-
data: Cursor<String>
19+
data: Cursor<String>,
2120
}
2221

2322
impl HasKind for Odp {
@@ -31,7 +30,6 @@ impl HasKind for Odp {
3130
}
3231

3332
impl OpenOfficeDoc<Odp> for Odp {
34-
3533
fn open<P: AsRef<Path>>(path: P) -> io::Result<Odp> {
3634
let text = doc::open_doc_read_data(path.as_ref(), "content.xml", &["text:p", "text:span"])?;
3735

@@ -82,12 +80,10 @@ impl OpenOfficeDoc<Odp> for Odp {
8280
// }
8381
// }
8482

85-
Ok(
86-
Odp {
87-
path: path.as_ref().to_path_buf(),
88-
data: Cursor::new(text)
89-
}
90-
)
83+
Ok(Odp {
84+
path: path.as_ref().to_path_buf(),
85+
data: Cursor::new(text),
86+
})
9187
}
9288
}
9389

@@ -97,19 +93,18 @@ impl Read for Odp {
9793
}
9894
}
9995

100-
10196
#[cfg(test)]
10297
mod tests {
103-
use std::path::{Path, PathBuf};
10498
use super::*;
99+
use std::path::{Path, PathBuf};
105100

106101
#[test]
107-
fn instantiate(){
102+
fn instantiate() {
108103
let _ = Odp::open(Path::new("samples/sample.odp"));
109104
}
110105

111106
#[test]
112-
fn read(){
107+
fn read() {
113108
let mut f = Odp::open(Path::new("samples/sample.odp")).unwrap();
114109

115110
let mut data = String::new();

0 commit comments

Comments
 (0)