Rust crate reference · AIP-122 · AIP-159
ResourcePattern, explained and testable.
A practical guide to compiling, scanning, validating, and formatting Google-style resource names in Rust—without a protobuf runtime dependency.
The design
Pure resource-name manipulation
aip-rs publishes the crate under that package name, while its Rust library name is aip. The crate focuses on text-level resource patterns. Protobuf traversal belongs to generated code, keeping this layer dependency-free.
Pattern
publishers/{publisher}
Capture
publisher → p1
Format
publishers/p1
Quick start
Compile once, reuse safely
use std::sync::LazyLock;
use aip::ResourcePattern;
static BOOK_NAME: LazyLock<ResourcePattern> = LazyLock::new(|| {
"publishers/{publisher}/books/{book}"
.parse()
.expect("book name pattern")
});
let ids = BOOK_NAME.scan("publishers/p1/books/b1")?;
assert_eq!(ids, ["p1", "b1"]);
assert_eq!(BOOK_NAME.format(&ids), "publishers/p1/books/b1");
The example is illustrative and does not run in this page.
Interactive local simulator
Try a resource pattern
This playground models the documented segment behavior in your browser. It does not compile Rust or call a service.
Output
Ready for a pattern.
Run the checks to see captured variables, validation, and formatted output.
Methods
API reference
Use allocating methods for ordinary application code and fixed-size methods in generated code where the pattern arity is known.
compile(pattern)
Compiles the pattern and returns CompileError for invalid syntax or naming.
as_str()
Returns the original pattern string.
variables()
Returns the number of variable segments.
variable_names()
Iterates over variable names in pattern order.
scan(name)
Matches a relative resource name and returns borrowed variable values.
scan_into(name, values)
Allocation-free scan into a caller-provided slice. Panics on an arity mismatch.
scan_full(name, domain)
Matches //domain/ followed by the relative pattern.
format(values)
Substitutes values in pattern order. Call validate when values are untrusted.
validate(values)
Rejects empty values and values containing the / separator.
invalid_value(...)
Wraps a typed conversion failure as a pattern-aware ParseError.
Generated code boundary
Why generated names still matter
A generated API can expose a concrete BookName rather than making callers pass arbitrary pattern strings. The generated type owns domain semantics; ResourcePattern owns the reusable segment walking.
use aip::ResourcePattern;
use std::sync::LazyLock;
static BOOK_PATTERN: LazyLock<ResourcePattern> = LazyLock::new(|| {
"publishers/{publisher}/books/{book}"
.parse()
.expect("generated pattern is valid")
});
fn parse_book(name: &str) -> Result<(&str, &str), aip::resource::ParseError> {
let values = BOOK_PATTERN.scan(name)?;
match values.as_slice() {
[publisher, book] => Ok((*publisher, *book)),
_ => unreachable!("generated pattern arity must be two"),
}
}
Quality checklist
Tests worth keeping
Compilation
Valid literals, valid variables, duplicate variables, invalid casing, and unsupported path-template syntax.
Matching
Exact literals, missing segments, extra segments, and empty variable values.
Allocation-free paths
Successful scan_into, untouched values after failure, and deliberate arity panic tests.
Typed IDs
Successful parsing and invalid_value errors for UUIDs, integers, or other declared types.