fit_kit 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -1
- data/Cargo.lock +11 -1
- data/README.md +1 -1
- data/ext/fit_kit/Cargo.toml +1 -0
- data/ext/fit_kit/src/lib.rs +31 -40
- data/lib/fit_kit/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3fb71b8237876ebc05273a7cb9c78306424ad04ba4c1505f8bc70781cf2c35c2
|
4
|
+
data.tar.gz: 5ac086bb3a7b95729c74ad69c6818495ef3111b0cbf062b3f0beca072bcd53ff
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f56f45e3911e4ba9cfab9bf1acb57b53f58e06cf3abae892dc3ad5f58d48c10ba8ec54a895056e0612295b32815db838ccb7f01428d9558aeac190fd6ca3ba6a
|
7
|
+
data.tar.gz: 4544ae74e7bc2510f4bcff6826ecc08850e9c01f6bdf0c6e7f95873c2e80753989bbc471855cfd72f6293946016d682223d33e64816310fe517b38ee1b4c7d4c
|
data/CHANGELOG.md
CHANGED
data/Cargo.lock
CHANGED
@@ -41,7 +41,7 @@ dependencies = [
|
|
41
41
|
"bitflags",
|
42
42
|
"cexpr",
|
43
43
|
"clang-sys",
|
44
|
-
"itertools",
|
44
|
+
"itertools 0.12.1",
|
45
45
|
"lazy_static",
|
46
46
|
"lazycell",
|
47
47
|
"proc-macro2",
|
@@ -131,6 +131,7 @@ name = "fit_kit"
|
|
131
131
|
version = "0.1.0"
|
132
132
|
dependencies = [
|
133
133
|
"fitparser",
|
134
|
+
"itertools 0.13.0",
|
134
135
|
"magnus",
|
135
136
|
]
|
136
137
|
|
@@ -183,6 +184,15 @@ dependencies = [
|
|
183
184
|
"either",
|
184
185
|
]
|
185
186
|
|
187
|
+
[[package]]
|
188
|
+
name = "itertools"
|
189
|
+
version = "0.13.0"
|
190
|
+
source = "registry+https://github.com/rust-lang/crates.io-index"
|
191
|
+
checksum = "413ee7dfc52ee1a4949ceeb7dbc8a33f2d6c088194d9f922fb8318faf1f01186"
|
192
|
+
dependencies = [
|
193
|
+
"either",
|
194
|
+
]
|
195
|
+
|
186
196
|
[[package]]
|
187
197
|
name = "js-sys"
|
188
198
|
version = "0.3.72"
|
data/README.md
CHANGED
@@ -17,7 +17,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
17
17
|
```ruby
|
18
18
|
test_fit_file = File.join(Dir.pwd, "example.fit")
|
19
19
|
fit_data_records = ::FitKit.parse_fit_file(test_fit_file)
|
20
|
-
# [
|
20
|
+
# { record: [{...}, {...}], session: [{...}], lap: [..], activity: [...] }
|
21
21
|
```
|
22
22
|
|
23
23
|
## Performance
|
data/ext/fit_kit/Cargo.toml
CHANGED
data/ext/fit_kit/src/lib.rs
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
use fitparser::{self, FitDataRecord, Value};
|
2
|
-
use
|
2
|
+
use itertools::Itertools;
|
3
|
+
use magnus::{function, prelude::*, Error, IntoValue, RArray, RHash, Ruby, Symbol};
|
3
4
|
use std::fs::File;
|
4
5
|
|
5
6
|
// recursive method to turn Fit value into magnus::Value
|
@@ -33,43 +34,22 @@ fn value_to_rb_value(value: &Value) -> magnus::Value {
|
|
33
34
|
}
|
34
35
|
}
|
35
36
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
let hash = RHash::new();
|
47
|
-
for field in self.0.fields() {
|
48
|
-
let value = value_to_rb_value(field.value());
|
49
|
-
let pair = RHash::new();
|
50
|
-
pair.aset(Symbol::new("units"), field.units()).unwrap();
|
51
|
-
pair.aset(Symbol::new("value"), value).unwrap();
|
52
|
-
// here we add the stuff to the hash
|
53
|
-
let field_name_symbol = Symbol::new(field.name());
|
54
|
-
hash.aset(field_name_symbol, pair).unwrap();
|
55
|
-
}
|
56
|
-
|
57
|
-
hash
|
37
|
+
fn get_fields_hash(record: &FitDataRecord) -> RHash {
|
38
|
+
let hash = RHash::new();
|
39
|
+
for field in record.fields() {
|
40
|
+
let value = value_to_rb_value(field.value());
|
41
|
+
let pair = RHash::new();
|
42
|
+
pair.aset(Symbol::new("units"), field.units()).unwrap();
|
43
|
+
pair.aset(Symbol::new("value"), value).unwrap();
|
44
|
+
// here we add the stuff to the hash
|
45
|
+
let field_name_symbol = Symbol::new(field.name());
|
46
|
+
hash.aset(field_name_symbol, pair).unwrap();
|
58
47
|
}
|
59
|
-
}
|
60
|
-
|
61
|
-
// Here we define two ruby classes
|
62
|
-
// RFitDataRecord and RFitDataField
|
63
|
-
fn define_ruby_classes(ruby: &Ruby) -> Result<(), magnus::Error> {
|
64
|
-
// definie the the other one here
|
65
|
-
let data_record_class = ruby.define_class("RFitDataRecord", ruby.class_object())?;
|
66
|
-
data_record_class.define_method("kind", method!(RFitDataRecord::kind, 0))?;
|
67
|
-
data_record_class.define_method("fields_hash", method!(RFitDataRecord::fields_hash, 0))?;
|
68
48
|
|
69
|
-
|
49
|
+
hash
|
70
50
|
}
|
71
51
|
|
72
|
-
fn parse_fit_file(file_path: String) -> Result<
|
52
|
+
fn parse_fit_file(file_path: String) -> Result<RHash, magnus::Error> {
|
73
53
|
let mut fp = File::open(file_path)
|
74
54
|
.map_err(|e| Error::new(Ruby::get().unwrap().exception_io_error(), e.to_string()))?;
|
75
55
|
let data = fitparser::from_reader(&mut fp).map_err(|e| {
|
@@ -79,19 +59,30 @@ fn parse_fit_file(file_path: String) -> Result<RArray, magnus::Error> {
|
|
79
59
|
)
|
80
60
|
})?;
|
81
61
|
|
82
|
-
//
|
83
|
-
let
|
84
|
-
for
|
85
|
-
|
62
|
+
// now let's group by the record by kind
|
63
|
+
let result_hash = RHash::new();
|
64
|
+
for (kind, kind_records) in data
|
65
|
+
.iter()
|
66
|
+
.chunk_by(|record| record.kind().to_string())
|
67
|
+
.into_iter()
|
68
|
+
{
|
69
|
+
// turn records into rarray
|
70
|
+
let array = RArray::new();
|
71
|
+
for record in kind_records {
|
72
|
+
// TODO here do not pass RFitDataRecord
|
73
|
+
// turn it into fields_hash directly
|
74
|
+
array.push(get_fields_hash(record)).unwrap();
|
75
|
+
}
|
76
|
+
|
77
|
+
result_hash.aset(Symbol::new(kind), array).unwrap();
|
86
78
|
}
|
87
79
|
|
88
|
-
Ok(
|
80
|
+
Ok(result_hash)
|
89
81
|
}
|
90
82
|
|
91
83
|
#[magnus::init]
|
92
84
|
fn init(ruby: &Ruby) -> Result<(), Error> {
|
93
85
|
let module = ruby.define_module("FitKit")?;
|
94
|
-
let _ = define_ruby_classes(&ruby);
|
95
86
|
|
96
87
|
module.define_singleton_method("parse_fit_file", function!(parse_fit_file, 1))?;
|
97
88
|
|
data/lib/fit_kit/version.rb
CHANGED