1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-22 09:31:10 +00:00

docs: document internal Path fields

This commit is contained in:
Rob Ede 2023-12-23 18:49:17 +00:00
parent ede0201aa4
commit fdef224a06
No known key found for this signature in database
GPG key ID: 97C636207D3EF933

View file

@ -3,7 +3,7 @@ use std::{
ops::{DerefMut, Index}, ops::{DerefMut, Index},
}; };
use serde::de; use serde::{de, Deserialize};
use crate::{de::PathDeserializer, Resource, ResourcePath}; use crate::{de::PathDeserializer, Resource, ResourcePath};
@ -24,8 +24,13 @@ impl Default for PathItem {
/// If resource path contains variable patterns, `Path` stores them. /// If resource path contains variable patterns, `Path` stores them.
#[derive(Debug, Clone, Default)] #[derive(Debug, Clone, Default)]
pub struct Path<T> { pub struct Path<T> {
/// Full path representation.
path: T, path: T,
/// Number of characters in `path` that have been processed into `segments`.
pub(crate) skip: u16, pub(crate) skip: u16,
/// List of processed dynamic segments; name->value pairs.
pub(crate) segments: Vec<(Cow<'static, str>, PathItem)>, pub(crate) segments: Vec<(Cow<'static, str>, PathItem)>,
} }
@ -83,8 +88,8 @@ impl<T: ResourcePath> Path<T> {
/// Set new path. /// Set new path.
#[inline] #[inline]
pub fn set(&mut self, path: T) { pub fn set(&mut self, path: T) {
self.skip = 0;
self.path = path; self.path = path;
self.skip = 0;
self.segments.clear(); self.segments.clear();
} }
@ -103,7 +108,7 @@ impl<T: ResourcePath> Path<T> {
pub(crate) fn add(&mut self, name: impl Into<Cow<'static, str>>, value: PathItem) { pub(crate) fn add(&mut self, name: impl Into<Cow<'static, str>>, value: PathItem) {
match value { match value {
PathItem::Static(s) => self.segments.push((name.into(), PathItem::Static(s))), PathItem::Static(seg) => self.segments.push((name.into(), PathItem::Static(seg))),
PathItem::Segment(begin, end) => self.segments.push(( PathItem::Segment(begin, end) => self.segments.push((
name.into(), name.into(),
PathItem::Segment(self.skip + begin, self.skip + end), PathItem::Segment(self.skip + begin, self.skip + end),
@ -168,9 +173,13 @@ impl<T: ResourcePath> Path<T> {
} }
} }
/// Try to deserialize matching parameters to a specified type `U` /// Deserializes matching parameters to a specified type `U`.
pub fn load<'de, U: serde::Deserialize<'de>>(&'de self) -> Result<U, de::value::Error> { ///
de::Deserialize::deserialize(PathDeserializer::new(self)) /// # Errors
///
/// Returns error when dynamic path segments cannot be deserialized into a `U` type.
pub fn load<'de, U: Deserialize<'de>>(&'de self) -> Result<U, de::value::Error> {
Deserialize::deserialize(PathDeserializer::new(self))
} }
} }