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
//! Parsed MQTT topic patterns.
//!
//! [`TopicPatternPath`] holds a validated sequence of [`TopicPatternItem`]
//! segments (literals, `+` single-level and `#` multi-level wildcards) together
//! with bound parameter values, and renders back to the wire MQTT pattern.

use std::collections::HashSet;
use std::convert::TryFrom;
use std::fmt::{self, Display, Write};
use std::slice::Iter;
use std::sync::Arc;
#[cfg(feature = "lru-cache")]
use std::sync::Mutex;

use arcstr::ArcStr;
#[cfg(feature = "lru-cache")]
use lru::LruCache;
use smallvec::SmallVec;
use thiserror::Error;

use crate::cache_strategy::CacheStrategy;
use crate::topic_match::{TopicMatch, TopicMatchError, TopicPath};
use crate::topic_pattern_item::{TopicPatternError, TopicPatternItem};

/// Error types for formatting topics with parameters
#[derive(Error, Debug, Clone, PartialEq, Eq)]
pub enum TopicFormatError {
	/// Attempted to format a topic with a hash wildcard (#) which is not allowed
	#[error("Cannot format topic with # wildcard for publishing")]
	HashWildcardNotSupported,

	/// Parameter count mismatch when formatting a topic
	#[error(
		"Parameter count mismatch: expected {expected}, provided {provided}"
	)]
	ParameterCountMismatch {
		/// Expected number of parameters
		expected: usize,
		/// Number of parameters actually provided
		provided: usize,
	},
	/// Error during formatting, e.g. invalid parameter type
	#[error("Error formatting topic")]
	FormatError {
		#[source]
		/// The underlying formatting error
		source: fmt::Error,
	},
}

impl From<fmt::Error> for TopicFormatError {
	fn from(source: fmt::Error) -> Self {
		TopicFormatError::FormatError { source }
	}
}

/// Parsed MQTT topic pattern with wildcard support
#[derive(Debug)]
pub struct TopicPatternPath {
	template_pattern: ArcStr, // original topic pattern as a string
	mqtt_topic_subscription: ArcStr, // mqtt topic pattern with wildcards "sensors/+/data"
	segments: Vec<TopicPatternItem>,
	/// Optional LRU cache for topic match results.
	///
	/// Uses `Mutex` instead of `RefCell` for interior mutability because:
	/// 1. This struct needs to be `Send + Sync` to work in the actor-based subscription manager
	/// 2. Although used in single-threaded actor context, `RefCell` is not `Send`
	/// 3. No contention occurs since access is serialized within the actor's event loop
	/// 4. `Mutex` provides the same interior mutability as `RefCell` but with `Send + Sync`
	///
	/// **Note:** This field is only available with the `lru-cache` feature enabled.
	#[cfg(feature = "lru-cache")]
	match_cache: Option<Mutex<LruCache<ArcStr, Arc<TopicMatch>>>>,

	parameter_bindings: Option<SmallVec<[(ArcStr, ArcStr); 4]>>,
}

impl Clone for TopicPatternPath {
	fn clone(&self) -> Self {
		Self {
			template_pattern: self.template_pattern.clone(),
			mqtt_topic_subscription: self.mqtt_topic_subscription.clone(),
			segments: self.segments.clone(),
			#[cfg(feature = "lru-cache")]
			match_cache: self.match_cache.as_ref().map(|cache| {
				let cache_guard = cache.lock().unwrap();
				let capacity = cache_guard.cap();
				drop(cache_guard);
				Mutex::new(LruCache::new(capacity))
			}),
			parameter_bindings: self.parameter_bindings.clone(),
		}
	}
}

