Skip to main content

validate_path_segment

Function validate_path_segment 

Source
pub fn validate_path_segment(segment: &str) -> Option<Component<'_>>
Expand description

Validates that segment is a single plain path component: non-empty, and with no .., path separator, or root/prefix component.

Intended for validating a caller-supplied identifier (e.g. server_id) that will be pushed onto a confined base directory: constructing a fresh Component::Normal from the raw string instead of using the one this function returns would defeat the check on an input like "a/.", where Path::components() normalizes away the trailing . and this function sees a single Normal("a"), but a fresh Component::Normal(OsStr::new("a/.")) would still carry the embedded separator. Callers should push the returned Component itself.

Returns None (rather than an error) so each caller can report the failure in its own crate-specific error type with whatever context it has (e.g. which parameter was invalid).

ยงExamples

use mcp_execution_core::validate_path_segment;

assert!(validate_path_segment("my-server").is_some());
assert!(validate_path_segment("").is_none());
assert!(validate_path_segment("..").is_none());
assert!(validate_path_segment("a/b").is_none());