use crate::{
prelude::*,
state::{context::HttpRequestContext, request_state::RequestState},
};
use super::RemainingPath;
pub trait ContextCreateRemainingPathExt {
fn create_remaining_path(&mut self);
}
impl ContextCreateRemainingPathExt for HttpRequestContext {
fn create_remaining_path(&mut self) {
if RequestState::get_from_ctx(self).exists::<RemainingPath>() {
return;
}
let path_without_starting_slash = {
let path = self.path();
let mut path_chars = path.chars();
path_chars.next();
path_chars.as_str().to_string()
};
RequestState::get_mut_from_ctx(self)
.insert(RemainingPath(Some(path_without_starting_slash)));
}
}
pub trait ContextGetPathExt {
fn path(&self) -> &str;
fn remaining_path(&mut self) -> &RemainingPath;
fn remaining_path_mut(&mut self) -> &mut RemainingPath;
}
impl ContextGetPathExt for HttpRequestContext {
#[cfg(test)]
fn path(&self) -> &str {
use crate::state::request_state::RequestState;
(RequestState::get_from_ctx(self).get::<String>()).expect("must have path (as String)")
}
#[cfg(not(test))]
fn path(&self) -> &str {
self.request().uri().path()
}
fn remaining_path(&mut self) -> &RemainingPath {
self.create_remaining_path();
RequestState::get_mut_from_ctx(self).get().unwrap()
}
fn remaining_path_mut(&mut self) -> &mut RemainingPath {
self.create_remaining_path();
RequestState::get_mut_from_ctx(self).get_mut().unwrap()
}
}