nautilus-common 0.56.0

Common functionality and machinery for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Type-safe string wrappers for message bus patterns, topics, and endpoints.

use std::{fmt::Display, ops::Deref};

use nautilus_core::correctness::{FAILED, check_valid_string_utf8};
use serde::{Deserialize, Serialize};
use ustr::Ustr;

/// Check that a string contains no wildcard characters.
#[inline(always)]
fn check_no_wildcards(value: &Ustr, key: &str) -> anyhow::Result<()> {
    // Check bytes directly - faster than chars() for ASCII wildcards
    if value.as_bytes().iter().any(|&b| b == b'*' || b == b'?') {
        anyhow::bail!("{key} `value` contained invalid characters, was {value}");
    }
    Ok(())
}

/// Marker for subscription patterns. Allows wildcards (`*`, `?`).
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Pattern;

/// Marker for publish topics. No wildcards allowed.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Topic;

/// Marker for direct message endpoints. No wildcards allowed.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Endpoint;

/// A message bus string type parameterized by marker type.
///
/// - `MStr<Pattern>` - for subscriptions, allows wildcards (`*`, `?`)
/// - `MStr<Topic>` - for publishing, no wildcards
/// - `MStr<Endpoint>` - for direct messages, no wildcards
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct MStr<T> {
    value: Ustr,
    #[serde(skip)]
    _marker: std::marker::PhantomData<T>,
}

impl<T> Display for MStr<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.value)
    }
}

impl<T> Deref for MStr<T> {
    type Target = Ustr;

    fn deref(&self) -> &Self::Target {
        &self.value
    }
}

impl<T> AsRef<str> for MStr<T> {
    fn as_ref(&self) -> &str {
        self.value.as_str()
    }
}

impl MStr<Pattern> {
    /// Create a new pattern from a string.
    pub fn pattern<T: AsRef<str>>(value: T) -> Self {
        let value = Ustr::from(value.as_ref());

        Self {
            value,
            _marker: std::marker::PhantomData,
        }
    }
}

impl From<&str> for MStr<Pattern> {
    fn from(value: &str) -> Self {
        Self::pattern(value)
    }
}

impl From<String> for MStr<Pattern> {
    fn from(value: String) -> Self {
        value.as_str().into()
    }
}

impl From<&String> for MStr<Pattern> {
    fn from(value: &String) -> Self {
        value.as_str().into()
    }
}

impl From<MStr<Topic>> for MStr<Pattern> {
    fn from(value: MStr<Topic>) -> Self {
        Self {
            value: value.value,
            _marker: std::marker::PhantomData,
        }
    }
}

impl MStr<Topic> {
    /// Create a new topic from a fully qualified string.
    ///
    /// # Errors
    ///
    /// Returns an error if the topic has white space or invalid characters.
    pub fn topic<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
        let topic = Ustr::from(value.as_ref());
        check_valid_string_utf8(value, stringify!(value))?;
        check_no_wildcards(&topic, stringify!(Topic))?;

        Ok(Self {
            value: topic,
            _marker: std::marker::PhantomData,
        })
    }

    /// Create a topic from an already-interned Ustr.
    ///
    /// # Errors
    ///
    /// Returns an error if the topic is empty, all whitespace, or contains wildcard characters.
    pub fn topic_from_ustr(value: Ustr) -> anyhow::Result<Self> {
        check_valid_string_utf8(value.as_str(), stringify!(value))?;
        check_no_wildcards(&value, stringify!(Topic))?;

        Ok(Self {
            value,
            _marker: std::marker::PhantomData,
        })
    }
}

impl From<&str> for MStr<Topic> {
    fn from(value: &str) -> Self {
        Self::topic(value).expect(FAILED)
    }
}

impl From<String> for MStr<Topic> {
    fn from(value: String) -> Self {
        value.as_str().into()
    }
}

impl From<&String> for MStr<Topic> {
    fn from(value: &String) -> Self {
        value.as_str().into()
    }
}

impl From<Ustr> for MStr<Topic> {
    fn from(value: Ustr) -> Self {
        Self::topic_from_ustr(value).expect(FAILED)
    }
}

impl From<&Ustr> for MStr<Topic> {
    fn from(value: &Ustr) -> Self {
        (*value).into()
    }
}

