phonograph/interim-server/src/navigator.rs

61 lines
1.5 KiB
Rust
Raw Normal View History

2025-07-18 16:20:03 -07:00
use axum::{
extract::FromRequestParts,
http::request::Parts,
response::{IntoResponse as _, Redirect, Response},
};
use interim_models::portal::Portal;
use uuid::Uuid;
2025-07-18 16:20:03 -07:00
use crate::{app_error::AppError, app_state::App};
2025-07-18 16:20:03 -07:00
/// 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 {
2025-07-18 16:20:03 -07:00
Self {
sub_path: format!(
"/w/{0}/r/{1}/p/{2}/",
portal.workspace_id.simple(),
portal.class_oid.0,
portal.id.simple()
2025-07-18 16:20:03 -07:00
),
..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()
2025-07-18 16:20:03 -07:00
}
}
impl FromRequestParts<App> for Navigator {
2025-07-18 16:20:03 -07:00
type Rejection = AppError;
async fn from_request_parts(_: &mut Parts, state: &App) -> Result<Self, Self::Rejection> {
2025-07-18 16:20:03 -07:00
Ok(Navigator {
root_path: state.settings.root_path.clone(),
2025-07-18 16:20:03 -07:00
sub_path: "/".to_owned(),
})
}
}