macro_rules! rel_ref {
( unsafe $S:expr ) => { ... };
( $S:expr ) => { ... };
( ) => { ... };
}
Expand description
Creates a &'static RelRef
from a string literal.
Accepts only string constants and literals. The given string MUST be well-formed.
Example:
let x = rel_ref!("a/b/c?q=foobar#frag");
assert_eq!(x.raw_path(),"a/b/c");
assert_eq!(x.raw_query(),Some("q=foobar"));
assert_eq!(x.raw_fragment(),Some("frag"));
Checks for correctness are performed at compile time:
ⓘ
// This will not compile.
let x = rel_ref!("%00 invalid %ff");
Degenerate cases (strings that could be confused with URIs if parsed as URI-Refs) will also not compile:
ⓘ
// This will not compile because `//a/b/c` is
// a degenerate relative reference.
let x = rel_ref!("//a/b/c");
ⓘ
// This will not compile because `g:a:b:c` is
// a degenerate relative reference.
let x = rel_ref!("g:a:b:c");
Both of those cases can be made to compile by adjusting them to no longer be degenerate:
let b = rel_ref!("/.//a/b/c"); // Prepending "/."
let a = rel_ref!("./g:a:b:c"); // Prepending "./"
let a = rel_ref!("g%3Aa:b:c"); // Convert first colon to "%3A"
At runtime, UriRef::from_str("g:a:b:c")
is allowed since in some circumstances it cannot
be avoided, but there is usually no good reason to have a degenerate RelRef
literal.
In the rare case where such a thing is warranted (unit tests, for example), you can disable
compile-time verification by prepending the keyword unsafe
to the string:
// Both of these will compile because the `unsafe` keyword
// disables the compile-time validity checks:
assert!(rel_ref!(unsafe "//a/b/c").is_degenerate());
assert!(rel_ref!(unsafe "g:a:b:c").is_degenerate());