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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f5d819105551844f5a7e45a8399aa60b403cbc5b8b2adb278c0fb9a8a29631c
4
- data.tar.gz: 1a55ae0af1a09860935dc3dee596a191c42133ae2a070c7c788514e4b4694248
3
+ metadata.gz: 3fb71b8237876ebc05273a7cb9c78306424ad04ba4c1505f8bc70781cf2c35c2
4
+ data.tar.gz: 5ac086bb3a7b95729c74ad69c6818495ef3111b0cbf062b3f0beca072bcd53ff
5
5
  SHA512:
6
- metadata.gz: 46cd84be49270a1197f815f776cc51627ba58d7abf72d8f0f4b6d80cb9f485ceca1a73745eb62df2696fe2a0cd3b78e2311550bf3f711dfcaf133daeed3abf1e
7
- data.tar.gz: 32993953f6611a2ad85a70426be7525bb558e3cdf201e994e6848d04ab29f8ebdee3e1d722ac432173f05e799196f47a8f38e3d73c6f151c7de83497cdfa3463
6
+ metadata.gz: f56f45e3911e4ba9cfab9bf1acb57b53f58e06cf3abae892dc3ad5f58d48c10ba8ec54a895056e0612295b32815db838ccb7f01428d9558aeac190fd6ca3ba6a
7
+ data.tar.gz: 4544ae74e7bc2510f4bcff6826ecc08850e9c01f6bdf0c6e7f95873c2e80753989bbc471855cfd72f6293946016d682223d33e64816310fe517b38ee1b4c7d4c
data/CHANGELOG.md CHANGED
@@ -1,4 +1,10 @@
1
- ## [Unreleased]
1
+ ## [0.3.0] - 2024-10-16
2
+
3
+ - Simplify to return hash for the parsing
4
+
5
+ ## [0.2.0] - 2024-10-15
6
+
7
+ - Simplify to return hash fileds for records
2
8
 
3
9
  ## [0.1.0] - 2024-10-14
4
10
 
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
- # [RFitDataRecord, RFitDataRecord, RFitDataRecord ...]
20
+ # { record: [{...}, {...}], session: [{...}], lap: [..], activity: [...] }
21
21
  ```
22
22
 
23
23
  ## Performance
@@ -11,4 +11,5 @@ crate-type = ["cdylib"]
11
11
 
12
12
  [dependencies]
13
13
  fitparser = "0.7.0"
14
+ itertools = "0.13.0"
14
15
  magnus = { version = "0.6.2" }
@@ -1,5 +1,6 @@
1
1
  use fitparser::{self, FitDataRecord, Value};
2
- use magnus::{function, method, prelude::*, Error, IntoValue, RArray, RHash, Ruby, Symbol};
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
- ///////////////////////// RFitDataRecord ///////////////////////////
37
- #[magnus::wrap(class = "RFitDataRecord")]
38
- struct RFitDataRecord(FitDataRecord);
39
-
40
- impl RFitDataRecord {
41
- fn kind(&self) -> String {
42
- self.0.kind().to_string()
43
- }
44
-
45
- fn fields_hash(&self) -> RHash {
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
- Ok(())
49
+ hash
70
50
  }
71
51
 
72
- fn parse_fit_file(file_path: String) -> Result<RArray, magnus::Error> {
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
- // finally we have the result array of record
83
- let array = RArray::new();
84
- for record in data {
85
- array.push(RFitDataRecord(record)).unwrap();
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(array)
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
 
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FitKit
4
- VERSION = "0.2.0"
4
+ VERSION = "0.3.0"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fit_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - 29decibel