From c067ca9cc898b9080cd542331a5f3d73b84fbe5b Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 22 Feb 2025 15:22:35 -0500 Subject: [PATCH] Introduce an enum for a separate highlighted src block type. --- src/context/src_block.rs | 31 +++++++++++++++++++++++++------ src/intermediate/src_block.rs | 35 +++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 14 deletions(-) diff --git a/src/context/src_block.rs b/src/context/src_block.rs index 2f6298d..f6af667 100644 --- a/src/context/src_block.rs +++ b/src/context/src_block.rs @@ -6,19 +6,38 @@ use crate::intermediate::ISrcBlock; use super::macros::render; +#[derive(Debug, Serialize)] +#[serde(untagged)] +pub(crate) enum RenderSrcBlock { + Plain(RenderPlainSrcBlock), + Highlighted(RenderHighlightedSrcBlock), +} + #[derive(Debug, Serialize)] #[serde(tag = "type")] #[serde(rename = "src_block")] -pub(crate) struct RenderSrcBlock { +pub(crate) struct RenderPlainSrcBlock { + lines: Vec, + language: Option, + post_blank: organic::types::PostBlank, +} + +#[derive(Debug, Serialize)] +#[serde(tag = "type")] +#[serde(rename = "highlighted_src_block")] +pub(crate) struct RenderHighlightedSrcBlock { lines: Vec, language: Option, post_blank: organic::types::PostBlank, } render!(RenderSrcBlock, ISrcBlock, original, _render_context, { - Ok(RenderSrcBlock { - lines: original.lines.clone(), - language: original.language.clone(), - post_blank: original.post_blank, - }) + match original { + ISrcBlock::Plain(plain_src_block) => Ok(RenderSrcBlock::Plain(RenderPlainSrcBlock { + lines: plain_src_block.lines.clone(), + language: plain_src_block.language.clone(), + post_blank: plain_src_block.post_blank, + })), + ISrcBlock::Highlighted(_) => todo!(), + } }); diff --git a/src/intermediate/src_block.rs b/src/intermediate/src_block.rs index 0055d46..5566cbf 100644 --- a/src/intermediate/src_block.rs +++ b/src/intermediate/src_block.rs @@ -1,3 +1,4 @@ +use std::borrow::Borrow; use std::borrow::Cow; use super::macros::intermediate; @@ -9,7 +10,20 @@ use tree_sitter_highlight::HighlightEvent; use tree_sitter_highlight::Highlighter; #[derive(Debug, Clone)] -pub(crate) struct ISrcBlock { +pub(crate) enum ISrcBlock { + Plain(PlainSrcBlock), + Highlighted(HighlightedSrcBlock), +} + +#[derive(Debug, Clone)] +pub(crate) struct PlainSrcBlock { + pub(crate) lines: Vec, + pub(crate) language: Option, + pub(crate) post_blank: organic::types::PostBlank, +} + +#[derive(Debug, Clone)] +pub(crate) struct HighlightedSrcBlock { pub(crate) lines: Vec, pub(crate) language: Option, pub(crate) post_blank: organic::types::PostBlank, @@ -66,18 +80,20 @@ intermediate!( .collect(); let language = original.language.map(str::to_owned); - let lines = match language.as_ref().map(String::as_str) { + match language.as_ref().map(String::as_str) { Some("nix") => { - // foo - highlight_nix(lines)? + let highlighted = highlight_nix(&lines); + if let Ok(highlighted) = highlighted { + // return the other type + } } - _ => lines, + _ => {} }; - Ok(ISrcBlock { + Ok(ISrcBlock::Plain(PlainSrcBlock { lines, language, post_blank: original.get_post_blank(), - }) + })) } ); @@ -91,7 +107,10 @@ fn ascii_whitespace_value(c: char) -> usize { } } -fn highlight_nix(lines: Vec) -> Result, CustomError> { +fn highlight_nix(lines: &[L]) -> Result, CustomError> +where + L: Borrow, +{ let highlight_names = ["comment", "keyword"]; // Need 1 highlighter per thread let mut highlighter = Highlighter::new();