mqtt-topic-engine 0.1.1

High-performance MQTT topic pattern matching and routing 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
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
<div align="center">

# MQTT Topic Engine

**High-performance MQTT topic pattern matching and routing engine for Rust**

[![Crates.io](https://img.shields.io/crates/v/mqtt-topic-engine.svg)](https://crates.io/crates/mqtt-topic-engine)
[![Documentation](https://docs.rs/mqtt-topic-engine/badge.svg)](https://docs.rs/mqtt-topic-engine)
[![License: MIT OR Apache-2.0](https://img.shields.io/badge/License-MIT%20OR%20Apache--2.0-blue.svg)](https://github.com/holovskyi/mqtt-typed-client/blob/main/LICENSE-MIT)

Zero-dependency (by default) topic pattern parser, matcher, and router for MQTT applications

</div>

> **This crate is the topic-routing engine behind [`mqtt-typed-client`]https://crates.io/crates/mqtt-typed-client.**
> If you want a type-safe async MQTT client — connect, typed publish/subscribe, automatic
> topic routing and (de)serialization — reach for
> [`mqtt-typed-client`]https://crates.io/crates/mqtt-typed-client; that's what most users
> want. This crate is the matching/routing core on its own: dependency-light and
> client-agnostic, for when you're building your own MQTT layer and just need the matcher.

## Contents

- [Features]#features
- [Installation]#installation
- [Quick Start]#quick-start
- [Core Concepts]#core-concepts
- [Advanced Examples]#advanced-examples
- [Use Cases]#use-cases
- [Configuration]#configuration
- [Performance]#performance
- [Integration]#integration
- [Alternatives]#alternatives

## Features

- **Pattern Parsing** - Parse MQTT topic patterns with support for wildcards (`+`, `#`) and named parameters
- **Fast Matching** - Efficient tree-based topic matching algorithm with O(n) complexity
- **Optional Caching** - LRU cache for topic match results to boost performance (optional `lru-cache` feature)
- **Message Routing** - Route incoming messages to multiple subscribers based on topic patterns (optional `router` feature)
- **Named Parameters** - Extract topic segments as named parameters (`{sensor_id}`, `{path:#}`)
- **Modular Design** - Enable only the features you need for minimal binary size
- **Well Tested** - Comprehensive test suite with 100+ tests covering edge cases
- **Lightweight** - Minimal dependencies, optimized for embedded and performance-critical applications
- **MQTT Client Agnostic** - Works with any MQTT client library (rumqttc, paho-mqtt, ntex-mqtt, etc.)

## Installation

Add to your `Cargo.toml`:

```toml
[dependencies]
mqtt-topic-engine = "0.1.0"
```

### Feature Flags

The library is highly modular with optional features:

```toml
# Default: all features enabled
mqtt-topic-engine = "0.1.0"

# Minimal: only pattern matching and validation
mqtt-topic-engine = { version = "0.1.0", default-features = false }

# Pattern matching + routing (without LRU cache)
mqtt-topic-engine = { version = "0.1.0", default-features = false, features = ["router"] }

# Pattern matching + LRU cache (without routing)
mqtt-topic-engine = { version = "0.1.0", default-features = false, features = ["lru-cache"] }

# With specific MQTT client integration
mqtt-topic-engine = { version = "0.1.0", features = ["rumqttc"] }
mqtt-topic-engine = { version = "0.1.0", features = ["paho-mqtt"] }
mqtt-topic-engine = { version = "0.1.0", features = ["ntex-mqtt"] }
```

**Available features:**

| Feature | Description | Default |
|---------|-------------|----------|
| `router` | Topic routing with `TopicRouter` and `TopicMatcher` | Yes |
| `lru-cache` | LRU caching for pattern matching results | Yes |
| `rumqttc` | Integration with rumqttc MQTT client (QoS conversion) | No |
| `paho-mqtt` | Integration with paho-mqtt client (QoS conversion) | No |
| `ntex-mqtt` | Integration with ntex-mqtt client (QoS conversion) | No |

## Quick Start

### Basic Pattern Matching

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy};

// Parse a topic pattern
let pattern = TopicPatternPath::new_from_string(
    "sensors/+/temperature",
    CacheStrategy::NoCache
)?;

// Check if a topic matches - just pass a string!
let topic_match = pattern.try_match_str("sensors/kitchen/temperature")?;

println!("Topic matched: {}", topic_match.topic_path());
# Ok(())
# }
```

### Named Parameters

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy};

// Pattern with named parameters
let pattern = TopicPatternPath::new_from_string(
    "devices/{location}/{device_id}/status",
    CacheStrategy::NoCache
)?;

let topic_match = pattern.try_match_str("devices/kitchen/sensor001/status")?;

// Extract parameters by name
let location = topic_match.get_named_param("location").unwrap();
let device_id = topic_match.get_named_param("device_id").unwrap();

println!("Location: {}, Device: {}", location, device_id);
// Output: Location: kitchen, Device: sensor001
# Ok(())
# }
```

### Multi-Level Wildcards

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy};

// Match all subtopics under logs/
let pattern = TopicPatternPath::new_from_string(
    "logs/{service}/{details:#}",
    CacheStrategy::NoCache
)?;

let topic_match = pattern.try_match_str("logs/api/v1/users/create")?;

let service = topic_match.get_named_param("service").unwrap();
let details = topic_match.get_named_param("details").unwrap();

println!("Service: {}, Path: {}", service, details);
// Output: Service: api, Path: v1/users/create
# Ok(())
# }
```

## Core Concepts

### Topic Patterns

MQTT topic engine supports standard MQTT wildcards plus named parameters:

| Pattern | Description | Example | Matches |
|---------|-------------|---------|---------|
| `sensors/temperature` | Exact match | `sensors/temperature` | `sensors/temperature` only |
| `sensors/+/data` | Single-level wildcard | `sensors/+/data` | `sensors/kitchen/data`, `sensors/bedroom/data` |
| `sensors/#` | Multi-level wildcard | `sensors/#` | `sensors`, `sensors/temp`, `sensors/kitchen/temp` |
| `sensors/{room}/temp` | Named parameter | `sensors/{room}/temp` | Same as `sensors/+/temp` but extracts `room` |
| `logs/{app}/{path:#}` | Named multi-level | `logs/{app}/{path:#}` | Same as `logs/+/#` but extracts both |

### Topic Router

Route messages to multiple subscribers based on patterns:

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicRouter, TopicPatternPath, TopicPath, CacheStrategy, QoS};

let mut router = TopicRouter::new();

// Add subscriptions
let pattern1 = TopicPatternPath::new_from_string("sensors/+/temperature", CacheStrategy::NoCache)?;
let (needs_subscribe, sub_id1) = router.add_subscription(
    pattern1,
    QoS::AtLeastOnce,
    "handler1"  // Your subscriber data (any type)
);

let pattern2 = TopicPatternPath::new_from_string("sensors/#", CacheStrategy::NoCache)?;
let (needs_subscribe, sub_id2) = router.add_subscription(
    pattern2,
    QoS::AtMostOnce,
    "handler2"
);

// Route incoming message
let topic = TopicPath::new("sensors/kitchen/temperature");
let subscribers = router.get_subscribers(&topic);

for (sub_id, (pattern, _qos), handler) in subscribers {
    println!("Matched subscriber: {:?} with pattern: {}", sub_id, pattern.topic_pattern());
    // Forward message to handler
}
# let _ = (needs_subscribe, sub_id1, sub_id2);
# Ok(())
# }
```

### Parameter Binding

Bind specific values to parameters for filtered subscriptions:

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy};

let pattern = TopicPatternPath::new_from_string(
    "sensors/{location}/{sensor_type}/{sensor_id}/data",
    CacheStrategy::NoCache
)?;

// Only subscribe to temperature sensors in the kitchen
let filtered = pattern
    .bind_parameter("location", "kitchen")?
    .bind_parameter("sensor_type", "temperature")?;

println!("{}", filtered.mqtt_pattern());
// Output: sensors/kitchen/temperature/+/data
# Ok(())
# }
```

This is useful for:
- Dynamic subscription filtering
- Multi-tenant applications
- Selective message routing

## Advanced Examples

### LRU Caching for Performance

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy};
use std::num::NonZeroUsize;

// Create pattern with LRU cache of 1000 entries
let cache_size = NonZeroUsize::new(1000).unwrap();
let pattern = TopicPatternPath::new_from_string(
    "sensors/{location}/{device}/data",
    CacheStrategy::Lru(cache_size)
)?;

// First match - computed and cached
let match1 = pattern.try_match_str("sensors/kitchen/temp001/data")?;

// Second match to same topic - retrieved from cache (faster!)
let match2 = pattern.try_match_str("sensors/kitchen/temp001/data")?;
# let _ = (match1, match2);
# Ok(())
# }
```

### Topic Pattern Validation

```rust
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy, TopicPatternError};

// Valid patterns
assert!(TopicPatternPath::new_from_string("sensors/+/data", CacheStrategy::NoCache).is_ok());
assert!(TopicPatternPath::new_from_string("logs/#", CacheStrategy::NoCache).is_ok());
assert!(TopicPatternPath::new_from_string("home/{room}/temperature", CacheStrategy::NoCache).is_ok());

// Invalid patterns - # wildcard must be last
assert!(matches!(
    TopicPatternPath::new_from_string("sensors/#/invalid", CacheStrategy::NoCache),
    Err(TopicPatternError::HashPosition { .. })
));

// Empty pattern not allowed
assert!(matches!(
    TopicPatternPath::new_from_string("", CacheStrategy::NoCache),
    Err(TopicPatternError::EmptyTopic)
));
```

### Building Topics for Publishing

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy};

let pattern = TopicPatternPath::new_from_string(
    "devices/{device_type}/{device_id}/command",
    CacheStrategy::NoCache
)?;

// Format topic with parameters
let topic = pattern.format_topic(&[&"sensor", &42])?;
println!("{}", topic);
// Output: devices/sensor/42/command

// Type-safe: compile error if wrong number of parameters
// let invalid = pattern.format_topic(&[&"sensor"])?; // Runtime error!
# Ok(())
# }
```

### Managing Subscriptions

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicRouter, TopicPatternPath, CacheStrategy, QoS};

