Skip to content

Commit

Permalink
Optimize decoding
Browse files Browse the repository at this point in the history
  • Loading branch information
CathalMullan committed Aug 19, 2024
1 parent e476dd9 commit b3ea7bd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
23 changes: 17 additions & 6 deletions src/path.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
use crate::{decode::percent_decode, errors::decode::DecodeError};
use std::borrow::Cow;

pub struct Path {
pub inner: Vec<u8>,
pub struct Path<'a> {
original: Cow<'a, [u8]>,
decoded: Option<Vec<u8>>,
}

impl Path {
impl<'a> Path<'a> {
#[must_use]
pub fn new(path: &str) -> Self {
pub const fn new(path: &'a str) -> Self {
Self {
inner: path.as_bytes().to_vec(),
original: Cow::Borrowed(path.as_bytes()),
decoded: None,
}
}

pub fn percent_decode(&mut self) -> Result<(), DecodeError> {
self.inner = percent_decode(&self.inner)?;
if self.decoded.is_none() {
self.decoded = Some(percent_decode(&self.original)?);
}

Ok(())
}

#[must_use]
pub fn as_bytes(&self) -> &[u8] {
self.decoded.as_deref().unwrap_or(&self.original)
}
}
2 changes: 1 addition & 1 deletion src/router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl<T> Router<T> {
let mut parameters = smallvec![];
let Some(node) = self
.root
.search(&path.inner, &mut parameters, &self.constraints)
.search(path.as_bytes(), &mut parameters, &self.constraints)
else {
return Ok(None);
};
Expand Down

0 comments on commit b3ea7bd

Please sign in to comment.