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
#[macro_use] extern crate enum_primitive_derive;
#[macro_use] extern crate nom;
extern crate num_traits;
extern crate byteorder;
extern crate data_buffer as buffer;
#[macro_use]
pub mod basic;
pub mod parser;
pub mod writer;
pub mod model;
use std::io;
use std::path::Path;
pub type IOBuffer = buffer::DataBuffer;
#[derive(Debug)]
pub enum Error {
IO(io::Error),
Parse(nom::ErrorKind<u32>),
Unknown,
}
impl From<io::Error> for Error {
fn from(e: io::Error) -> Error {
Error::IO(e)
}
}
impl From<Error> for io::Error {
fn from(err: Error) -> io::Error {
match err {
Error::IO(e) => e,
_ => io::Error::new(io::ErrorKind::Other, format!("{:?}", err)),
}
}
}
fn import_impl<F>(file_path: &Path, parse: F) -> Result<model::Vtk, Error>
where F: Fn(&[u8]) -> nom::IResult<&[u8], model::Vtk>
{
use std::fs::File;
use io::Read;
use nom::IResult;
let mut file = File::open(file_path)?;
let mut buf = Vec::new();
file.read_to_end(&mut buf)?;
match parse(&buf) {
IResult::Done(_, vtk) => Ok(vtk),
IResult::Error(e) => Err(Error::Parse(e.into_error_kind())),
IResult::Incomplete(_) => Err(Error::Unknown),
}
}
pub fn import(file_path: &Path) -> Result<model::Vtk, Error> {
import_impl(file_path, parser::parse)
}
pub fn import_le(file_path: &Path) -> Result<model::Vtk, Error> {
import_impl(file_path, parser::parse_le)
}
pub fn import_be(file_path: &Path) -> Result<model::Vtk, Error> {
import_impl(file_path, parser::parse_be)
}
pub fn export(data: model::Vtk, file_path: &Path) -> Result<(), Error> {
use std::fs::File;
use io::Write;
use writer::WriteVtk;
let mut file = File::create(file_path)?;
file.write_all(Vec::<u8>::new().write_vtk(data).as_slice())?;
Ok(())
}
pub fn export_ascii(data: model::Vtk, file_path: &Path) -> Result<(), Error> {
use std::fs::File;
use io::Write;
use writer::WriteVtk;
let mut file = File::create(file_path)?;
file.write_all(String::new().write_vtk(data).as_bytes())?;
Ok(())
}