let mut router = TopicRouter::new();

// Add subscription
let pattern = TopicPatternPath::new_from_string("sensors/+/data", CacheStrategy::NoCache)?;
let (needs_mqtt_subscribe, sub_id) = router.add_subscription(
    pattern,
    QoS::AtLeastOnce,
    "my_handler"
);

if needs_mqtt_subscribe {
    // Subscribe to MQTT broker
    println!("New subscription needed on broker");
}

// Later: remove subscription
let (needs_mqtt_unsubscribe, pattern) = router.unsubscribe(&sub_id)?;

if needs_mqtt_unsubscribe {
    // Unsubscribe from MQTT broker
    println!("Should unsubscribe from broker: {}", pattern.mqtt_pattern());
}
# Ok(())
# }
```

### QoS Aggregation

A `TopicRouter` is more than a `pattern -> handler` map: it tracks the **maximum
QoS** requested across all subscribers of the same pattern and tells you, via the
`needs_subscribe` flag, exactly when a broker action is required. You only need to
(re)subscribe on the broker when a pattern is new or when its aggregated QoS rises —
adding another subscriber at the same or a lower QoS needs no wire traffic at all.

```rust
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicRouter, TopicPatternPath, CacheStrategy, QoS};

let mut router = TopicRouter::new();
let pattern = || TopicPatternPath::new_from_string("sensors/+/data", CacheStrategy::NoCache);