impl TopicPatternPath {
	/// Creates a topic pattern from string with optional caching.
	pub fn new_from_string(
		topic_pattern: impl Into<ArcStr>,
		cache_strategy: CacheStrategy,
	) -> Result<Self, TopicPatternError> {
		let topic_pattern = topic_pattern.into();
		if topic_pattern.is_empty() || topic_pattern.trim().is_empty() {
			return Err(TopicPatternError::EmptyTopic);
		}

		let segments: Result<Vec<_>, _> = topic_pattern
			.split('/')
			.map(|s| topic_pattern.substr_from(s))
			.map(TopicPatternItem::try_from)
			.collect();

		let segments = segments?;

		//Error on duplicate named parameters
		let mut seen_names = HashSet::new();
		for segment in &segments {
			if let Some(name) = segment.param_name() {
				if !seen_names.insert(name.to_string()) {
					return Err(TopicPatternError::wildcard_usage(
						segment.as_str(),
					));
				}
			}
		}

		if let Some(hash_pos) = segments
			.iter()
			.position(|s| matches!(*s, TopicPatternItem::Hash(_)))
		{
			if hash_pos != segments.len() - 1 {
				return Err(TopicPatternError::hash_position(
					topic_pattern.as_str(),
				));
			}
		}

		#[cfg(feature = "lru-cache")]
		let match_cache = match cache_strategy {
			| CacheStrategy::Lru(cache_size) => {
				Some(Mutex::new(LruCache::new(cache_size)))
			}
			| CacheStrategy::NoCache => None,
		};

		#[cfg(not(feature = "lru-cache"))]
		{
			if let Some(capacity) = cache_strategy.capacity() {
				tracing::warn!(
					capacity = capacity.get(),
					pattern = %topic_pattern,
					"LRU cache strategy provided for topic pattern '{}' with capacity {}, \
					but 'lru-cache' feature is disabled. Caching will not be used. \
					Enable 'lru-cache' feature in Cargo.toml to use caching.",
					topic_pattern,
					capacity.get()
				);
			}
		}

		Ok(Self {
			template_pattern: topic_pattern,
			mqtt_topic_subscription: ArcStr::from(
				Self::to_mqtt_subscription_pattern(&segments),
			),
			segments,
			#[cfg(feature = "lru-cache")]
			match_cache,
			parameter_bindings: None,
		})
	}

	/// Get the cache strategy of this topic pattern.
	pub fn cache_strategy(&self) -> CacheStrategy {
		#[cfg(feature = "lru-cache")]
		{
			match &self.match_cache {
				| Some(cache_mutex) => {
					let cache_guard = cache_mutex.lock().unwrap();
					CacheStrategy::Lru(cache_guard.cap())
				}
				| None => CacheStrategy::NoCache,
			}
		}
		#[cfg(not(feature = "lru-cache"))]
		{
			CacheStrategy::NoCache
		}
	}

	/// Returns the current parameter bindings, if any.
	pub fn parameter_bindings(
		&self,
	) -> Option<&SmallVec<[(ArcStr, ArcStr); 4]>> {
		self.parameter_bindings.as_ref()
	}

	/// Returns the bound value for a named parameter, if it exists.
	pub fn get_bound_value(&self, param_name: Option<&str>) -> Option<&ArcStr> {
		let name = param_name?; // Якщо None - одразу повертаємо None
		self.parameter_bindings
			.as_ref()?
			.iter()
			.find(|(binding_name, _)| binding_name == name)
			.map(|(_, value)| value)
	}

	#[cfg(all(test, feature = "router"))]
	/// Creates a topic pattern from segments directly, useful for testing.
	pub(crate) fn new_from_segments(
		segments: &[TopicPatternItem],
	) -> Result<Self, TopicPatternError> {
		let topic_pattern = ArcStr::from(Self::to_template_pattern(segments));
		let pattern = Self {
			mqtt_topic_subscription: ArcStr::from(
				Self::to_mqtt_subscription_pattern(segments),
			),
			template_pattern: topic_pattern.clone(),
			segments: segments.to_vec(),
			#[cfg(feature = "lru-cache")]
			match_cache: None,
			parameter_bindings: None,
		};
		if let Some(hash_pos) = segments
			.iter()
			.position(|s| matches!(*s, TopicPatternItem::Hash(_)))
		{
			if hash_pos != segments.len() - 1 {
				return Err(TopicPatternError::hash_position(
					topic_pattern.as_str(),
				));
			}
		}
		Ok(pattern)
	}