impl MStr<Endpoint> {
    /// Create a new endpoint from a fully qualified string.
    ///
    /// # Errors
    ///
    /// Returns an error if the endpoint has white space or invalid characters.
    pub fn endpoint<T: AsRef<str>>(value: T) -> anyhow::Result<Self> {
        let endpoint = Ustr::from(value.as_ref());
        check_valid_string_utf8(value, stringify!(value))?;
        check_no_wildcards(&endpoint, stringify!(Endpoint))?;

        Ok(Self {
            value: endpoint,
            _marker: std::marker::PhantomData,
        })
    }

    /// Create an endpoint from an already-interned Ustr.
    ///
    /// # Errors
    ///
    /// Returns an error if the endpoint is empty, all whitespace, or contains wildcard characters.
    pub fn endpoint_from_ustr(value: Ustr) -> anyhow::Result<Self> {
        check_valid_string_utf8(value.as_str(), stringify!(value))?;
        check_no_wildcards(&value, stringify!(Endpoint))?;

        Ok(Self {
            value,
            _marker: std::marker::PhantomData,
        })
    }
}

impl From<&str> for MStr<Endpoint> {
    fn from(value: &str) -> Self {
        Self::endpoint(value).expect(FAILED)
    }
}

impl From<String> for MStr<Endpoint> {
    fn from(value: String) -> Self {
        value.as_str().into()
    }
}

impl From<&String> for MStr<Endpoint> {
    fn from(value: &String) -> Self {
        value.as_str().into()
    }
}

impl From<Ustr> for MStr<Endpoint> {
    fn from(value: Ustr) -> Self {
        Self::endpoint_from_ustr(value).expect(FAILED)
    }
}

#[cfg(test)]
mod tests {
    use proptest::prelude::*;
    use rstest::rstest;

    use super::*;

    #[rstest]
    #[case("data.quotes.BINANCE.BTCUSDT")]
    #[case("events.order.filled")]
    #[case("a")]
    #[case("a.b.c.d.e.f")]
    fn test_topic_valid(#[case] input: &str) {
        let topic = MStr::<Topic>::topic(input).unwrap();
        assert_eq!(topic.as_ref(), input);
    }

    #[rstest]
    #[case("data.*.BINANCE")]
    #[case("events.order.*")]
    #[case("*")]
    #[case("data.quotes.?")]
    #[case("a?b")]
    fn test_topic_rejects_wildcards(#[case] input: &str) {
        assert!(MStr::<Topic>::topic(input).is_err());
    }

    #[rstest]
    #[case("DataEngine.execute")]
    #[case("RiskEngine.process")]
    fn test_endpoint_valid(#[case] input: &str) {
        let endpoint = MStr::<Endpoint>::endpoint(input).unwrap();
        assert_eq!(endpoint.as_ref(), input);
    }

    #[rstest]
    #[case("DataEngine.*")]
    #[case("*.execute")]
    #[case("Risk?Engine")]
    fn test_endpoint_rejects_wildcards(#[case] input: &str) {
        assert!(MStr::<Endpoint>::endpoint(input).is_err());
    }

    #[rstest]
    #[case("data.*")]
    #[case("*.quotes.*")]
    #[case("data.?.BINANCE")]
    #[case("*")]
    #[case("exact.match.no.wildcards")]
    fn test_pattern_accepts_all(#[case] input: &str) {
        let pattern = MStr::<Pattern>::pattern(input);
        assert_eq!(pattern.as_ref(), input);
    }

    #[rstest]
    fn test_topic_to_pattern_conversion() {
        let topic: MStr<Topic> = "data.quotes.BINANCE.BTCUSDT".into();
        let pattern: MStr<Pattern> = topic.into();
        assert_eq!(pattern.as_ref(), "data.quotes.BINANCE.BTCUSDT");
    }

    #[rstest]
    fn test_topic_from_ustr_valid() {
        let ustr = Ustr::from("data.quotes.BINANCE");
        let topic = MStr::<Topic>::topic_from_ustr(ustr).unwrap();
        assert_eq!(topic.as_ref(), "data.quotes.BINANCE");
    }

    #[rstest]
    #[case("")]
    #[case("   ")]
    #[case("\t\n")]
    fn test_topic_from_ustr_rejects_empty_whitespace(#[case] input: &str) {
        let ustr = Ustr::from(input);
        assert!(MStr::<Topic>::topic_from_ustr(ustr).is_err());
    }

    #[rstest]
    #[case("data.*")]
    #[case("a?b")]
    fn test_topic_from_ustr_rejects_wildcards(#[case] input: &str) {
        let ustr = Ustr::from(input);
        assert!(MStr::<Topic>::topic_from_ustr(ustr).is_err());
    }