// First subscriber on this pattern -> a broker subscription is needed.
let (needs_subscribe, _id1) = router.add_subscription(pattern()?, QoS::AtMostOnce, "h1");
assert!(needs_subscribe);

// Same pattern, but a HIGHER QoS -> must resubscribe at the higher level.
let (needs_subscribe, _id2) = router.add_subscription(pattern()?, QoS::AtLeastOnce, "h2");
assert!(needs_subscribe);

// Same pattern, equal-or-lower QoS -> aggregated max is unchanged, no broker action.
let (needs_subscribe, _id3) = router.add_subscription(pattern()?, QoS::AtMostOnce, "h3");
assert!(!needs_subscribe);
# Ok(())
# }
```

### Wildcard Patterns

```rust
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, TopicPath, CacheStrategy};
use std::sync::Arc;

// Anonymous wildcards (standard MQTT)
let pattern1 = TopicPatternPath::new_from_string("home/+/temperature", CacheStrategy::NoCache)?;

// Named wildcards (extracts parameter)
let pattern2 = TopicPatternPath::new_from_string("home/{room}/temperature", CacheStrategy::NoCache)?;

// Matching one topic against several patterns: build the Arc<TopicPath> once
// and share it via cheap Arc::clone (no re-parsing per pattern).
let topic = Arc::new(TopicPath::new("home/kitchen/temperature"));

