pub struct TopicPath { /* private fields */ }Implementations§
Source§impl TopicPath
impl TopicPath
Sourcepub fn new_action_topic(&self, action_name: &str) -> Result<Self, String>
pub fn new_action_topic(&self, action_name: &str) -> Result<Self, String>
Creates a new TopicPath for an action based on this service path
INTENTION: Provide a simple way to create an action path from a service path, ensuring consistent formatting and proper path type.
Example:
use runar_node::TopicPath;
let service_path = TopicPath::new("main:auth", "default").expect("Valid path");
let action_path = service_path.new_action_topic("login").expect("Valid action path");
assert_eq!(action_path.action_path(), "auth/login");Sourcepub fn new_event_topic(&self, event_name: &str) -> Result<Self, String>
pub fn new_event_topic(&self, event_name: &str) -> Result<Self, String>
Creates a new TopicPath for an event based on this service path
INTENTION: Provide a simple way to create an event path from a service path, ensuring consistent formatting and proper path type.
Example:
use runar_node::TopicPath;
let service_path = TopicPath::new("main:auth", "default").expect("Valid path");
let event_path = service_path.new_event_topic("user_logged_in").expect("Valid event path");
assert_eq!(event_path.action_path(), "auth/user_logged_in");pub fn from_full_path(path: &str) -> Result<Self, String>
Sourcepub fn new(path: &str, default_network: &str) -> Result<Self, String>
pub fn new(path: &str, default_network: &str) -> Result<Self, String>
Create a new TopicPath from a string
INTENTION: Validate and construct a TopicPath from a string input, ensuring it follows the required format conventions.
This method now supports wildcard patterns:
- “*” matches any single segment
- “>” matches one or more segments to the end (must be the last segment)
Example:
use runar_node::routing::TopicPath;
// With network_id prefix
let path = TopicPath::new("main:auth/login", "default").expect("Valid path");
assert_eq!(path.network_id(), "main");
assert_eq!(path.service_path(), "auth");
assert_eq!(path.action_path(), "auth/login");
// With wildcards
let pattern = TopicPath::new("main:services/*/state", "default").expect("Valid pattern");
assert!(pattern.is_pattern());Sourcepub fn is_pattern(&self) -> bool
pub fn is_pattern(&self) -> bool
Check if this path contains wildcards
INTENTION: Quickly determine if a path is a wildcard pattern, which affects how it’s used for matching.
Sourcepub fn has_multi_wildcard(&self) -> bool
pub fn has_multi_wildcard(&self) -> bool
Check if this path contains a multi-segment wildcard
INTENTION: Identify paths with multi-segment wildcards which have special matching rules and storage requirements.
Sourcepub fn action_path(&self) -> String
pub fn action_path(&self) -> String
Get the path after the network ID
INTENTION: Get the complete path after the network ID, including all segments. This is useful when you need the path for routing.
Example:
use runar_node::routing::TopicPath;
let path = TopicPath::new("main:auth/login", "default").expect("Valid path");
assert_eq!(path.action_path(), "auth/login");
// When there is only a service with no action, returns empty string
let service_only = TopicPath::new("main:auth", "default").expect("Valid path");
assert_eq!(service_only.action_path(), "");Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
Get the path as a string
INTENTION: Access the raw path string for this TopicPath, useful for display and logging purposes.
Sourcepub fn network_id(&self) -> String
pub fn network_id(&self) -> String
Get the network ID from this path
INTENTION: Extract just the network segment from the path, which is crucial for network isolation and routing.
Example:
use runar_node::routing::TopicPath;
let path = TopicPath::new("main:auth/login", "default").expect("Valid path");
assert_eq!(path.network_id(), "main");Sourcepub fn service_path(&self) -> String
pub fn service_path(&self) -> String
Get the service path part of this path
INTENTION: Extract the service path from the full path, which is used for service lookup and addressing.
Example:
use runar_node::routing::TopicPath;
let path = TopicPath::new("main:auth/login", "default").expect("Valid path");
// The service_path method returns just the service name (first segment)
assert_eq!(path.service_path(), "auth");
// If you need just the first segment, use get_segments
let segments = path.get_segments();
assert_eq!(segments[0], "auth");Sourcepub fn new_service(network_id: &str, service_name: &str) -> Self
pub fn new_service(network_id: &str, service_name: &str) -> Self
Create a new service path from a network ID and service name
INTENTION: Create a TopicPath specifically for a service, avoiding manual string concatenation and validation.
Example:
use runar_node::routing::TopicPath;
let path = TopicPath::new_service("main", "auth");
assert_eq!(path.as_str(), "main:auth");Sourcepub fn starts_with(&self, other: &TopicPath) -> bool
pub fn starts_with(&self, other: &TopicPath) -> bool
Check if this path starts with another path
INTENTION: Determine if this path is a “child” or more specific version of another path, useful for hierarchical routing and subscription matching.
Example:
use runar_node::routing::TopicPath;
let path = TopicPath::new("main:auth/users", "default").expect("Valid path");
let prefix = TopicPath::new("main:auth", "default").expect("Valid path");
let different = TopicPath::new("main:payments", "default").expect("Valid path");
assert!(path.starts_with(&prefix));
assert!(!path.starts_with(&different));Sourcepub fn child(&self, segment: &str) -> Result<Self, String>
pub fn child(&self, segment: &str) -> Result<Self, String>
Create a child path by appending a segment
INTENTION: Generate a new TopicPath that is a child of this path, useful for creating more specific paths from a base path.
Example:
use runar_node::routing::TopicPath;
let base = TopicPath::new("main:auth", "default").expect("Valid path");
let child = base.child("login").expect("Valid child path");
assert_eq!(child.as_str(), "main:auth/login");Sourcepub fn get_segments(&self) -> Vec<String>
pub fn get_segments(&self) -> Vec<String>
Get the path segments
INTENTION: Get the segments of the path after the network ID. This is useful for path analysis and custom path parsing.
Example:
use runar_node::routing::TopicPath;
let path = TopicPath::new("main:auth/login/form", "default").expect("Valid path");
let segments = path.get_segments();
assert_eq!(segments, vec!["auth", "login", "form"]);Sourcepub fn parent(&self) -> Result<Self, String>
pub fn parent(&self) -> Result<Self, String>
Create a parent path
INTENTION: Generate a new TopicPath that is the parent of this path, useful for navigating up the path hierarchy.
Example:
use runar_node::routing::TopicPath;
let path = TopicPath::new("main:auth/users", "default").expect("Valid path");
let parent = path.parent().expect("Valid parent path");
assert_eq!(parent.as_str(), "main:auth");Sourcepub fn test_default(path: &str) -> Self
pub fn test_default(path: &str) -> Self
Create a default path for running unit tests with default network ID
INTENTION: Simplify test setup by creating a valid path with a default network ID.
Note: This is primarily intended for testing purposes.
Example:
use runar_node::routing::TopicPath;
let path = TopicPath::test_default("auth/login");
assert_eq!(path.as_str(), "default:auth/login");Sourcepub fn extract_params(
&self,
template: &str,
) -> Result<HashMap<String, String>, String>
pub fn extract_params( &self, template: &str, ) -> Result<HashMap<String, String>, String>
Match this path against a template pattern and extract parameters
INTENTION: Enable services to define URL-like templates with named parameters and extract those parameter values from actual request paths.
Template patterns can include segments like {param_name} which will match
any value in that position and capture it with the specified name.
Example:
use runar_node::routing::TopicPath;
let template = "services/{service_path}/state";
let path = TopicPath::new("main:services/math/state", "main").expect("Valid path");
let params = path.extract_params(template).expect("Template should match");
assert_eq!(params.get("service_path"), Some(&"math".to_string()));
// Non-matching templates return an error
let non_matching = TopicPath::new("main:users/profile", "main").expect("Valid path");
assert!(non_matching.extract_params(template).is_err());Sourcepub fn matches_template(&self, template: &str) -> bool
pub fn matches_template(&self, template: &str) -> bool
Check if a path matches a template pattern
INTENTION: Quickly determine if a path can be processed by a particular template, without needing to extract parameters.
Example:
use runar_node::routing::TopicPath;
let template = "services/{service_path}/state";
let path = TopicPath::new("main:services/math/state", "default").expect("Valid path");
assert!(path.matches_template(template));
let non_matching = TopicPath::new("main:users/profile", "default").expect("Valid path");
assert!(!non_matching.matches_template(template));Sourcepub fn has_templates(&self) -> bool
pub fn has_templates(&self) -> bool
Check if this path contains template parameters
INTENTION: Quickly determine if a path has template parameters, which affects template matching behavior.
Sourcepub fn segment_count(&self) -> usize
pub fn segment_count(&self) -> usize
Get the number of segments in this path
INTENTION: Quickly get the segment count without iterating segments.
pub fn has_segment_type(&self, index: usize, segment_type: u8) -> bool
Sourcepub fn matches(&self, topic: &TopicPath) -> bool
pub fn matches(&self, topic: &TopicPath) -> bool
Implement pattern matching against another path
INTENTION: Allow checking if a topic matches a pattern with wildcards, enabling powerful pattern-based subscriptions.
Example:
use runar_node::routing::TopicPath;
let pattern = TopicPath::new("main:services/*/state", "default").expect("Valid pattern");
let topic1 = TopicPath::new("main:services/math/state", "default").expect("Valid topic");
let topic2 = TopicPath::new("main:services/math/config", "default").expect("Valid topic");
assert!(pattern.matches(&topic1));
assert!(!pattern.matches(&topic2));Sourcepub fn from_template(
template_string: &str,
params: HashMap<String, String>,
network_id_string: &str,
) -> Result<Self, String>
pub fn from_template( template_string: &str, params: HashMap<String, String>, network_id_string: &str, ) -> Result<Self, String>
Create a TopicPath from a template and parameter values
INTENTION: Allow dynamic construction of TopicPaths from templates with parameter placeholders, similar to route templates in web frameworks.
Example:
use runar_node::routing::TopicPath;
use std::collections::HashMap;
let mut params = HashMap::new();
params.insert("service_path".to_string(), "math".to_string());
params.insert("action".to_string(), "add".to_string());
let path = TopicPath::from_template(
"services/{service_path}/{action}",
params,
"main"
).expect("Valid template");
assert_eq!(path.as_str(), "main:services/math/add");