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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#[macro_use]
extern crate enum_primitive_derive;
#[macro_use]
extern crate nom;
extern crate byteorder;
extern crate data_buffer as buffer;
extern crate num_traits;
#[macro_use]
pub mod basic;
pub mod model;
pub mod parser;
pub mod writer;
use std::io;
use std::path::Path;
pub use buffer::call_numeric_buffer_fn;
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 io::Read;
use nom::IResult;
use std::fs::File;
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 io::Write;
use writer::WriteVtk;
let mut file = ::std::fs::File::create(file_path)?;
file.write_all(Vec::<u8>::new().write_vtk(data).as_slice())?;
Ok(())
}
pub fn export_le(data: model::Vtk, file_path: &Path) -> Result<(), Error> {
use io::Write;
use writer::WriteVtk;
let mut file = ::std::fs::File::create(file_path)?;
file.write_all(Vec::<u8>::new().write_vtk_le(data).as_slice())?;
Ok(())
}
pub fn export_be(data: model::Vtk, file_path: &Path) -> Result<(), Error> {
use io::Write;
use writer::WriteVtk;
let mut file = ::std::fs::File::create(file_path)?;
file.write_all(Vec::<u8>::new().write_vtk_be(data).as_slice())?;
Ok(())
}
pub fn export_ascii(data: model::Vtk, file_path: &Path) -> Result<(), Error> {
use io::Write;
use std::fs::File;
use writer::WriteVtk;
let mut out_str = String::new();
out_str.write_vtk(data);
{
let mut file = File::create(file_path)?;
file.write_all(out_str.as_bytes())?;
}
Ok(())
}