refix 0.3.0__tar.gz → 0.4.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. {refix-0.3.0 → refix-0.4.0}/Cargo.lock +2 -2
  2. {refix-0.3.0 → refix-0.4.0}/PKG-INFO +1 -1
  3. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/CHANGELOG.md +6 -0
  4. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/Cargo.toml +1 -1
  5. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/lib.rs +1 -1
  6. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/message.rs +42 -2
  7. refix-0.4.0/crates/refix-message/src/value/decimal.rs +232 -0
  8. refix-0.4.0/crates/refix-message/src/value.rs +5 -0
  9. {refix-0.3.0 → refix-0.4.0}/python/refix/_core.pyi +3 -0
  10. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/CHANGELOG.md +6 -0
  11. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/Cargo.toml +1 -1
  12. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/src/message.rs +27 -2
  13. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/tests/test_groups.py +14 -0
  14. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/tests/test_raw_message.py +26 -0
  15. {refix-0.3.0 → refix-0.4.0}/Cargo.toml +0 -0
  16. {refix-0.3.0 → refix-0.4.0}/LICENSE-APACHE +0 -0
  17. {refix-0.3.0 → refix-0.4.0}/LICENSE-MIT +0 -0
  18. {refix-0.3.0 → refix-0.4.0}/README.md +0 -0
  19. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/LICENSE-APACHE +0 -0
  20. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/LICENSE-MIT +0 -0
  21. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/README.md +0 -0
  22. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/framing.rs +0 -0
  23. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/group/error.rs +0 -0
  24. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/group.rs +0 -0
  25. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/length_tags.rs +0 -0
  26. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/multiple_values.rs +0 -0
  27. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/stream.rs +0 -0
  28. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/tag.rs +0 -0
  29. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/test_utils.rs +0 -0
  30. {refix-0.3.0 → refix-0.4.0}/crates/refix-message/src/tokenizer.rs +0 -0
  31. /refix-0.3.0/crates/refix-message/src/value.rs → /refix-0.4.0/crates/refix-message/src/value/error.rs +0 -0
  32. {refix-0.3.0 → refix-0.4.0}/pyproject.toml +0 -0
  33. {refix-0.3.0 → refix-0.4.0}/python/refix/__init__.py +0 -0
  34. {refix-0.3.0 → refix-0.4.0}/python/refix/enums.py +0 -0
  35. {refix-0.3.0 → refix-0.4.0}/python/refix/errors.py +0 -0
  36. {refix-0.3.0 → refix-0.4.0}/python/refix/log.py +0 -0
  37. {refix-0.3.0 → refix-0.4.0}/python/refix/py.typed +0 -0
  38. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/.python-version +0 -0
  39. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/LICENSE-APACHE +0 -0
  40. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/LICENSE-MIT +0 -0
  41. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/README.md +0 -0
  42. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/src/group.rs +0 -0
  43. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/src/lib.rs +0 -0
  44. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/src/stream.rs +0 -0
  45. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/src/tokenizer.rs +0 -0
  46. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/tests/test_benchmarks.py +0 -0
  47. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/tests/test_enums.py +0 -0
  48. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/tests/test_log.py +0 -0
  49. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/tests/test_stream.py +0 -0
  50. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/tests/test_tokenizer.py +0 -0
  51. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/tests/test_toy_generated.py +0 -0
  52. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/tests/toy_generated.py +0 -0
  53. {refix-0.3.0 → refix-0.4.0}/python/refix-engine/uv.lock +0 -0
