use super::super::resolve::*;
use {
axum::http::*,
compris::resolve::*,
kutil::{cli::depict::*, std::immutable::*},
regex::*,
};
#[derive(Clone, Debug, Depict, Resolve)]
pub struct Redirect {
#[resolve(required)]
#[depict(as(display), style(string))]
pub regex: ResolveRegex,
#[resolve(required, key = "to")]
#[depict(style(string))]
pub expand_to: ByteString,
#[resolve(key = "code")]
#[depict(as(display), style(symbol))]
pub status_code: ResolveStatusCode,
}
impl Redirect {
pub fn redirect(&self, uri_path: &str) -> Option<(String, StatusCode)> {
if let Some(captures) = self.regex.inner.captures(uri_path) {
let mut uri_path = String::default();
captures.expand(&self.expand_to, &mut uri_path);
return Some((uri_path, self.status_code.inner));
}
None
}
}
impl Default for Redirect {
fn default() -> Self {
Self {
regex: Regex::new("").expect("regex").into(),
expand_to: Default::default(),
status_code: StatusCode::MOVED_PERMANENTLY.into(),
}
}
}