pub struct TopicPath {
pub segments: VecDeque<String>,
}
Expand description
Represents a slash (/
) deliminated path.
This is especially useful when trying to parse nested data, such as from Shuffleboard
(found in /Shuffleboard/...
).
This can be thought of as a wrapper for a VecDeque
, only providing trait impls to convert
to/from a String
.
§Note
The Display
impl will always contain a leading slash, but not a trailing one,
regardless of if the path was parsed from a String
containing either a leading or trailing
slash.
§Warning
In cases where slashes are present in segment names, turning to and from a String
is
NOT guaranteed to preserve segment names.
use nt_client::{topic::TopicPath, path};
let path = path!["///weird//", "na//mes//"];
assert_ne!(<String as Into<TopicPath>>::into(path.to_string()), path);
In the above example, .to_string()
is converting the path to ////weird///na//mes//
.
When turning this back into a TopicPath
, it recognizes the following segments (with
trailing and leading slashes removed):
/ / /weird / / / na / /mes /
§Examples
use nt_client::{topic::TopicPath, path};
// automatically inserts a leading slash
assert_eq!(path!["my", "topic"].to_string(), "/my/topic");
// slashes in the segment names are preserved
assert_eq!(path!["some", "/data"].to_string(), "/some//data");
assert_eq!(<&str as Into<TopicPath>>::into("/path/to/data"), path!["path", "to", "data"]);
assert_eq!(<&str as Into<TopicPath>>::into("//some///weird/path/"), path!["/some", "/", "weird", "path"]);
Getting a topic:
use nt_client::{Client, path};
let client = Client::new(Default::default());
let topic = client.topic(path!["nested", "data"]);
// do something with `topic`
client.connect().await;
Parsing topic name:
use nt_client::{topic::TopicPath, data::SubscriptionOptions, subscribe::ReceivedMessage, Client};
let client = Client::new(Default::default());
client.connect_setup(setup).await;
fn setup(client: &Client) {
let sub_topic = client.topic("/Root/");
tokio::spawn(async move {
let mut sub = sub_topic.subscribe(SubscriptionOptions {
topics_only: Some(true),
prefix: Some(true),
..Default::default()
}).await.unwrap();
while let Ok(ReceivedMessage::Announced(topic)) = sub.recv().await {
let path: TopicPath = topic.name().into();
// do something with `path`
}
});
}
Fields§
§segments: VecDeque<String>
The segments contained in the path.