use axum::{ extract::FromRequestParts, http::request::Parts, response::{IntoResponse as _, Redirect, Response}, }; use interim_models::portal::Portal; use uuid::Uuid; use crate::{app::App, errors::AppError}; /// Helper type for semantically generating URI paths, e.g. for redirects. #[derive(Clone, Debug)] pub struct Navigator { root_path: String, sub_path: String, } impl Navigator { pub(crate) fn workspace_page(&self, workspace_id: Uuid) -> Self { Self { sub_path: format!("/w/{0}/", workspace_id.simple()), ..self.clone() } } pub(crate) fn portal_page(&self, portal: &Portal) -> Self { Self { sub_path: format!( "/w/{0}/r/{1}/p/{2}/", portal.workspace_id.simple(), portal.class_oid.0, portal.id.simple() ), ..self.clone() } } pub(crate) fn get_root_path(&self) -> String { self.root_path.to_owned() } pub(crate) fn abs_path(&self) -> String { format!("{0}{1}", self.root_path, self.sub_path) } pub(crate) fn redirect_to(&self) -> Response { Redirect::to(&self.abs_path()).into_response() } } impl FromRequestParts for Navigator { type Rejection = AppError; async fn from_request_parts(_: &mut Parts, state: &App) -> Result { Ok(Navigator { root_path: state.settings.root_path.clone(), sub_path: "/".to_owned(), }) } }