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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Copyright 2018 Martin Billinger
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! # ARFF
//!
//! An ARFF (Attribute-Relation File Format) file is an ASCII text file
//! that describes a list of instances sharing a set of attributes. Its
//! main use is in data science to store tabular data: each row is an
//! instance and each column is an attribute. In addition it contains
//! meta-data such as attribute (column) names, data types, and comments.
//!
//! ## Usage
//! - ARFF is used as an input file format by the machine-learning tool Weka.
//! - The [OpenML website](https://www.openml.org/) provides data sets in
//!   ARFF and CSV formats.
//!
//! The ARFF crate utilizes the power of Serde to allow serialization and
//! deserialization of certain Rust types. The file format is relatively
//! simple, so not all rust types are supported. As a general rule of thumb,
//! data needs to be represented as a sequence of rows, and a row can be
//! either a `struct` with named columns or a sequence with static length.
//!
//! ## Example
//!
//! ```rust
//! extern crate arff;
//!
//! #[macro_use]
//! extern crate serde_derive;
//!
//! fn main() {
//!     let input = "
//! @RELATION Data
//! @ATTRIBUTE a NUMERIC
//! @ATTRIBUTE b NUMERIC
//!
//! @DATA
//! 42, 9
//! 7, 5";
//!
//!     #[derive(Debug, Deserialize)]
//!     struct NamedRow {
//!         b: i32,  // order of fields does not matter
//!         a: i32,
//!     }
//!
//!     let named_data: Vec<NamedRow> = arff::from_str(input).unwrap();
//!     println!("{:?}", named_data);
//!
//!     let unnamed_data: Vec<[i32; 2]> = arff::from_str(input).unwrap();
//!     println!("{:?}", unnamed_data);
//! }
//! ```

extern crate num_traits;
extern crate serde;

#[cfg(test)]
#[macro_use]
extern crate serde_derive;

mod de;
pub mod dynamic;
mod error;
mod parser;
mod ser;

pub use de::{flat_from_str, from_str, Deserializer};
pub use error::{Error, Result};
pub use ser::{to_string, Serializer};

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn roundtrip_1() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct Row {
            a: i16,
            b: f32,
            c: String,
        }

        let orig = vec![
            Row {
                a: 0,
                b: 0.0,
                c: String::new(),
            },
            Row {
                a: 1,
                b: 2.0,
                c: "123".to_owned(),
            },
            Row {
                a: -1726,
                b: 3.1415,
                c: "pie".to_owned(),
            },
        ];

        let arff = to_string(&orig).unwrap();
        let deser: Vec<Row> = from_str(&arff).unwrap();

        assert_eq!(deser, orig);
    }

    #[test]
    fn roundtrip_2() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct Row {
            a: i16,
            b: f32,
            c: String,
        }

        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct MyData(Vec<Row>);

        let input = "@RELATION MyData

@ATTRIBUTE a NUMERIC
@ATTRIBUTE b NUMERIC
@ATTRIBUTE c STRING

