✨ [pagetop] Añade gestión de rutas con parámetros
This commit is contained in:
parent
f1cbd3bd66
commit
35013b36df
8 changed files with 197 additions and 56 deletions
|
|
@ -1,6 +1,6 @@
|
|||
//! API para construir nuevos componentes.
|
||||
|
||||
use std::borrow::Cow;
|
||||
use crate::html::RoutePath;
|
||||
|
||||
mod definition;
|
||||
pub use definition::{Component, ComponentRender};
|
||||
|
|
@ -66,12 +66,31 @@ pub use context::{Context, ContextError, ContextOp, Contextual};
|
|||
/// ```
|
||||
pub type FnIsRenderable = fn(cx: &Context) -> bool;
|
||||
|
||||
/// Alias de función (*callback*) para **resolver una URL** según el contexto de renderizado.
|
||||
/// Alias de función (*callback*) para **resolver una ruta URL** según el contexto de renderizado.
|
||||
///
|
||||
/// Se usa para generar enlaces dinámicos en función del contexto (petición, idioma, etc.). El
|
||||
/// resultado se devuelve como [`Cow<'static, str>`](std::borrow::Cow), lo que permite:
|
||||
/// Se usa para generar enlaces dinámicos en función del contexto (petición, idioma, parámetros,
|
||||
/// etc.). El resultado se devuelve como una [`RoutePath`], que representa un *path* base junto con
|
||||
/// una lista opcional de parámetros de consulta.
|
||||
///
|
||||
/// - Usar rutas estáticas sin asignaciones adicionales (`"/path".into()`).
|
||||
/// - Construir rutas dinámicas en tiempo de ejecución (`format!(...).into()`), por ejemplo, en
|
||||
/// función de parámetros almacenados en [`Context`].
|
||||
pub type FnPathByContext = fn(cx: &Context) -> Cow<'static, str>;
|
||||
/// Gracias a la implementación de [`RoutePath`] puedes usar rutas estáticas sin asignaciones
|
||||
/// adicionales:
|
||||
///
|
||||
/// ```rust
|
||||
/// # use pagetop::prelude::*;
|
||||
/// # let static_path: FnPathByContext =
|
||||
/// |_| "/path/to/resource".into()
|
||||
/// # ;
|
||||
/// ```
|
||||
///
|
||||
/// O construir rutas dinámicas en tiempo de ejecución:
|
||||
///
|
||||
/// ```rust
|
||||
/// # use pagetop::prelude::*;
|
||||
/// # let dynamic_path: FnPathByContext =
|
||||
/// |cx| RoutePath::new("/user").with_param("id", cx.param::<u64>("user_id").unwrap().to_string())
|
||||
/// # ;
|
||||
/// ```
|
||||
///
|
||||
/// El componente que reciba un [`FnPathByContext`] invocará esta función durante el renderizado
|
||||
/// para obtener la URL final para asignarla al atributo HTML correspondiente.
|
||||
pub type FnPathByContext = fn(cx: &Context) -> RoutePath;
|
||||
|
|
|
|||
|
|
@ -5,6 +5,9 @@ use crate::AutoDefault;
|
|||
mod maud;
|
||||
pub use maud::{display, html, html_private, Escaper, Markup, PreEscaped, DOCTYPE};
|
||||
|
||||
mod route;
|
||||
pub use route::RoutePath;
|
||||
|
||||
// **< HTML DOCUMENT ASSETS >***********************************************************************
|
||||
|
||||
mod assets;
|
||||
|
|
|
|||
|
|
@ -67,14 +67,15 @@ impl AttrClasses {
|
|||
}
|
||||
ClassesOp::Remove => {
|
||||
for class in classes {
|
||||
self.0.retain(|c| c.ne(&class.to_string()));
|
||||
self.0.retain(|c| c != class);
|
||||
}
|
||||
}
|
||||
ClassesOp::Replace(classes_to_replace) => {
|
||||
let mut pos = self.0.len();
|
||||
let replace: Vec<&str> = classes_to_replace.split_ascii_whitespace().collect();
|
||||
let replace = classes_to_replace.to_ascii_lowercase();
|
||||
let replace: Vec<&str> = replace.split_ascii_whitespace().collect();
|
||||
for class in replace {
|
||||
if let Some(replace_pos) = self.0.iter().position(|c| c.eq(class)) {
|
||||
if let Some(replace_pos) = self.0.iter().position(|c| c == class) {
|
||||
self.0.remove(replace_pos);
|
||||
if pos > replace_pos {
|
||||
pos = replace_pos;
|
||||
|
|
|
|||
106
src/html/route.rs
Normal file
106
src/html/route.rs
Normal file
|
|
@ -0,0 +1,106 @@
|
|||
use crate::AutoDefault;
|
||||
|
||||
use std::borrow::Cow;
|
||||
use std::fmt;
|
||||
|
||||
/// Representa una ruta como un *path* inicial más una lista opcional de parámetros.
|
||||
///
|
||||
/// Modela rutas del estilo `/path/to/resource?foo=bar&debug` o `https://example.com/path?foo=bar`,
|
||||
/// pensadas para usarse en atributos HTML como `href`, `action` o `src`.
|
||||
///
|
||||
/// `RoutePath` no valida ni interpreta la estructura del *path*; simplemente concatena los
|
||||
/// parámetros de consulta sobre el valor proporcionado.
|
||||
///
|
||||
/// # Ejemplos
|
||||
///
|
||||
/// ```rust
|
||||
/// # use pagetop::prelude::*;
|
||||
/// // Ruta relativa con parámetros y una *flag* sin valor.
|
||||
/// let route = RoutePath::new("/search")
|
||||
/// .with_param("q", "rust")
|
||||
/// .with_param("page", "2")
|
||||
/// .with_flag("debug");
|
||||
/// assert_eq!(route.to_string(), "/search?q=rust&page=2&debug");
|
||||
///
|
||||
/// // Ruta absoluta a un recurso externo.
|
||||
/// let external = RoutePath::new("https://example.com/export").with_param("format", "csv");
|
||||
/// assert_eq!(external.to_string(), "https://example.com/export?format=csv");
|
||||
/// ```
|
||||
#[derive(AutoDefault)]
|
||||
pub struct RoutePath {
|
||||
// *Path* inicial sobre el que se añadirán los parámetros.
|
||||
//
|
||||
// Puede ser relativo (p. ej. `/about`) o una ruta completa (`https://example.com/about`).
|
||||
// `RoutePath` no realiza ninguna validación ni normalización.
|
||||
//
|
||||
// Se almacena como `Cow<'static, str>` para reutilizar literales estáticos sin asignación
|
||||
// adicional y, al mismo tiempo, aceptar rutas dinámicas representadas como `String`.
|
||||
path: Cow<'static, str>,
|
||||
|
||||
// Conjunto de parámetros asociados a la ruta.
|
||||
//
|
||||
// Cada clave es única y se mantiene el orden de inserción. El valor vacío se utiliza para
|
||||
// representar *flags* sin valor explícito (por ejemplo `?debug`).
|
||||
query: indexmap::IndexMap<String, String>,
|
||||
}
|
||||
|
||||
impl RoutePath {
|
||||
/// Crea un `RoutePath` a partir de un *path* inicial.
|
||||
///
|
||||
/// Por ejemplo: `RoutePath::new("/about")`.
|
||||
pub fn new(path: impl Into<Cow<'static, str>>) -> Self {
|
||||
Self {
|
||||
path: path.into(),
|
||||
query: indexmap::IndexMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Añade o sustituye un parámetro `key=value`. Si la clave ya existe, el valor se sobrescribe.
|
||||
pub fn with_param(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
|
||||
self.query.insert(key.into(), value.into());
|
||||
self
|
||||
}
|
||||
|
||||
/// Añade o sustituye un *flag* sin valor, por ejemplo `?debug`.
|
||||
pub fn with_flag(mut self, flag: impl Into<String>) -> Self {
|
||||
self.query.insert(flag.into(), String::new());
|
||||
self
|
||||
}
|
||||
|
||||
/// Devuelve el *path* inicial tal y como se pasó a [`RoutePath::new`], sin parámetros.
|
||||
pub fn path(&self) -> &str {
|
||||
&self.path
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for RoutePath {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.write_str(&self.path)?;
|
||||
if !self.query.is_empty() {
|
||||
f.write_str("?")?;
|
||||
for (i, (key, value)) in self.query.iter().enumerate() {
|
||||
if i > 0 {
|
||||
f.write_str("&")?;
|
||||
}
|
||||
f.write_str(key)?;
|
||||
if !value.is_empty() {
|
||||
f.write_str("=")?;
|
||||
f.write_str(value)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<&'static str> for RoutePath {
|
||||
fn from(path: &'static str) -> Self {
|
||||
RoutePath::new(path)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<String> for RoutePath {
|
||||
fn from(path: String) -> Self {
|
||||
RoutePath::new(path)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue