From 5bf22ec1419cb8c0aab5d6c47cc3575bae9cb9e4 Mon Sep 17 00:00:00 2001 From: crowlkats Date: Wed, 6 Dec 2023 13:22:51 +0100 Subject: [PATCH] replace current file and current symbol for current resolve --- src/html/mod.rs | 39 +++++++++++++++----------- src/html/symbols/namespace.rs | 5 ++-- src/html/util.rs | 53 ++++++----------------------------- 3 files changed, 34 insertions(+), 63 deletions(-) diff --git a/src/html/mod.rs b/src/html/mod.rs index e555da7e..ff8e7228 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -44,7 +44,7 @@ const FUSE_FILENAME: &str = "fuse.js"; const SEARCH_JS: &str = include_str!("./templates/search.js"); const SEARCH_FILENAME: &str = "search.js"; -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Copy)] pub enum UrlResolveKind<'a> { Root, AllSymbols, @@ -52,6 +52,17 @@ pub enum UrlResolveKind<'a> { Symbol { file: &'a str, symbol: &'a str }, } +impl UrlResolveKind<'_> { + fn get_file(&self) -> Option<&str> { + match self { + UrlResolveKind::Root => None, + UrlResolveKind::AllSymbols => None, + UrlResolveKind::File(file) => Some(file), + UrlResolveKind::Symbol { file, .. } => Some(file), + } + } +} + /// Arguments are current and target pub type UrlResolver = Rc String>; @@ -642,15 +653,15 @@ fn render_index( &file, ); + let short_path = specifier.map(|specifier| ctx.url_to_short_path(specifier)); + let render_ctx = RenderContext::new( ctx, all_symbols, - specifier.map(|specifier| ctx.url_to_short_path(specifier)), - None, - if specifier.is_none() { - Some(UrlResolveKind::Root) + if let Some(short_path) = &short_path { + UrlResolveKind::File(short_path) } else { - None + UrlResolveKind::Root }, ); @@ -709,13 +720,8 @@ fn render_all_symbols( partitions: &IndexMap>, all_symbols: NamespacedSymbols, ) -> Result { - let render_ctx = RenderContext::new( - ctx, - all_symbols, - None, - None, - Some(UrlResolveKind::AllSymbols), - ); + let render_ctx = + RenderContext::new(ctx, all_symbols, UrlResolveKind::AllSymbols); let namespace_ctx = namespace::get_namespace_render_ctx(&render_ctx, partitions); @@ -801,9 +807,10 @@ fn render_page( let mut render_ctx = RenderContext::new( ctx, all_symbols, - Some(file.to_string()), - Some(namespaced_name.clone()), - None, + UrlResolveKind::Symbol { + file, + symbol: &namespaced_name, + }, ); if !namespace_paths.is_empty() { render_ctx = render_ctx.with_namespace(namespace_paths.to_vec()) diff --git a/src/html/symbols/namespace.rs b/src/html/symbols/namespace.rs index 02ac8b74..80bc990d 100644 --- a/src/html/symbols/namespace.rs +++ b/src/html/symbols/namespace.rs @@ -206,15 +206,16 @@ fn get_namespace_section_render_ctx( name = format!("{}.{}", ns_parts.join("."), doc_node.name); } + let current_resolve = ctx.get_current_resolve(); NamespaceSectionNodeCtx { doc_node_kind_ctx: doc_node.kind.into(), origin: origin.clone(), href: (ctx.url_resolver)( - ctx.get_current_resolve(), + current_resolve, crate::html::UrlResolveKind::Symbol { file: origin .as_deref() - .or_else(|| ctx.get_current_file()) + .or_else(|| current_resolve.get_file()) .unwrap(), symbol: &name, }, diff --git a/src/html/util.rs b/src/html/util.rs index 863ea62e..6060d086 100644 --- a/src/html/util.rs +++ b/src/html/util.rs @@ -1,7 +1,8 @@ use crate::html::UrlResolveKind; use crate::DocNodeKind; use serde::Serialize; -use std::collections::{HashMap, HashSet}; +use std::collections::HashMap; +use std::collections::HashSet; use std::rc::Rc; use tinytemplate::TinyTemplate; @@ -91,18 +92,14 @@ pub struct RenderContext<'ctx> { global_symbols: NamespacedGlobalSymbols, global_symbol_href_resolver: GlobalSymbolHrefResolver, pub url_resolver: super::UrlResolver, - current_file: Option, - current_symbol: Option, - current_resolve: Option>, + current_resolve: UrlResolveKind<'ctx>, } impl<'ctx> RenderContext<'ctx> { pub fn new( ctx: &super::GenerateCtx<'ctx>, all_symbols: NamespacedSymbols, - current_file: Option, - current_symbol: Option, - current_resolve: Option>, + current_resolve: UrlResolveKind<'ctx>, ) -> Self { Self { tt: ctx.tt.clone(), @@ -112,8 +109,6 @@ impl<'ctx> RenderContext<'ctx> { global_symbols: ctx.global_symbols.clone(), global_symbol_href_resolver: ctx.global_symbol_href_resolver.clone(), url_resolver: ctx.url_resolver.clone(), - current_file, - current_symbol, current_resolve, } } @@ -135,29 +130,10 @@ impl<'ctx> RenderContext<'ctx> { } } - pub fn with_file(&self, current_file: Option) -> Self { - Self { - current_file, - ..self.clone() - } - } - - pub fn with_symbol(&self, current_symbol: Option) -> Self { - Self { - current_symbol, - ..self.clone() - } - } - pub fn with_current_resolve( &self, - current_resolve: Option>, + current_resolve: UrlResolveKind<'ctx>, ) -> Self { - assert!(matches!( - current_resolve, - None | Some(UrlResolveKind::Root) | Some(UrlResolveKind::AllSymbols) - )); - Self { current_resolve, ..self.clone() @@ -180,21 +156,8 @@ impl<'ctx> RenderContext<'ctx> { self.namespace_parts.clone() } - pub fn get_current_file(&self) -> Option<&str> { - self.current_file.as_deref() - } - - pub fn get_current_symbol(&self) -> Option<&str> { - self.current_symbol.as_deref() - } - pub fn get_current_resolve(&self) -> UrlResolveKind { - match (self.get_current_file(), self.get_current_symbol()) { - (Some(file), Some(symbol)) => UrlResolveKind::Symbol { file, symbol }, - (Some(file), None) => UrlResolveKind::File(file), - (None, None) => self.current_resolve.clone().unwrap(), - _ => unreachable!(), - } + self.current_resolve } pub fn lookup_symbol_href(&self, target_symbol: &str) -> Option { @@ -213,7 +176,7 @@ impl<'ctx> RenderContext<'ctx> { return Some((self.url_resolver)( self.get_current_resolve(), UrlResolveKind::Symbol { - file: self.current_file.as_deref().unwrap(), + file: self.get_current_resolve().get_file().unwrap(), symbol: ¤t_parts.join("."), }, )); @@ -227,7 +190,7 @@ impl<'ctx> RenderContext<'ctx> { return Some((self.url_resolver)( self.get_current_resolve(), UrlResolveKind::Symbol { - file: self.get_current_file().unwrap_or_default(), // TODO + file: self.get_current_resolve().get_file().unwrap_or_default(), // TODO symbol: &target_symbol_parts.join("."), }, ));