@DATA
0, 0, ''
1, 2, '123'
-1726, 3.1414999961853027, 'pie'
";

        let data: MyData = from_str(input).unwrap();
        let output = to_string(&data).unwrap();

        assert_eq!(input, output);
    }

    #[test]
    fn roundtrip_3() {
        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        enum Answer {
            Yes,
            No,
            Maybe,
            Dunno,
        }

        #[derive(Debug, Serialize, Deserialize, PartialEq)]
        struct Row {
            x: f32,
            class: Answer,
        }

        let orig = vec![
            Row {
                x: -1.0,
                class: Answer::No,
            },
            Row {
                x: 0.0,
                class: Answer::Maybe,
            },
            Row {
                x: 1.0,
                class: Answer::Yes,
            },
        ];

        let arff = to_string(&orig).unwrap();
        let deser: Vec<Row> = from_str(&arff).unwrap();

        assert_eq!(deser, orig);
    }

    #[test]
    fn roundtrip_4() {
        type Row = [[i32; 2]; 2];

        let orig = vec![[[1, 2], [3, 4]], [[1, 3], [2, 4]]];

        let arff = to_string(&orig).unwrap();
        let deser: Vec<Row> = from_str(&arff).unwrap();

        assert_eq!(deser, orig);
    }

    #[test]
    fn roundtrip_5() {
        type Row = (i32, [u8; 2], i32);

        let orig = vec![(1, [2, 3], 4), (5, [6, 7], 8)];

        let arff = to_string(&orig).unwrap();
        let deser: Vec<Row> = from_str(&arff).unwrap();

        assert_eq!(deser, orig);
    }

    #[test]
    fn type_ser_support_outer() {
        type Row = [i32; 1];

        let d_tuple: (Row, Row) = ([1], [2]);
        let d_array: [Row; 2] = [[1], [2]];
        let d_vec: Vec<Row> = d_array.to_vec();
        let d_slice: &[Row] = d_array.as_ref();

        assert_eq!(
            to_string(&d_tuple).unwrap(),
            format!(
                "@RELATION {}\n\n@ATTRIBUTE col1 NUMERIC\n\n@DATA\n1\n2\n",
                "unnamed_data"
            )
        );
        assert_eq!(
            to_string(&d_array).unwrap(),
            format!(
                "@RELATION {}\n\n@ATTRIBUTE col1 NUMERIC\n\n@DATA\n1\n2\n",
                "unnamed_data"
            )
        );
        assert_eq!(
            to_string(&d_vec).unwrap(),
            format!(
                "@RELATION {}\n\n@ATTRIBUTE col1 NUMERIC\n\n@DATA\n1\n2\n",
                "unnamed_data"
            )
        );
        assert_eq!(
            to_string(&d_slice).unwrap(),
            format!(
                "@RELATION {}\n\n@ATTRIBUTE col1 NUMERIC\n\n@DATA\n1\n2\n",
                "unnamed_data"
            )
        );

        #[derive(Serialize, Deserialize)]
        struct NewtypeStruct(Vec<Row>);
        let d_newtype_struct = NewtypeStruct(vec![[1], [2]]);
        assert_eq!(
            to_string(&d_newtype_struct).unwrap(),
            format!(
                "@RELATION {}\n\n@ATTRIBUTE col1 NUMERIC\n\n@DATA\n1\n2\n",
                "NewtypeStruct"
            )
        );

        #[derive(Serialize, Deserialize)]
        struct TupleStruct(Row, Row);
        let d_tuple_struct = TupleStruct([1], [2]);
        assert_eq!(
            to_string(&d_tuple_struct).unwrap(),
            format!(
                "@RELATION {}\n\n@ATTRIBUTE col1 NUMERIC\n\n@DATA\n1\n2\n",
                "TupleStruct"
            )
        );
    }

    #[test]
    fn type_ser_support_inner() {
        #[derive(Serialize)]
        struct StructRow {
            x: f64,
            y: i32,
        };

        let d_struct = [StructRow { x: 1.1, y: 2 }];
        let d_tuple: [(f64, i32); 1] = [(1.1, 2)];
        let d_array: [[f64; 2]; 1] = [[1.1, 2.0]];

        assert_eq!(to_string(&d_struct).unwrap(), "@RELATION unnamed_data\n\n@ATTRIBUTE x NUMERIC\n@ATTRIBUTE y NUMERIC\n\n@DATA\n1.1, 2\n");
        assert_eq!(to_string(&d_tuple).unwrap(), "@RELATION unnamed_data\n\n@ATTRIBUTE col1 NUMERIC\n@ATTRIBUTE col2 NUMERIC\n\n@DATA\n1.1, 2\n");
        assert_eq!(to_string(&d_array).unwrap(), "@RELATION unnamed_data\n\n@ATTRIBUTE col1 NUMERIC\n@ATTRIBUTE col2 NUMERIC\n\n@DATA\n1.1, 2\n");
    }
}