// Both match the same topics
let match1 = pattern1.try_match(topic.clone())?;
let match2 = pattern2.try_match(topic)?;

// But only named wildcards extract parameters
assert!(match2.get_named_param("room").is_some());
println!("Room: {}", match2.get_named_param("room").unwrap());
// Output: Room: kitchen
# let _ = match1;
# Ok(())
# }
```

### Complex Routing Scenarios

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicRouter, TopicPatternPath, TopicPath, CacheStrategy, QoS};

let mut router = TopicRouter::<String>::new();

// Multiple overlapping patterns
let patterns = vec![
    ("home/kitchen/temperature", "exact_handler"),
    ("home/+/temperature", "any_room_temp_handler"),
    ("home/kitchen/+", "kitchen_all_sensors_handler"),
    ("home/#", "all_home_handler"),
    ("+/kitchen/#", "all_kitchen_subtopics_handler"),
];

for (pattern_str, handler_name) in patterns {
    let pattern = TopicPatternPath::new_from_string(pattern_str, CacheStrategy::NoCache)?;
    router.add_subscription(pattern, QoS::AtMostOnce, handler_name.to_string());
}

// Check which handlers receive the message
let topic = TopicPath::new("home/kitchen/temperature");
let subscribers = router.get_subscribers(&topic);

println!("Message 'home/kitchen/temperature' matches {} subscribers:", subscribers.len());
for (sub_id, (pattern, _qos), handler) in subscribers {
    println!("  - {} (pattern: {})", handler, pattern.topic_pattern());
    let _ = sub_id;
}

/* Output:
Message 'home/kitchen/temperature' matches 5 subscribers:
  - exact_handler (pattern: home/kitchen/temperature)
  - any_room_temp_handler (pattern: home/+/temperature)
  - kitchen_all_sensors_handler (pattern: home/kitchen/+)
  - all_home_handler (pattern: home/#)
  - all_kitchen_subtopics_handler (pattern: +/kitchen/#)
*/
# Ok(())
# }
```

### Reconnection: Resubscribe After Broker Restart

When the broker connection drops, you must replay your subscriptions. The router
gives you the **deduplicated** set of patterns, each already collapsed to the
**maximum QoS** among its subscribers, so you resubscribe each distinct topic
exactly once at the correct level:

```rust
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicRouter, TopicPatternPath, CacheStrategy, QoS};

let mut router = TopicRouter::new();
let pattern = |s| TopicPatternPath::new_from_string(s, CacheStrategy::NoCache);

// Two subscribers share one pattern at different QoS; another pattern is distinct.
router.add_subscription(pattern("sensors/+/data")?, QoS::AtMostOnce, "h1");
router.add_subscription(pattern("sensors/+/data")?, QoS::ExactlyOnce, "h2");
router.add_subscription(pattern("alerts/#")?, QoS::AtLeastOnce, "h3");

// On reconnect, replay exactly the distinct broker subscriptions, each at the
// maximum QoS aggregated across its subscribers - no duplicates.
let to_resubscribe = router.get_topics_for_resubscribe();
assert_eq!(to_resubscribe.len(), 2);
assert_eq!(to_resubscribe.get("sensors/+/data").copied(), Some(QoS::ExactlyOnce));
assert_eq!(to_resubscribe.get("alerts/#").copied(), Some(QoS::AtLeastOnce));

for (topic_pattern, qos) in &to_resubscribe {
    // client.subscribe(topic_pattern.as_ref(), (*qos).to_rumqttc()).await?;
    let _ = (topic_pattern, qos);
}
# Ok(())
# }
```

