dimas_core/utils/
mod.rs

1// Copyright © 2024 Stephan Kunz
2
3//! Helper functions and structs
4//!
5
6#[doc(hidden)]
7extern crate alloc;
8
9// region:		--- modules
10use alloc::string::{String, ToString};
11// endregion:	--- modules
12
13// region:    --- tracing
14/// Initialize tracing
15pub fn init_tracing() {
16	let subscriber = tracing_subscriber::fmt()
17		//.with_env_filter(env_filter)
18		.with_thread_ids(true)
19		.with_thread_names(true)
20		.with_level(true)
21		.with_target(true);
22
23	let subscriber = subscriber.finish();
24	let _ = tracing::subscriber::set_global_default(subscriber);
25}
26// endregion: --- tracing
27
28// region:    --- helper
29/// create selector
30#[must_use]
31pub fn selector_from(topic: &str, mut prefix: Option<&String>) -> String {
32	prefix.take().map_or_else(
33		|| topic.to_string(),
34		|prefix| {
35			let mut result = String::from(prefix);
36			result.push('/');
37			result.push_str(topic);
38			result
39		},
40	)
41}
42
43/// create request selector
44#[must_use]
45pub fn request_selector_from(selector: &str) -> String {
46	let mut result = String::from(selector);
47	result.push_str("?request");
48	result
49}
50
51/// create cancel selector
52#[must_use]
53pub fn cancel_selector_from(selector: &str) -> String {
54	let mut result = String::from(selector);
55	result.push_str("?cancel");
56	result
57}
58
59/// create feedback selector
60#[must_use]
61pub fn feedback_selector_from(selector: &str, id: &str) -> String {
62	let mut result = String::from(selector);
63	result.push_str("/feedback/");
64	result.push_str(id);
65	result
66}
67// endregion: --- helper