phonograph/interim-server/src/navigator.rs

49 lines
1.2 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::lens::Lens;
use crate::{app_error::AppError, app_state::AppState};
/// 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 fn lens_page(&self, lens: &Lens) -> Self {
Self {
sub_path: format!(
"/d/{0}/r/{1}/l/{2}/",
lens.base_id.simple(),
lens.class_oid.0,
lens.id.simple()
),
..self.clone()
}
}
pub fn redirect_to(&self) -> Response {
Redirect::to(&format!("{0}{1}", self.root_path, self.sub_path)).into_response()
}
}
impl<S> FromRequestParts<S> for Navigator
where
S: Into<AppState> + Clone + Sync,
{
type Rejection = AppError;
async fn from_request_parts(_: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let app_state: AppState = state.clone().into();
Ok(Navigator {
root_path: app_state.settings.root_path.clone(),
sub_path: "/".to_owned(),
})
}
}