### Integration with MQTT Clients

#### With rumqttc

```rust,ignore
use mqtt_topic_engine::{TopicRouter, TopicPatternPath, TopicPath, CacheStrategy};
use rumqttc::{AsyncClient, MqttOptions, QoS, Event, Packet};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Setup MQTT client
    let mqtt_options = MqttOptions::new("test-client", "broker.hivemq.com", 1883);
    let (client, mut eventloop) = AsyncClient::new(mqtt_options, 10);
    
    // Setup topic router
    let mut router = TopicRouter::<String>::new();
    
    // Add subscription with automatic broker subscription
    let pattern = TopicPatternPath::new_from_string("sensors/+/data", CacheStrategy::NoCache)?;
    let (needs_subscribe, _sub_id) = router.add_subscription(
        pattern.clone(),
        QoS::AtLeastOnce,
        "sensor_handler".to_string()
    );
    
    if needs_subscribe {
        client.subscribe(pattern.mqtt_pattern().as_ref(), QoS::AtLeastOnce).await?;
    }
    
    // Handle incoming messages
    loop {
        match eventloop.poll().await {
            Ok(Event::Incoming(Packet::Publish(publish))) => {
                let topic = TopicPath::new(publish.topic.as_str());
                let subscribers = router.get_subscribers(&topic);
                
                for (_sub_id, (pattern, _qos), handler) in subscribers {
                    println!("Handler '{}' received message on pattern: {}", 
                        handler, pattern.topic_pattern());
                    
                    // Route to appropriate handler
                    // process_message(handler, &publish.payload);
                }
            }
            Ok(_) => {}
            Err(e) => {
                eprintln!("Error: {:?}", e);
                break;
            }
        }
    }
    
    Ok(())
}
```

## Use Cases

### IoT Device Management

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy};

// Subscribe to all sensors in a specific location
let pattern = TopicPatternPath::new_from_string(
    "iot/{building}/{floor}/sensors/{sensor_type}/{sensor_id}/telemetry",
    CacheStrategy::NoCache
)?;

let filtered = pattern
    .bind_parameter("building", "headquarters")?
    .bind_parameter("floor", "3")?;

println!("{}", filtered.mqtt_pattern());
// Output: iot/headquarters/3/sensors/+/+/telemetry
# Ok(())
# }
```

### Log Aggregation

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy};

// Route logs by service and severity
let pattern = TopicPatternPath::new_from_string(
    "logs/{service}/{severity}/{details:#}",
    CacheStrategy::NoCache
)?;

let topic_match = pattern.try_match_str("logs/api/error/auth/invalid-token")?;

let service = topic_match.get_named_param("service").unwrap();
let severity = topic_match.get_named_param("severity").unwrap();
let details = topic_match.get_named_param("details").unwrap();

if severity == "error" {
    println!("ERROR in {}: {}", service, details);
}
# Ok(())
# }
```

### Multi-Tenant Applications

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy};

// Each tenant gets isolated topic space
let pattern = TopicPatternPath::new_from_string(
    "tenant/{tenant_id}/devices/{device_id}/events",
    CacheStrategy::NoCache
)?;

// Tenant A only sees their devices (clone: bind_parameter consumes the pattern)
let tenant_a_pattern = pattern.clone().bind_parameter("tenant_id", "tenant-a")?;

// Tenant B only sees their devices
let tenant_b_pattern = pattern.bind_parameter("tenant_id", "tenant-b")?;
# let _ = (tenant_a_pattern, tenant_b_pattern);
# Ok(())
# }
```

### Working with ArcStr

Under the hood, `mqtt-topic-engine` uses `ArcStr` for efficient string handling with cheap cloning.
You can pass regular `&str` or `String` values, and they will be converted automatically:

```rust,no_run
# fn main() {
use mqtt_topic_engine::TopicPath;
use arcstr::ArcStr;