	/// Returns MQTT pattern with wildcards for broker subscription with bound parameters applied.
	pub fn mqtt_pattern(&self) -> ArcStr {
		match &self.parameter_bindings {
			| Some(bindings) => {
				let new_segments = self.apply_bindings_to_segments(bindings);
				ArcStr::from(Self::to_mqtt_subscription_pattern(&new_segments))
			}
			| None => self.mqtt_topic_subscription.clone(),
		}
	}

	/// Resolves bound parameters into concrete segments
	///
	/// Returns segments with bound parameters replaced by their values.
	/// Unbound wildcards remain as wildcards.
	pub fn resolve_bound_segments(&self) -> Vec<TopicPatternItem> {
		if let Some(ref bindings) = self.parameter_bindings {
			self.apply_bindings_to_segments(bindings)
		} else {
			self.segments.clone()
		}
	}

	// Internal helper that applies bindings to segments
	fn apply_bindings_to_segments(
		&self,
		bindings: &[(ArcStr, ArcStr)],
	) -> Vec<TopicPatternItem> {
		let mut new_segments = self.segments.clone();

		for (param_name, value) in bindings {
			if let Some(segment_pos) = new_segments.iter().position(|segment| {
                matches!(segment, TopicPatternItem::Plus(Some(name)) if name == param_name)
            }) {
                new_segments[segment_pos] = TopicPatternItem::Str(value.into());
            } else {
				tracing::debug!(
					pattern = %self.topic_pattern(),
					"Parameter '{param_name}' not found in pattern"
				);
            }
		}

		new_segments
	}

	/// Returns original pattern with named parameters.
	pub fn topic_pattern(&self) -> ArcStr {
		self.template_pattern.clone()
	}

	/// Returns true if pattern has no segments.
	pub fn is_empty(&self) -> bool {
		self.segments.is_empty()
	}

	/// Returns true if pattern contains multi-level wildcard (#).
	pub fn contains_hash(&self) -> bool {
		self.segments
			.last()
			.is_some_and(|s| matches!(s, TopicPatternItem::Hash(_)))
	}

