trading-ig 0.1.0

Async Rust client for the IG Markets REST and Lightstreamer streaming APIs
Documentation
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
//! Pure TLCP frame parser — no I/O, no async.
//!
//! Lightstreamer uses a newline-delimited text protocol.  Each line is
//! either a server command (`OK`, `PROBE`, `LOOP`, …) or a subscription
//! update whose first pipe-delimited token is the item index.
//!
//! # Special tokens
//!
//! | Wire token | Meaning             |
//! | ---------- | ------------------- |
//! | `$`        | Empty string `""`   |
//! | `#`        | `null` / `None`     |
//! | *(absent)* | Unchanged from last update |
//!
//! Unchanged trailing fields are represented by [`FieldValue::Unchanged`].
//!
//! # Usage
//!
//! ```
//! use trading_ig::streaming::protocol::{Frame, parse_line};
//!
//! let frame = parse_line(b"1|100.5|101.0|$|#");
//! assert!(matches!(frame, Frame::Update { .. }));
//! ```

/// A parsed value for a single subscription field.
#[derive(Debug, Clone, PartialEq)]
pub enum FieldValue {
    /// A concrete non-null string value (possibly empty, decoded from `$`).
    Value(String),
    /// The field is explicitly `null` (wire token `#`).
    Null,
    /// The field was absent in this frame — callers should keep the previous
    /// value.
    Unchanged,
}

impl FieldValue {
    /// Return `Some(&str)` if this is a concrete non-null value, otherwise `None`.
    pub fn as_str(&self) -> Option<&str> {
        match self {
            FieldValue::Value(s) => Some(s.as_str()),
            _ => None,
        }
    }

    /// Parse into an `f64`, returning `None` for null/unchanged/non-numeric values.
    pub fn parse_f64(&self) -> Option<f64> {
        self.as_str()?.parse().ok()
    }

    /// Parse into an `i64`, returning `None` for null/unchanged/non-numeric values.
    pub fn parse_i64(&self) -> Option<i64> {
        self.as_str()?.parse().ok()
    }

    /// Parse into a `bool` (`"true"`/`"false"`), returning `None` otherwise.
    pub fn parse_bool(&self) -> Option<bool> {
        match self.as_str()? {
            "true" => Some(true),
            "false" => Some(false),
            _ => None,
        }
    }
}

/// A decoded frame from the Lightstreamer streaming channel.
#[derive(Debug, Clone, PartialEq)]
pub enum Frame {
    /// A subscription update: item index (1-based) and field values.
    Update {
        /// 1-based subscription item index, as returned by the server's OK response.
        item_index: usize,
        /// Field values in the order declared at subscription time.
        fields: Vec<FieldValue>,
    },
    /// Server keep-alive; no action needed.
    Probe,
    /// Server asks client to call `bind_session` — reconnect is required.
    Loop,
    /// Session out-of-sync; all subscriptions must be re-registered.
    SyncError,
    /// Server signals a fatal error.
    Error {
        /// Error code from the server.
        code: String,
        /// Human-readable message.
        message: String,
    },
    /// Session has been terminated by the server.
    End {
        /// Optional server-supplied reason.
        cause: Option<String>,
    },
    /// Handshake acknowledgement; contains the assigned session ID.
    Ok {
        /// Lightstreamer session identifier.
        session_id: String,
    },
    /// A line that was not recognised (defensive: callers should ignore).
    Unknown(String),
}