// All of these work - the API accepts impl Into<ArcStr>:
let topic1 = TopicPath::new("sensors/temp");                    // &str
let topic2 = TopicPath::new(String::from("sensors/temp"));      // String  
let topic3 = TopicPath::new(ArcStr::from("sensors/temp"));      // ArcStr directly

// For performance-critical code, reuse ArcStr to avoid allocations:
let topic_string = ArcStr::from("sensors/temperature");
let topic_a = TopicPath::new(topic_string.clone());  // Cheap clone (reference counted)
let topic_b = TopicPath::new(topic_string.clone());  // Another cheap clone
let topic_c = TopicPath::new(topic_string);          // Move (also cheap)
# let _ = (topic1, topic2, topic3, topic_a, topic_b, topic_c);
# }
```

**When to use `ArcStr` directly:**

- Storing topic strings for reuse (cheap cloning via reference counting)
- High-frequency topic creation from the same string values
- Sharing topic strings across threads (`ArcStr` is `Send + Sync`)

Not needed for simple one-off topic matching — just use `&str`, it's simpler.

## Configuration

### Cache Strategies

```rust,no_run
# fn main() {
use mqtt_topic_engine::CacheStrategy;
use std::num::NonZeroUsize;

// No caching (minimal memory, recompute every match)
let no_cache = CacheStrategy::NoCache;

// LRU cache with 100 entries (balance memory/performance)
// Note: requires the 'lru-cache' feature (enabled by default)
let lru_100 = CacheStrategy::Lru(NonZeroUsize::new(100).unwrap());

// LRU cache with 10000 entries (high performance, more memory)
let lru_10k = CacheStrategy::Lru(NonZeroUsize::new(10000).unwrap());
# let _ = (no_cache, lru_100, lru_10k);
# }
```

**When to use caching:**

- Repeated matches to the same topics (e.g., sensor data every second)
- High message throughput
- Complex patterns with multiple wildcards

Skip caching for unique (non-repeating) topics — no cache benefit — and in memory-constrained environments.

### Minimal Configuration for Embedded Systems

For resource-constrained environments, use only the core pattern matching without routing or caching:

```toml
[dependencies]
mqtt-topic-engine = { version = "0.1.0", default-features = false }
```

```rust,no_run
# fn main() -> Result<(), Box<dyn std::error::Error>> {
use mqtt_topic_engine::{TopicPatternPath, CacheStrategy};

// Minimal footprint: only pattern validation and matching
let pattern = TopicPatternPath::new_from_string(
    "sensors/{location}/data",
    CacheStrategy::NoCache  // No LRU dependency
)?;

let topic_match = pattern.try_match_str("sensors/kitchen/data")?;

if let Some(location) = topic_match.get_named_param("location") {
    // Process message for specific location
#   let _ = location;
}
# Ok(())
# }
```

**Benefits of minimal configuration:**

- Smallest binary size (~10KB additional code)
- No heap allocations for routing or caching
- Suitable for microcontrollers and embedded systems
- Still provides full pattern matching and parameter extraction

## Performance

Topic engine is designed for high-performance applications:

- **Matching:** O(n) where n = number of topic segments  
- **Routing:** O(m) where m = number of matching subscriptions
- **Memory:** Optimized with `arcstr` and `smallvec` for minimal allocations
- **Caching:** Optional LRU cache for repeated topic matches

**Expected performance characteristics:**
- Pattern parsing: ~1-2 μs per pattern (typical case: 3-5 segments)
- Topic matching: ~200-500 ns per match without cache
- Topic matching: ~20-50 ns with cache hit (LRU lookup)
- Router lookup: ~500 ns - 2 μs (depends on subscription count and matches)

> **Note:** Actual performance depends on pattern complexity, topic depth, and hardware.
> Always benchmark with your specific workload.

## Integration

### MQTT Client Support

The library includes its own QoS type and provides optional integration with popular MQTT clients through seamless QoS type conversions:

**Supported MQTT clients:**
- **rumqttc** - Enable with `features = ["rumqttc"]`
- **paho-mqtt** - Enable with `features = ["paho-mqtt"]`
- **ntex-mqtt** - Enable with `features = ["ntex-mqtt"]`
- **Zero-dependency** - Works standalone without any MQTT client (default)

```toml
# With rumqttc integration
mqtt-topic-engine = { version = "0.1.0", features = ["rumqttc"] }