    #[rstest]
    fn test_endpoint_from_ustr_valid() {
        let ustr = Ustr::from("DataEngine.execute");
        let endpoint = MStr::<Endpoint>::endpoint_from_ustr(ustr).unwrap();
        assert_eq!(endpoint.as_ref(), "DataEngine.execute");
    }

    #[rstest]
    #[case("")]
    #[case("   ")]
    fn test_endpoint_from_ustr_rejects_empty_whitespace(#[case] input: &str) {
        let ustr = Ustr::from(input);
        assert!(MStr::<Endpoint>::endpoint_from_ustr(ustr).is_err());
    }

    #[rstest]
    #[case("Engine.*")]
    #[case("a?b")]
    fn test_endpoint_from_ustr_rejects_wildcards(#[case] input: &str) {
        let ustr = Ustr::from(input);
        assert!(MStr::<Endpoint>::endpoint_from_ustr(ustr).is_err());
    }

    #[rstest]
    fn test_from_impls_equivalent() {
        let s = "test.topic";
        let from_str: MStr<Topic> = s.into();
        let from_string: MStr<Topic> = s.to_string().into();
        let from_string_ref: MStr<Topic> = (&s.to_string()).into();
        let from_ustr: MStr<Topic> = Ustr::from(s).into();

        assert_eq!(from_str, from_string);
        assert_eq!(from_string, from_string_ref);
        assert_eq!(from_string_ref, from_ustr);
    }

    #[rstest]
    fn test_deref_to_ustr() {
        let topic: MStr<Topic> = "test.topic".into();
        let ustr: &Ustr = &topic;
        assert_eq!(ustr.as_str(), "test.topic");
    }

    fn valid_segment() -> impl Strategy<Value = String> {
        "[a-zA-Z][a-zA-Z0-9_]{0,15}".prop_filter("non-empty", |s| !s.is_empty())
    }

    fn valid_topic_string() -> impl Strategy<Value = String> {
        prop::collection::vec(valid_segment(), 1..=5).prop_map(|segs| segs.join("."))
    }

    fn string_with_wildcards() -> impl Strategy<Value = String> {
        prop::collection::vec(
            prop_oneof![
                valid_segment(),
                Just("*".to_string()),
                Just("?".to_string()),
            ],
            1..=5,
        )
        .prop_map(|segs| segs.join("."))
        .prop_filter("must contain wildcard", |s| {
            s.contains('*') || s.contains('?')
        })
    }

    proptest! {
        #[rstest]
        fn prop_topic_roundtrip(s in valid_topic_string()) {
            let topic = MStr::<Topic>::topic(&s).unwrap();
            prop_assert_eq!(topic.as_ref(), s.as_str());
        }

        #[rstest]
        fn prop_endpoint_roundtrip(s in valid_topic_string()) {
            let endpoint = MStr::<Endpoint>::endpoint(&s).unwrap();
            prop_assert_eq!(endpoint.as_ref(), s.as_str());
        }

        #[rstest]
        fn prop_pattern_accepts_wildcards(s in string_with_wildcards()) {
            let pattern = MStr::<Pattern>::pattern(&s);
            prop_assert_eq!(pattern.as_ref(), s.as_str());
        }

        #[rstest]
        fn prop_topic_rejects_wildcards(s in string_with_wildcards()) {
            prop_assert!(MStr::<Topic>::topic(&s).is_err());
        }

        #[rstest]
        fn prop_endpoint_rejects_wildcards(s in string_with_wildcards()) {
            prop_assert!(MStr::<Endpoint>::endpoint(&s).is_err());
        }

        #[rstest]
        fn prop_topic_to_pattern_preserves_value(s in valid_topic_string()) {
            let topic: MStr<Topic> = MStr::topic(&s).unwrap();
            let pattern: MStr<Pattern> = topic.into();
            prop_assert_eq!(pattern.as_ref(), s.as_str());
        }

        #[rstest]
        fn prop_from_impls_consistent(s in valid_topic_string()) {
            let from_str: MStr<Topic> = s.as_str().into();
            let from_string: MStr<Topic> = s.clone().into();
            let from_ustr: MStr<Topic> = Ustr::from(&s).into();

            prop_assert_eq!(from_str, from_string);
            prop_assert_eq!(from_string, from_ustr);
        }
    }
}