/// Parse a single raw line (without its trailing newline) into a [`Frame`].
///
/// The `line` argument must not include the `\n` terminator.
#[must_use]
pub fn parse_line(line: &[u8]) -> Frame {
    let s = match std::str::from_utf8(line) {
        Ok(s) => s.trim_end_matches('\r'),
        Err(_) => return Frame::Unknown(String::from_utf8_lossy(line).into_owned()),
    };

    // --- Server commands ---
    if s == "PROBE" {
        return Frame::Probe;
    }
    if s == "LOOP" {
        return Frame::Loop;
    }
    if s == "SYNC ERROR" {
        return Frame::SyncError;
    }
    // OK response: "OK\nSessionId:...\nControlAddress:...\n..."
    // When streaming responses include multiple response lines, the session_id
    // comes on the line "SessionId:<id>" after the "OK".  But in practice the
    // server sends one logical block.  We handle the inline variant here; the
    // multi-line OK block is assembled by the connection layer.
    if s == "OK" {
        return Frame::Ok {
            session_id: String::new(),
        };
    }
    // "OK\r\n" in chunked; multi-line OK values are handled by parse_ok_block.
    if let Some(rest) = s.strip_prefix("SessionId:") {
        return Frame::Ok {
            session_id: rest.to_owned(),
        };
    }
    if let Some(code_rest) = s.strip_prefix("ERROR") {
        // ERROR\n<code>\n<message>  — again multi-line; we handle the inline case.
        let trimmed = code_rest.trim();
        let (code, message) = if let Some(idx) = trimmed.find('\n') {
            (trimmed[..idx].to_owned(), trimmed[idx + 1..].to_owned())
        } else {
            (trimmed.to_owned(), String::new())
        };
        return Frame::Error { code, message };
    }
    if let Some(rest) = s.strip_prefix("END") {
        let cause = {
            let trimmed = rest.trim();
            if trimmed.is_empty() {
                None
            } else {
                Some(trimmed.to_owned())
            }
        };
        return Frame::End { cause };
    }

    // --- Update line: "<item_index>|<f1>|<f2>|..." ---
    if let Some(pipe_pos) = s.find('|') {
        let index_str = &s[..pipe_pos];
        if let Ok(item_index) = index_str.parse::<usize>() {
            let fields_str = &s[pipe_pos + 1..];
            let fields = fields_str.split('|').map(decode_field).collect();
            return Frame::Update { item_index, fields };
        }
    }

    Frame::Unknown(s.to_owned())
}

/// Parse a complete multi-line OK block from `create_session` / `bind_session`.
///
/// The block is the entire text up to and including the blank line that
/// terminates the TLCP header.  Returns the session ID and any extra
/// key-value pairs.
///
/// ```
/// use trading_ig::streaming::protocol::parse_ok_block;
///
/// let block = "OK\r\nSessionId:S1abc\r\nControlAddress:srv1\r\n\r\n";
/// let (session_id, _rest) = parse_ok_block(block).unwrap();
/// assert_eq!(session_id, "S1abc");
/// ```
pub fn parse_ok_block(block: &str) -> Option<(String, Vec<(String, String)>)> {
    let mut lines = block.lines();
    let first = lines.next()?.trim();
    if first != "OK" {
        return None;
    }
    let mut session_id = String::new();
    let mut extras = Vec::new();
    for line in lines {
        let line = line.trim();
        if line.is_empty() {
            break;
        }
        if let Some(id) = line.strip_prefix("SessionId:") {
            id.clone_into(&mut session_id);
        } else if let Some((k, v)) = line.split_once(':') {
            extras.push((k.to_owned(), v.to_owned()));
        }
    }
    Some((session_id, extras))
}

/// Decode a single pipe-delimited field token.
///
/// Per the TLCP spec:
/// - `$`   → empty string
/// - `#`   → null
/// - `""`  (empty between pipes) → unchanged
/// - anything else → concrete value
fn decode_field(token: &str) -> FieldValue {
    match token {
        "$" => FieldValue::Value(String::new()),
        "#" => FieldValue::Null,
        "" => FieldValue::Unchanged,
        other => FieldValue::Value(other.to_owned()),
    }
}

/// Apply a new frame's fields on top of a running state vector, respecting
/// the "absent = unchanged" rule.
///
/// `state` is modified in-place.  If `state` is shorter than `fields`, it
/// is extended.
pub fn merge_fields(state: &mut Vec<Option<String>>, fields: &[FieldValue]) {
    if state.len() < fields.len() {
        state.resize(fields.len(), None);
    }
    for (i, fv) in fields.iter().enumerate() {
        match fv {
            FieldValue::Value(s) => state[i] = Some(s.clone()),
            FieldValue::Null => state[i] = None,
            FieldValue::Unchanged => { /* keep current */ }
        }
    }
}