# With paho-mqtt integration
mqtt-topic-engine = { version = "0.1.0", features = ["paho-mqtt"] }

# With ntex-mqtt integration
mqtt-topic-engine = { version = "0.1.0", features = ["ntex-mqtt"] }

# Zero-dependency (no features)
mqtt-topic-engine = "0.1.0"
```

The core matching and routing logic is already client-agnostic and works with any MQTT client library.

### QoS Type Conversions

When MQTT client integration features are enabled, the library provides automatic QoS type conversions:

```rust,ignore
use mqtt_topic_engine::QoS;

// From client-specific QoS to mqtt-topic-engine QoS
#[cfg(feature = "rumqttc")]
let qos: QoS = rumqttc::QoS::AtLeastOnce.into();

#[cfg(feature = "paho-mqtt")]
let qos: QoS = paho_mqtt::QoS::AtLeastOnce.into();

#[cfg(feature = "ntex-mqtt")]
let qos: QoS = ntex_mqtt::QoS::AtLeastOnce.into();

// To client-specific QoS from mqtt-topic-engine QoS
let engine_qos = QoS::AtLeastOnce;

#[cfg(feature = "rumqttc")]
let rumqttc_qos: rumqttc::QoS = engine_qos.to_rumqttc();

#[cfg(feature = "paho-mqtt")]
let paho_qos: paho_mqtt::QoS = engine_qos.to_paho_mqtt();

#[cfg(feature = "ntex-mqtt")]
let ntex_qos: ntex_mqtt::QoS = engine_qos.to_ntex_mqtt();
```

This enables seamless integration with any supported MQTT client without manual QoS type conversions.

## API Documentation

For detailed API documentation, visit [docs.rs/mqtt-topic-engine](https://docs.rs/mqtt-topic-engine).

## Alternatives

Standalone MQTT topic-matching crates are rare — matching is usually embedded
inside a full client or broker rather than offered as a reusable building block:

- **[`mqtt_topic_tree`]https://crates.io/crates/mqtt_topic_tree** — a lightweight
  topic tree for MQTT routing, and the closest standalone equivalent. This engine
  adds named-parameter extraction, pluggable LRU caching, a typed `QoS` with
  conversions to/from popular clients, and a zero-dependency embedded mode.
- The matching built into full clients and brokers (e.g. **rumqttc**, **ntex-mqtt**) —
  reach for those when you don't need a reusable, client-agnostic matcher.

Choose `mqtt-topic-engine` when you want MQTT wildcard matching with parameter
extraction as a small, client-agnostic component — usable with rumqttc, paho-mqtt,
ntex-mqtt, or no MQTT client at all.

## Contributing

Contributions are welcome! This library is part of the [mqtt-typed-client](https://github.com/holovskyi/mqtt-typed-client) project.

## License

This project is licensed under either of

 * Apache License, Version 2.0 ([LICENSE-APACHE]https://github.com/holovskyi/mqtt-typed-client/blob/main/LICENSE-APACHE or <http://www.apache.org/licenses/LICENSE-2.0>)
 * MIT license ([LICENSE-MIT]https://github.com/holovskyi/mqtt-typed-client/blob/main/LICENSE-MIT or <http://opensource.org/licenses/MIT>)

at your option.

## See Also

- [mqtt-typed-client]https://github.com/holovskyi/mqtt-typed-client - Type-safe MQTT client using this engine
- [rumqttc]https://github.com/bytebeamio/rumqtt - Async MQTT client for Rust
- [MQTT Specification]https://mqtt.org/ - Official MQTT protocol documentation