	/// Returns iterator over pattern segments.
	pub fn iter(&self) -> Iter<'_, TopicPatternItem> {
		self.segments.iter()
	}

	/// Returns number of segments in pattern.
	pub fn len(&self) -> usize {
		self.segments.len()
	}

	fn str_len(segments: &[TopicPatternItem]) -> usize {
		if segments.is_empty() {
			return 0;
		}
		(segments.len() - 1) + // slashes count
		segments.iter().map(|s| s.as_str().len()).sum::<usize>()
	}

	/// Returns pattern segments as slice.
	pub fn slice(&self) -> &[TopicPatternItem] {
		&self.segments
	}

	/// Returns pattern segments for testing.
	#[cfg(test)]
	pub fn segments(&self) -> &Vec<TopicPatternItem> {
		&self.segments
	}

	/// Formats topic by substituting wildcards with provided parameters
	pub fn format_topic(
		&self,
		params: &[&dyn Display],
	) -> Result<String, TopicFormatError> {
		let wildcard_count =
			self.segments.iter().filter(|s| s.is_wildcard()).count();

		if params.len() != wildcard_count {
			return Err(TopicFormatError::ParameterCountMismatch {
				expected: wildcard_count,
				provided: params.len(),
			});
		}

		let mut result = String::with_capacity(self.topic_pattern().len() + 10); //Est.
		let mut param_index = 0;

		for (i, segment) in self.segments.iter().enumerate() {
			if i > 0 {
				result.push('/');
			}

			match segment {
				| TopicPatternItem::Str(s) => result.push_str(s),
				| TopicPatternItem::Plus(_) => {
					write!(result, "{}", params[param_index])?;
					param_index += 1;
				}
				| TopicPatternItem::Hash(_) => {
					return Err(TopicFormatError::HashWildcardNotSupported);
				}
			}
		}

		Ok(result)
	}

	fn to_mqtt_subscription_pattern(segments: &[TopicPatternItem]) -> String {
		// Convert to MQTT wildcards: sensors/+/data
		if segments.is_empty() {
			return String::new();
		}
		let mut mqtt_topic = String::with_capacity(Self::str_len(segments));
		segments.iter().enumerate().for_each(|(i, segment)| {
			if i > 0 {
				mqtt_topic.push('/');
			}
			mqtt_topic.push_str(segment.as_str());
		});
		mqtt_topic
	}

	#[cfg(all(test, feature = "router"))]
	fn to_template_pattern(segments: &[TopicPatternItem]) -> String {
		// Convert to named wildcards: sensors/{sensor_id}/data
		if segments.is_empty() {
			return String::new();
		}
		let mut mqtt_topic = String::new();
		segments.iter().enumerate().for_each(|(i, segment)| {
			if i > 0 {
				mqtt_topic.push('/');
			}
			mqtt_topic.push_str(segment.as_wildcard().as_ref());
		});
		mqtt_topic
	}

	/// Checks if the provided topic pattern is compatible with this one.
	///
	/// Static segments can differ, but wildcards must be identical in type,
	/// order, and names (if named).
	pub fn check_pattern_compatibility(
		&self,
		custom_topic: impl TryInto<TopicPatternPath, Error: Into<TopicPatternError>>,
	) -> Result<Self, TopicPatternError> {
		let candidate = custom_topic.try_into().map_err(Into::into)?;
		// Validate wildcard structure compatibility
		let self_wildcards =
			self.segments.iter().filter(|item| item.is_wildcard());
		let candidate_wildcards =
			candidate.segments.iter().filter(|item| item.is_wildcard());

		if !self_wildcards.eq(candidate_wildcards) {
			return Err(TopicPatternError::pattern_mismatch(
				self.template_pattern.as_str(),
				candidate.template_pattern.as_str(),
			));
		}

		Ok(candidate)
	}

	/// Create new pattern with different cache strategy
	pub fn with_cache_strategy(&self, new_cache: CacheStrategy) -> Self {
		let mut new_pattern =
			Self::new_from_string(self.template_pattern.clone(), new_cache)
				.expect("Pattern already validated");
		new_pattern.parameter_bindings = self.parameter_bindings.clone();
		new_pattern
	}

	/// Add value for topic wildcard parameter
	pub fn bind_parameter(
		mut self,
		param_name: impl Into<ArcStr>,
		value: impl Into<ArcStr>,
	) -> Result<Self, TopicPatternError> {
		let param_name_arc = param_name.into();

		let param_exists = self.segments.iter().any(|segment| {
			matches!(segment, TopicPatternItem::Plus(Some(name)) if name.as_str() == param_name_arc.as_str())
		});
		if !param_exists {
			return Err(TopicPatternError::wildcard_usage(format!(
				"Parameter '{param_name_arc}' not found in pattern '{}'",
				self.topic_pattern()
			)));
		}

		let value_arc = value.into();

		let bindings =
			self.parameter_bindings.get_or_insert_with(SmallVec::new);

		if let Some(pos) =
			bindings.iter().position(|(k, _)| k == &param_name_arc)
		{
			bindings[pos].1 = value_arc;
		} else {
			bindings.push((param_name_arc, value_arc));
		}

		Ok(self)
	}

	/// Matches a topic against this pattern, extracting parameters.
	///
	/// Takes an `Arc<TopicPath>` so the topic can be shared cheaply: when
	/// matching ONE topic against MANY patterns (the hot path), build the
	/// `Arc<TopicPath>` once and pass `Arc::clone(&topic)` to each pattern to
	/// avoid re-parsing and re-allocating per match. For a single one-off match
	/// from a string, [`try_match_str`](Self::try_match_str) is more convenient.
	pub fn try_match(
		&self,
		topic: Arc<TopicPath>,
	) -> Result<Arc<TopicMatch>, TopicMatchError> {
		#[cfg(feature = "lru-cache")]
		{
			match &self.match_cache {
				| Some(cache_mutex) => {
					{
						let mut match_cache = cache_mutex.lock().unwrap();
						if let Some(cached_match) = match_cache.get(&topic.path)
						{
							return Ok(cached_match.clone());
						}
					}

					let topic_match = self.try_match_internal(topic.clone())?;
					let topic_match_arc = Arc::new(topic_match);
					{
						let mut match_cache = cache_mutex.lock().unwrap();
						match_cache.put(
							topic.path.clone(),
							Arc::clone(&topic_match_arc),
						);
					}
					Ok(topic_match_arc)
				}
				| None => {
					let topic_match = self.try_match_internal(topic)?;
					Ok(Arc::new(topic_match))
				}
			}
		}
		#[cfg(not(feature = "lru-cache"))]
		{
			let topic_match = self.try_match_internal(topic)?;
			Ok(Arc::new(topic_match))
		}
	}

	/// Convenience wrapper around [`try_match`](Self::try_match) for one-off
	/// matches: it builds the [`TopicPath`] and wraps it in an `Arc` for you.
	///
	/// Prefer [`try_match`](Self::try_match) on the hot path (one topic, many
	/// patterns): calling this in a loop re-parses and re-allocates the topic
	/// every time.
	pub fn try_match_str(
		&self,
		topic: impl Into<ArcStr>,
	) -> Result<Arc<TopicMatch>, TopicMatchError> {
		self.try_match(Arc::new(TopicPath::new(topic)))
	}

	#[allow(clippy::missing_docs_in_private_items)]
	fn try_match_internal(
		&self,
		topic: Arc<TopicPath>,
	) -> Result<TopicMatch, TopicMatchError> {
		let mut topic_index = 0;
		let mut params = SmallVec::new();
		let mut named_params = SmallVec::new();
		for (i, pattern_segment) in self.iter().enumerate() {
			match pattern_segment {
				| TopicPatternItem::Str(expected) => {
					if topic_index >= topic.segments.len() {
						return Err(TopicMatchError::UnexpectedEndOfTopic);
					}
					if topic.segments[topic_index] != *expected {
						return Err(TopicMatchError::SegmentMismatch {
							expected: expected.to_string(),
							found: topic.segments[topic_index].to_string(),
							position: topic_index,
						});
					}
					topic_index += 1;
				}
				| TopicPatternItem::Plus(opt_name) => {
					if topic_index >= topic.segments.len() {
						return Err(TopicMatchError::UnexpectedEndOfTopic);
					}
					let param_range = topic_index .. topic_index + 1;
					params.push(param_range.clone());
					topic_index += 1;
					if let Some(name) = opt_name {
						named_params.push((name.clone(), param_range));
					}
				}
				| TopicPatternItem::Hash(opt_name) => {
					let param_range = topic_index .. topic.segments.len();
					params.push(param_range.clone());
					if let Some(name) = opt_name {
						named_params.push((name.clone(), param_range));
					}
					if i < self.len() - 1 {
						return Err(TopicMatchError::UnexpectedHashSegment);
					}
					return Ok(TopicMatch::from_match_result(
						topic,
						params,
						named_params,
					));
				}
			}
		}
		if topic_index < topic.segments.len() {
			return Err(TopicMatchError::UnexpectedEndOfPattern);
		}
		Ok(TopicMatch::from_match_result(topic, params, named_params))
	}
}

impl std::fmt::Display for TopicPatternPath {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		// Convert segments to strings and join them with "/"
		let path = self.topic_pattern();
		write!(f, "{path}")
	}
}

impl TryFrom<String> for TopicPatternPath {
	type Error = TopicPatternError;

	fn try_from(value: String) -> Result<Self, Self::Error> {
		Self::new_from_string(value, CacheStrategy::NoCache)
	}
}

impl TryFrom<&str> for TopicPatternPath {
	type Error = TopicPatternError;

	fn try_from(value: &str) -> Result<Self, Self::Error> {
		Self::new_from_string(value, CacheStrategy::NoCache)
	}
}

impl TryFrom<ArcStr> for TopicPatternPath {
	type Error = TopicPatternError;

	fn try_from(value: ArcStr) -> Result<Self, Self::Error> {
		Self::new_from_string(value, CacheStrategy::NoCache)
	}
}