// ---------------------------------------------------------------------------
// Unit tests
// ---------------------------------------------------------------------------

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

    // ------------------------------------------------------------------
    // parse_line – server commands
    // ------------------------------------------------------------------

    #[test]
    fn test_probe() {
        assert_eq!(parse_line(b"PROBE"), Frame::Probe);
    }

    #[test]
    fn test_loop() {
        assert_eq!(parse_line(b"LOOP"), Frame::Loop);
    }

    #[test]
    fn test_sync_error() {
        assert_eq!(parse_line(b"SYNC ERROR"), Frame::SyncError);
    }

    #[test]
    fn test_ok_bare() {
        assert_eq!(
            parse_line(b"OK"),
            Frame::Ok {
                session_id: String::new()
            }
        );
    }

    #[test]
    fn test_session_id_line() {
        assert_eq!(
            parse_line(b"SessionId:Sabc123"),
            Frame::Ok {
                session_id: "Sabc123".into()
            }
        );
    }

    #[test]
    fn test_error_bare() {
        let f = parse_line(b"ERROR42 bad connection");
        assert!(matches!(f, Frame::Error { .. }));
    }

    #[test]
    fn test_end_no_cause() {
        assert_eq!(parse_line(b"END"), Frame::End { cause: None });
    }

    #[test]
    fn test_end_with_cause() {
        assert_eq!(
            parse_line(b"END session expired"),
            Frame::End {
                cause: Some("session expired".into())
            }
        );
    }

    // ------------------------------------------------------------------
    // parse_line – update frames
    // ------------------------------------------------------------------

    #[test]
    fn test_simple_update() {
        let f = parse_line(b"1|100.5|101.0");
        assert_eq!(
            f,
            Frame::Update {
                item_index: 1,
                fields: vec![
                    FieldValue::Value("100.5".into()),
                    FieldValue::Value("101.0".into()),
                ]
            }
        );
    }

    #[test]
    fn test_empty_string_dollar() {
        let f = parse_line(b"1|$|101.0");
        assert_eq!(
            f,
            Frame::Update {
                item_index: 1,
                fields: vec![
                    FieldValue::Value(String::new()),
                    FieldValue::Value("101.0".into()),
                ]
            }
        );
    }

    #[test]
    fn test_null_hash() {
        let f = parse_line(b"1|#|101.0");
        assert_eq!(
            f,
            Frame::Update {
                item_index: 1,
                fields: vec![FieldValue::Null, FieldValue::Value("101.0".into()),]
            }
        );
    }

    #[test]
    fn test_unchanged_trailing_fields() {
        // Only the first two fields are present; remaining are "unchanged".
        let f = parse_line(b"2|BID_VALUE|OFR_VALUE");
        assert_eq!(
            f,
            Frame::Update {
                item_index: 2,
                fields: vec![
                    FieldValue::Value("BID_VALUE".into()),
                    FieldValue::Value("OFR_VALUE".into()),
                ]
            }
        );
        // If the subscriber expects 10 fields they should treat indices 2..9 as Unchanged.
    }

    #[test]
    fn test_update_with_empty_middle_field() {
        // "||" → middle field is empty string decoded from "$"? No — bare empty is
        // actually the "unchanged" token (absent between pipes).  Wire spec:
        // absent = between consecutive pipes with nothing in between.
        let f = parse_line(b"1|100||200");
        assert_eq!(
            f,
            Frame::Update {
                item_index: 1,
                fields: vec![
                    FieldValue::Value("100".into()),
                    FieldValue::Unchanged,
                    FieldValue::Value("200".into()),
                ]
            }
        );
    }

    #[test]
    fn test_update_all_special() {
        let f = parse_line(b"3|$|#|$");
        assert_eq!(
            f,
            Frame::Update {
                item_index: 3,
                fields: vec![
                    FieldValue::Value(String::new()),
                    FieldValue::Null,
                    FieldValue::Value(String::new()),
                ]
            }
        );
    }

    #[test]
    fn test_high_item_index() {
        let f = parse_line(b"42|val");
        assert_eq!(
            f,
            Frame::Update {
                item_index: 42,
                fields: vec![FieldValue::Value("val".into())]
            }
        );
    }

    #[test]
    fn test_crlf_stripped() {
        // parse_line trims trailing CR (caller already strips LF)
        let f = parse_line(b"PROBE\r");
        assert_eq!(f, Frame::Probe);
    }

    #[test]
    fn test_unknown_line() {
        let f = parse_line(b"SOMETHING_WEIRD");
        assert!(matches!(f, Frame::Unknown(_)));
    }

    // ------------------------------------------------------------------
    // parse_ok_block
    // ------------------------------------------------------------------

    #[test]
    fn test_parse_ok_block_basic() {
        let block = "OK\r\nSessionId:Sabc\r\nControlAddress:push.lightstreamer.com\r\n\r\n";
        let (sid, extras) = parse_ok_block(block).unwrap();
        assert_eq!(sid, "Sabc");
        assert!(extras.iter().any(|(k, _)| k == "ControlAddress"));
    }

    #[test]
    fn test_parse_ok_block_missing_ok() {
        assert!(parse_ok_block("ERROR\r\nSomething\r\n").is_none());
    }

    #[test]
    fn test_parse_ok_block_no_session_id() {
        let block = "OK\r\nControlAddress:push.ls.com\r\n\r\n";
        let (sid, _) = parse_ok_block(block).unwrap();
        assert_eq!(sid, "");
    }

    // ------------------------------------------------------------------
    // merge_fields
    // ------------------------------------------------------------------

    #[test]
    fn test_merge_fields_all_new() {
        let mut state: Vec<Option<String>> = vec![];
        merge_fields(
            &mut state,
            &[
                FieldValue::Value("100.0".into()),
                FieldValue::Value("101.0".into()),
            ],
        );
        assert_eq!(state, vec![Some("100.0".into()), Some("101.0".into())]);
    }

    #[test]
    fn test_merge_fields_unchanged_preserved() {
        let mut state = vec![Some("100.0".into()), Some("101.0".into())];
        merge_fields(
            &mut state,
            &[FieldValue::Unchanged, FieldValue::Value("102.0".into())],
        );
        assert_eq!(state, vec![Some("100.0".into()), Some("102.0".into())]);
    }

    #[test]
    fn test_merge_fields_null_clears() {
        let mut state = vec![Some("100.0".into()), Some("101.0".into())];
        merge_fields(&mut state, &[FieldValue::Null, FieldValue::Unchanged]);
        assert_eq!(state, vec![None, Some("101.0".into())]);
    }

    #[test]
    fn test_merge_fields_extends_state() {
        let mut state: Vec<Option<String>> = vec![Some("a".into())];
        merge_fields(
            &mut state,
            &[
                FieldValue::Unchanged,
                FieldValue::Value("b".into()),
                FieldValue::Value("c".into()),
            ],
        );
        assert_eq!(
            state,
            vec![Some("a".into()), Some("b".into()), Some("c".into())]
        );
    }

    // ------------------------------------------------------------------
    // FieldValue helpers
    // ------------------------------------------------------------------

    #[test]
    fn test_field_value_parse_f64() {
        assert_eq!(FieldValue::Value("1.5".into()).parse_f64(), Some(1.5));
        assert_eq!(FieldValue::Null.parse_f64(), None);
        assert_eq!(FieldValue::Unchanged.parse_f64(), None);
    }

    #[test]
    fn test_field_value_parse_i64() {
        assert_eq!(FieldValue::Value("42".into()).parse_i64(), Some(42));
    }

    #[test]
    fn test_field_value_parse_bool() {
        assert_eq!(FieldValue::Value("true".into()).parse_bool(), Some(true));
        assert_eq!(FieldValue::Value("false".into()).parse_bool(), Some(false));
        assert_eq!(FieldValue::Value("yes".into()).parse_bool(), None);
    }
}