@@ -256,7 +256,7 @@ dependencies = [
256
256
 
257
257
  [[package]]
258
258
  name = "refix-engine"
259
- version = "0.3.0"
259
+ version = "0.4.0"
260
260
  dependencies = [
261
261
  "bytes",
262
262
  "pyo3",
@@ -273,7 +273,7 @@ dependencies = [
273
273
 
274
274
  [[package]]
275
275
  name = "refix-message"
276
- version = "0.3.2"
276
+ version = "0.3.3"
277
277
  dependencies = [
278
278
  "bytes",
279
279
  ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: refix
3
- Version: 0.3.0
3
+ Version: 0.4.0
4
4
  Classifier: Development Status :: 2 - Pre-Alpha
5
5
  Classifier: Intended Audience :: Financial and Insurance Industry
6
6
  Classifier: Programming Language :: Python :: 3
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.3.3](https://github.com/refixhq/refix/compare/refix-message-v0.3.2...refix-message-v0.3.3) - 2026-10-17
11
+
12
+ ### Added
13
+
14
+ - read FIX floats as decimals ([#91](https://github.com/refixhq/refix/pull/91))
15
+
10
16
  ## [0.3.2](https://github.com/refixhq/refix/compare/refix-message-v0.3.1...refix-message-v0.3.2) - 2026-10-10
11
17
 
12
18
  ### Other
@@ -1,7 +1,7 @@
1
1
  [package]
2
2
  name = "refix-message"
3
3
  description = "Framing, tokenisation and raw message representation for the ReFIX engine"
4
- version = "0.3.2"
4
+ version = "0.3.3"
5
5
  authors.workspace = true
6
6
  edition.workspace = true
7
7
  license.workspace = true
@@ -20,4 +20,4 @@ pub use multiple_values::MultipleValues;
20
20
  pub use stream::MessageStream;
21
21
  pub use tag::Tag;
22
22
  pub use tokenizer::{TokenizeError, Tokenizer};
23
- pub use value::InvalidValue;
23
+ pub use value::{Decimal, InvalidValue};
@@ -1,5 +1,4 @@
1
- use crate::Tag;
2
- use crate::value::InvalidValue;
1
+ use crate::{Decimal, InvalidValue, Tag};
3
2
  use bytes::Bytes;
4
3
 
5
4
  /// A field's position in a message's index.
@@ -79,6 +78,11 @@ impl RawMessage {
79
78
  self.scope().get_int(tag)
80
79
  }
81
80
 
81
+ /// First occurrence of `tag` parsed as a decimal or `Ok(None)` when absent.
82
+ pub fn get_decimal(&self, tag: Tag) -> Result<Option<Decimal>, InvalidValue> {
83
+ self.scope().get_decimal(tag)
84
+ }
85
+
82
86
  /// Every field as `(tag, value)`, in wire order, duplicates included.
83
87
  ///
84
88
  /// Includes an entry with [`Tag::MALFORMED`] for any byte run that could
@@ -124,6 +128,7 @@ impl<'a> Scope<'a> {
124
128
  let Some(value) = self.get(tag) else {
125
129
  return Ok(None);
126
130
  };
131
+
127
132
  let value = std::str::from_utf8(value).map_err(|_| InvalidValue { tag })?;
128
133
  Ok(Some(value))
129
134
  }
@@ -133,10 +138,21 @@ impl<'a> Scope<'a> {
133
138
  let Some(value) = self.get_str(tag)? else {
134
139
  return Ok(None);
135
140
  };
141
+
136
142
  let value = value.parse().map_err(|_| InvalidValue { tag })?;
137
143
  Ok(Some(value))
138
144
  }
139
145
 
146
+ /// First occurrence of `tag` parsed as a decimal or `Ok(None)` when absent.
147
+ pub fn get_decimal(&self, tag: Tag) -> Result<Option<Decimal>, InvalidValue> {
148
+ let Some(value) = self.get(tag) else {
149
+ return Ok(None);
150
+ };
151
+
152
+ let value = Decimal::parse(value).ok_or(InvalidValue { tag })?;
153
+ Ok(Some(value))
154
+ }
155
+
140
156
  /// First occurrence of `tag` at or after `from` and before the end of
141
157
  /// the scope, with its slot so the caller can continue or bound a range.
142
158
  pub(crate) fn find(&self, tag: Tag, from: Slot) -> Option<(Slot, &'a [u8])> {
@@ -270,6 +286,30 @@ mod tests {
270
286
  assert_eq!(message.get_int(Tag(38)), Err(InvalidValue { tag: Tag(38) }));
271
287
  }
272
288
 
289
+ #[test]
290
+ fn get_decimal_parses_a_price() {
291
+ let message = message_of(&[(44, "101.25")]);
292
+ assert_eq!(
293
+ message.get_decimal(Tag(44)),
294
+ Ok(Some(Decimal::new(10125, -2)))
295
+ );
296
+ }
297
+
298
+ #[test]
299
+ fn get_decimal_absent_tag() {
300
+ let message = message_of(&[(35, "0")]);
301
+ assert_eq!(message.get_decimal(Tag(44)), Ok(None));
302
+ }
303
+
304
+ #[test]
305
+ fn get_decimal_rejects_garbage() {
306
+ let message = message_of(&[(44, "1e5")]);
307
+ assert_eq!(
308
+ message.get_decimal(Tag(44)),
309
+ Err(InvalidValue { tag: Tag(44) })
310
+ );
311
+ }
312
+
273
313
  mod scope {
274
314
  use super::*;
275
315
 
@@ -0,0 +1,232 @@
1
+ use std::fmt;
2
+ use std::hash::{Hash, Hasher};
3
+
4
+ /// A FIX float, such as a price or quantity, as `mantissa * 10^exponent`.
5
+ #[derive(Clone, Copy, Debug)]
6
+ pub struct Decimal {
7
+ mantissa: i64,
8
+ exponent: i8,
9
+ }
10
+
11
+ impl Decimal {
12
+ /// The decimal `mantissa * 10^exponent`.
13
+ pub const fn new(mantissa: i64, exponent: i8) -> Self {
14
+ Self { mantissa, exponent }
15
+ }
16
+
17
+ /// The value's digits as an integer, such as `10125` for `101.25`.
18
+ pub const fn mantissa(&self) -> i64 {
19
+ self.mantissa
20
+ }
21
+
22
+ /// The power of ten the mantissa is scaled by, such as `-2` for `101.25`.
23
+ pub const fn exponent(&self) -> i8 {
24
+ self.exponent
25
+ }
26
+
27
+ /// Reads a FIX float such as `-101.25`, keeping its scale.
28
+ pub(crate) fn parse(text: &[u8]) -> Option<Self> {
29
+ let (negative, unsigned) = match text.strip_prefix(b"-") {
30
+ Some(rest) => (true, rest),
31
+ None => (false, text),
32
+ };
33
+ let mut parts = unsigned.splitn(2, |&byte| byte == b'.');
34
+ let whole = parts.next().unwrap_or_default();
35
+ let fraction = parts.next().unwrap_or_default();
36
+ if whole.is_empty() && fraction.is_empty() {
37
+ return None;
38
+ }
39
+ let exponent = -i8::try_from(fraction.len()).ok()?;
40
+ let mut mantissa: i64 = 0;
41
+ for &byte in whole.iter().chain(fraction) {
42
+ if !byte.is_ascii_digit() {
43
+ return None;
44
+ }
45
+ mantissa = mantissa
46
+ .checked_mul(10)?
47
+ .checked_add(i64::from(byte - b'0'))?;
48
+ }
49
+ let mantissa = if negative { -mantissa } else { mantissa };
50
+ Some(Self::new(mantissa, exponent))
51
+ }
52
+
53
+ /// The same value without trailing zeros, to compare across scales.
54
+ fn normalized(self) -> (i64, i8) {
55
+ if self.mantissa == 0 {
56
+ return (0, 0);
57
+ }
58
+ let (mut mantissa, mut exponent) = (self.mantissa, self.exponent);
59
+ while mantissa % 10 == 0 && exponent < i8::MAX {
60
+ mantissa /= 10;
61
+ exponent += 1;
62
+ }
63
+ (mantissa, exponent)
64
+ }
65
+ }
66
+
67
+ impl PartialEq for Decimal {
68
+ fn eq(&self, other: &Self) -> bool {
69
+ self.normalized() == other.normalized()
70
+ }
71
+ }
72
+
73
+ impl Eq for Decimal {}
74
+
75
+ impl Hash for Decimal {
76
+ fn hash<H: Hasher>(&self, state: &mut H) {
77
+ self.normalized().hash(state);
78
+ }
79
+ }
80
+
81
+ impl fmt::Display for Decimal {
82
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
83
+ let digits = self.mantissa.unsigned_abs().to_string();
84
+ let scale = usize::from(self.exponent.unsigned_abs());
85
+ let text = if self.exponent >= 0 {
86
+ format!("{digits}{}", "0".repeat(scale))
87
+ } else if digits.len() > scale {
88
+ let (whole, fraction) = digits.split_at(digits.len() - scale);
89
+ format!("{whole}.{fraction}")
90
+ } else {
91
+ format!("0.{digits:0>scale$}")
92
+ };
93
+ f.pad_integral(self.mantissa >= 0, "", &text)
94
+ }
95
+ }
96
+
97
+ #[cfg(test)]
98
+ mod tests {
99
+ use super::*;
100
+
101
+ fn parse(text: &str) -> Option<Decimal> {
102
+ Decimal::parse(text.as_bytes())
103
+ }
104
+
105
+ fn parts(text: &str) -> (i64, i8) {
106
+ let value = parse(text).unwrap();
107
+ (value.mantissa(), value.exponent())
108
+ }
109
+
110
+ mod parsing {
111
+ use super::*;
112
+
113
+ #[test]
114
+ fn reads_the_scale_from_the_fraction() {
115
+ assert_eq!(parts("101.25"), (10125, -2));
116
+ }
117
+
118
+ #[test]
119
+ fn keeps_trailing_zeros() {
120
+ assert_eq!(parts("1.50"), (150, -2));
121
+ }
122
+
123
+ #[test]
124
+ fn reads_a_negative_value() {
125
+ assert_eq!(parts("-0.005"), (-5, -3));
126
+ }
127
+
128
+ #[test]
129
+ fn reads_an_integer() {
130
+ assert_eq!(parts("100"), (100, 0));
131
+ }
132
+
133
+ #[test]
134
+ fn accepts_a_point_on_either_end() {
135
+ assert_eq!(parts("23."), (23, 0));
136
+ assert_eq!(parts(".5"), (5, -1));
137
+ assert_eq!(parts("-.5"), (-5, -1));
138
+ }
139
+
140
+ #[test]
141
+ fn skips_leading_zeros() {
142
+ assert_eq!(parts("00023.23"), (2323, -2));
143
+ }
144
+
145
+ #[test]
146
+ fn rejects_text_without_digits() {
147
+ for text in ["", "-", ".", "-."] {
148
+ assert_eq!(parse(text), None, "{text:?}");
149
+ }
150
+ }
151
+
152
+ #[test]
153
+ fn rejects_other_characters() {
154
+ for text in ["+5", "1e5", " 1", "1 ", "1.2.3", "--1", "1-"] {
155
+ assert_eq!(parse(text), None, "{text:?}");
156
+ }
157
+ }
158
+
159
+ #[test]
160
+ fn rejects_a_mantissa_beyond_i64() {
161
+ assert_eq!(parts("9223372036854775807"), (i64::MAX, 0));
162
+ assert_eq!(parse("9223372036854775808"), None);
163
+ assert_eq!(parse("1.0000000000000000000"), None);
164
+ }
165
+
166
+ #[test]
167
+ fn rejects_more_than_127_fraction_digits() {
168
+ assert_eq!(parts(&format!("0.{}", "0".repeat(127))), (0, -127));
169
+ assert_eq!(parse(&format!("0.{}", "0".repeat(128))), None);
170
+ }
171
+ }
172
+
173
+ mod display {
174
+ use super::*;
175
+
176
+ #[test]
177
+ fn keeps_the_scale() {
178
+ assert_eq!(Decimal::new(150, -2).to_string(), "1.50");
179
+ }
180
+
181
+ #[test]
182
+ fn pads_a_small_fraction() {
183
+ assert_eq!(Decimal::new(-5, -3).to_string(), "-0.005");
184
+ assert_eq!(Decimal::new(0, -2).to_string(), "0.00");
185
+ }
186
+
187
+ #[test]
188
+ fn writes_an_integer_without_a_point() {
189
+ assert_eq!(Decimal::new(23, 0).to_string(), "23");
190
+ assert_eq!(Decimal::new(15, 3).to_string(), "15000");
191
+ }
192
+
193
+ #[test]
194
+ fn round_trips_the_wire_text() {
195
+ for text in ["101.25", "-1.50", "0.005", "100"] {
196
+ assert_eq!(parse(text).unwrap().to_string(), text);
197
+ }
198
+ }
199
+
200
+ #[test]
201
+ fn honours_width_and_alignment() {
202
+ assert_eq!(format!("{:>8}", Decimal::new(150, -2)), " 1.50");
203
+ assert_eq!(format!("{:08}", Decimal::new(-150, -2)), "-0001.50");
204
+ }
205
+ }
206
+
207
+ mod equality {
208
+ use super::*;
209
+ use std::hash::DefaultHasher;
210
+
211
+ fn hash_of(value: Decimal) -> u64 {
212
+ let mut hasher = DefaultHasher::new();
213
+ value.hash(&mut hasher);
214
+ hasher.finish()
215
+ }
216
+
217
+ #[test]
218
+ fn compares_by_value_across_scales() {
219
+ assert_eq!(Decimal::new(150, -2), Decimal::new(15, -1));
220
+ assert_eq!(Decimal::new(0, -2), Decimal::new(0, 0));
221
+ assert_ne!(Decimal::new(150, -2), Decimal::new(151, -2));
222
+ }
223
+
224
+ #[test]
225
+ fn equal_values_hash_alike() {
226
+ assert_eq!(
227
+ hash_of(Decimal::new(150, -2)),
228
+ hash_of(Decimal::new(15, -1))
229
+ );
230
+ }
231
+ }
232
+ }
@@ -0,0 +1,5 @@
1
+ mod decimal;
2
+ mod error;
3
+
4
+ pub use decimal::Decimal;
5
+ pub use error::InvalidValue;
@@ -1,5 +1,6 @@
1
1
  import builtins
2
2
  from collections.abc import Sequence
3
+ from decimal import Decimal
3
4
 
4
5
  from refix.errors import GarbledReason
5
6
 
@@ -9,6 +10,7 @@ class RawMessage:
9
10
  def get(self, tag: int) -> builtins.bytes | None: ...
10
11
  def get_str(self, tag: int) -> str | None: ...
11
12
  def get_int(self, tag: int) -> int | None: ...
13
+ def get_decimal(self, tag: int) -> Decimal | None: ...
12
14
  def get_multiple_values(self, tag: int) -> tuple[str, ...] | None: ...
13
15
  def get_group(
14
16
  self, table: GroupTable, known: KnownTags
@@ -19,6 +21,7 @@ class Scope:
19
21
  def get(self, tag: int) -> builtins.bytes | None: ...
20
22
  def get_str(self, tag: int) -> str | None: ...
21
23
  def get_int(self, tag: int) -> int | None: ...
24
+ def get_decimal(self, tag: int) -> Decimal | None: ...
22
25
  def get_multiple_values(self, tag: int) -> tuple[str, ...] | None: ...
23
26
  def get_group(
24
27
  self, table: GroupTable, known: KnownTags
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.4.0](https://github.com/refixhq/refix/compare/python-v0.3.0...python-v0.4.0) - 2026-10-17
11
+
12
+ ### Added
13
+
14
+ - read decimals from Python ([#93](https://github.com/refixhq/refix/pull/93))
15
+
10
16
  ## [0.3.0](https://github.com/refixhq/refix/compare/python-v0.2.2...python-v0.3.0) - 2026-10-11
11
17
 
12
18
  ### Added
@@ -1,6 +1,6 @@
1
1
  [package]
2
2
  name = "refix-engine"
3
- version = "0.3.0"
3
+ version = "0.4.0"
4
4
  publish = false
5
5
  authors.workspace = true
6
6
  edition.workspace = true
@@ -1,5 +1,6 @@
1
- use pyo3::types::{PyBytes, PyString, PyTuple};
2
- use pyo3::{Bound, Py, PyErr, PyResult, Python, import_exception, pyclass, pymethods};
1
+ use pyo3::sync::PyOnceLock;
2
+ use pyo3::types::{PyAnyMethods, PyBytes, PyString, PyTuple, PyType};
3
+ use pyo3::{Bound, Py, PyAny, PyErr, PyResult, Python, import_exception, pyclass, pymethods};
3
4
  use refix_message::{
4
5
  Group, InvalidValue, RawMessage as CoreRawMessage, Scope as CoreScope, Slot, Tag,
5
6
  };
@@ -8,6 +9,9 @@ use crate::group::{GroupTable, KnownTags};
8
9
 
9
10
  import_exception!(refix.errors, InvalidValueError);
10
11
 
12
+ /// Python's `decimal.Decimal`, imported on first use.
13
+ static DECIMAL_TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
14
+
11
15
  #[pyclass(frozen, module = "refix._core")]
12
16
  pub(crate) struct RawMessage(CoreRawMessage);
13
17
 
@@ -36,6 +40,10 @@ impl RawMessage {
36
40
  get_int(&self.0.scope(), tag)
37
41
  }
38
42
 
43
+ fn get_decimal<'py>(&self, py: Python<'py>, tag: u32) -> PyResult<Option<Bound<'py, PyAny>>> {
44
+ get_decimal(py, &self.0.scope(), tag)
45
+ }
46
+
39
47
  fn get_multiple_values<'py>(
40
48
  &self,
41
49
  py: Python<'py>,
@@ -95,6 +103,10 @@ impl Scope {
95
103
  get_int(&self.scope(), tag)
96
104
  }
97
105
 
106
+ fn get_decimal<'py>(&self, py: Python<'py>, tag: u32) -> PyResult<Option<Bound<'py, PyAny>>> {
107
+ get_decimal(py, &self.scope(), tag)
108
+ }
109
+
98
110
  fn get_multiple_values<'py>(
99
111
  &self,
100
112
  py: Python<'py>,
@@ -130,6 +142,19 @@ fn get_int(scope: &CoreScope<'_>, tag: u32) -> PyResult<Option<i64>> {
130
142
  scope.get_int(Tag(tag)).map_err(to_py_err)
131
143
  }
132
144
 
145
+ fn get_decimal<'py>(
146
+ py: Python<'py>,
147
+ scope: &CoreScope<'_>,
148
+ tag: u32,
149
+ ) -> PyResult<Option<Bound<'py, PyAny>>> {
150
+ let Some(value) = scope.get_decimal(Tag(tag)).map_err(to_py_err)? else {
151
+ return Ok(None);
152
+ };
153
+
154
+ let decimal = DECIMAL_TYPE.import(py, "decimal", "Decimal")?;
155
+ decimal.call1((value.to_string(),)).map(Some)
156
+ }
157
+
133
158
  fn get_multiple_values<'py>(
134
159
  py: Python<'py>,
135
160
  scope: &CoreScope<'_>,
@@ -1,4 +1,5 @@
1
1
  import gc
2
+ from decimal import Decimal
2
3
 
3
4
  import pytest
4
5
  import refix
@@ -90,6 +91,19 @@ class TestGetGroup:
90
91
  assert instances is not None
91
92
  assert instances[0].get_str(448) == "AL"
92
93
 
94
+ def test_an_instance_reads_decimals(self):
95
+ entries = refix.GroupTable(268, 269, (269, 270), ())
96
+
97
+ message = tokenize_body("35=W|268=2|269=0|270=101.25|269=1|270=101.50|")
98
+
99
+ instances = message.get_group(entries, refix.KnownTags((35, 268, 269, 270)))
100
+
101
+ assert instances is not None
102
+ assert [entry.get_decimal(270) for entry in instances] == [
103
+ Decimal("101.25"),
104
+ Decimal("101.50"),
105
+ ]
106
+
93
107
  def test_a_scope_cannot_be_built_directly(self):
94
108
  with pytest.raises(TypeError):
95
109
  refix.Scope()
@@ -1,4 +1,5 @@
1
1
  import pickle
2
+ from decimal import Decimal
2
3
 
3
4
  import pytest
4
5
  import refix
@@ -84,3 +85,28 @@ class TestInvalidValueError:
84
85
  def test_pickles(self):
85
86
  clone = pickle.loads(pickle.dumps(InvalidValueError(38)))
86
87
  assert clone.tag == 38
88
+
89
+
90
+ class TestGetDecimal:
91
+ def test_reads_a_price(self):
92
+ assert tokenize_body("35=D|44=101.25|").get_decimal(44) == Decimal("101.25")
93
+
94
+ def test_keeps_the_scale(self):
95
+ assert str(tokenize_body("35=D|44=1.50|").get_decimal(44)) == "1.50"
96
+
97
+ def test_reads_a_negative_value(self):
98
+ assert tokenize_body("35=D|44=-.5|").get_decimal(44) == Decimal("-0.5")
99
+
100
+ def test_absent_tag_is_none(self):
101
+ assert tokenize_body("35=D|").get_decimal(44) is None
102
+
103
+ @pytest.mark.parametrize(
104
+ "value", ["", "1e5", "NaN", "Infinity", " 1", "+5", "1_000"]
105
+ )
106
+ def test_text_only_python_accepts_raises(self, value: str):
107
+ message = tokenize_body(f"35=D|44={value}|")
108
+
109
+ with pytest.raises(InvalidValueError) as excinfo:
110
+ message.get_decimal(44)
111
+
112
+ assert excinfo.value.tag == 44
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes