pub struct UrlRegistry { /* private fields */ }Expand description
Registry for named routes, enabling URL generation.
§Example
ⓘ
use fastapi_core::routing::UrlRegistry;
let mut registry = UrlRegistry::new();
registry.register("get_user", "/users/{id}");
registry.register("get_post", "/posts/{post_id:int}");
// Generate URL with path parameter
let url = registry.url_for("get_user", &[("id", "42")], &[]).unwrap();
assert_eq!(url, "/users/42");
// Generate URL with query parameters
let url = registry.url_for("get_user", &[("id", "42")], &[("fields", "name,email")]).unwrap();
assert_eq!(url, "/users/42?fields=name%2Cemail");Implementations§
Source§impl UrlRegistry
impl UrlRegistry
Sourcepub fn with_root_path(root_path: impl Into<String>) -> Self
pub fn with_root_path(root_path: impl Into<String>) -> Self
Create a URL registry with a root path prefix.
The root path is prepended to all generated URLs, useful for apps running behind a reverse proxy at a sub-path.
§Example
ⓘ
let registry = UrlRegistry::with_root_path("/api/v1");
registry.register("get_user", "/users/{id}");
let url = registry.url_for("get_user", &[("id", "42")], &[]).unwrap();
assert_eq!(url, "/api/v1/users/42");Sourcepub fn set_root_path(&mut self, root_path: impl Into<String>)
pub fn set_root_path(&mut self, root_path: impl Into<String>)
Set the root path prefix.
Sourcepub fn register(&mut self, name: impl Into<String>, pattern: &str)
pub fn register(&mut self, name: impl Into<String>, pattern: &str)
Register a named route.
§Arguments
name- The route name (used to look up the route)pattern- The route pattern (e.g., “/users/{id}”)
Sourcepub fn get_pattern(&self, name: &str) -> Option<&str>
pub fn get_pattern(&self, name: &str) -> Option<&str>
Get the pattern for a named route.
Sourcepub fn url_for(
&self,
name: &str,
params: &[(&str, &str)],
query: &[(&str, &str)],
) -> Result<String, UrlError>
pub fn url_for( &self, name: &str, params: &[(&str, &str)], query: &[(&str, &str)], ) -> Result<String, UrlError>
Generate a URL for a named route.
§Arguments
name- The route nameparams- Path parameters as (name, value) pairsquery- Query parameters as (name, value) pairs
§Errors
Returns an error if:
- The route name is not found
- A required path parameter is missing
- A path parameter value doesn’t match its converter type
§Example
ⓘ
let url = registry.url_for(
"get_user",
&[("id", "42")],
&[("fields", "name"), ("include", "posts")]
).unwrap();
// Returns: "/users/42?fields=name&include=posts"Sourcepub fn route_names(&self) -> impl Iterator<Item = &str>
pub fn route_names(&self) -> impl Iterator<Item = &str>
Get an iterator over route names.
Trait Implementations§
Source§impl Clone for UrlRegistry
impl Clone for UrlRegistry
Source§fn clone(&self) -> UrlRegistry
fn clone(&self) -> UrlRegistry
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for UrlRegistry
impl Debug for UrlRegistry
Source§impl Default for UrlRegistry
impl Default for UrlRegistry
Source§fn default() -> UrlRegistry
fn default() -> UrlRegistry
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for UrlRegistry
impl RefUnwindSafe for UrlRegistry
impl Send for UrlRegistry
impl Sync for UrlRegistry
impl Unpin for UrlRegistry
impl UnwindSafe for UrlRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, _span: NoopSpan) -> Self
fn instrument(self, _span: NoopSpan) -> Self
Instruments this future with a span (no-op when disabled).
Source§fn in_current_span(self) -> Self
fn in_current_span(self) -> Self
Instruments this future with the current span (no-op when disabled).