diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/reader/syntax.rs b/src/app/browser/window/tab/item/page/content/text/gemini/reader/syntax.rs index 4aa1ed82..16594b7e 100644 --- a/src/app/browser/window/tab/item/page/content/text/gemini/reader/syntax.rs +++ b/src/app/browser/window/tab/item/page/content/text/gemini/reader/syntax.rs @@ -1,3 +1,6 @@ +pub mod error; +pub use error::Error; + use adw::StyleManager; use gtk::{ gdk::RGBA, @@ -9,7 +12,6 @@ use syntect::{ easy::HighlightLines, highlighting::{Color, FontStyle, ThemeSet}, parsing::{SyntaxReference, SyntaxSet}, - Error, }; /* Default theme @@ -46,38 +48,26 @@ impl Syntax { ) -> Result, Error> { if let Some(value) = alt { if let Some(reference) = self.syntax_set.find_syntax_by_name(value) { - return self.syntect_buffer(source_code, source_tag, reference); + return self.buffer(source_code, source_tag, reference); } if let Some(reference) = self.syntax_set.find_syntax_by_token(value) { - return self.syntect_buffer(source_code, source_tag, reference); + return self.buffer(source_code, source_tag, reference); } if let Some(reference) = self.syntax_set.find_syntax_by_path(value) { - return self.syntect_buffer(source_code, source_tag, reference); + return self.buffer(source_code, source_tag, reference); } } if let Some(reference) = self.syntax_set.find_syntax_by_first_line(source_code) { - return self.syntect_buffer(source_code, source_tag, reference); + return self.buffer(source_code, source_tag, reference); } - Ok(self.default_buffer(source_code, source_tag)) + Err(Error::Parse) } - fn default_buffer(&self, source: &str, source_tag: &TextTag) -> Vec<(TextTag, String)> { - // Init new line buffer - let mut buffer = Vec::new(); - - // Create new tag preset from source - let tag = new_text_tag_from(source_tag); - - // Append - buffer.push((tag, source.to_string())); - buffer - } - - fn syntect_buffer( + fn buffer( &self, source: &str, source_tag: &TextTag, @@ -87,32 +77,35 @@ impl Syntax { let mut buffer = Vec::new(); // Apply syntect decorator - let ranges = HighlightLines::new( + let mut ranges = HighlightLines::new( syntax_reference, &self.theme_set.themes[if StyleManager::default().is_dark() { DEFAULT_THEME_DARK } else { DEFAULT_THEME_LIGHT }], // @TODO apply on env change - ) - .highlight_line(source, &self.syntax_set)?; + ); - // Build tags - for (style, entity) in ranges { - // Create new tag preset from source - let tag = new_text_tag_from(source_tag); + match ranges.highlight_line(source, &self.syntax_set) { + Ok(result) => { + // Build tags + for (style, entity) in result { + // Create new tag preset from source + let tag = new_text_tag_from(source_tag); - // Tuneup using syntect conversion - // tag.set_background_rgba(Some(&color_to_rgba(style.background))); - tag.set_foreground_rgba(Some(&color_to_rgba(style.foreground))); - tag.set_style(font_style_to_style(style.font_style)); - tag.set_underline(font_style_to_underline(style.font_style)); + // Tuneup using syntect conversion + // tag.set_background_rgba(Some(&color_to_rgba(style.background))); + tag.set_foreground_rgba(Some(&color_to_rgba(style.foreground))); + tag.set_style(font_style_to_style(style.font_style)); + tag.set_underline(font_style_to_underline(style.font_style)); - // Append - buffer.push((tag, entity.to_string())); + // Append + buffer.push((tag, entity.to_string())); + } + Ok(buffer) + } + Err(e) => Err(Error::Syntect(e)), } - - Ok(buffer) } } diff --git a/src/app/browser/window/tab/item/page/content/text/gemini/reader/syntax/error.rs b/src/app/browser/window/tab/item/page/content/text/gemini/reader/syntax/error.rs new file mode 100644 index 00000000..ae9bfdb6 --- /dev/null +++ b/src/app/browser/window/tab/item/page/content/text/gemini/reader/syntax/error.rs @@ -0,0 +1,18 @@ +use std::fmt::{Display, Formatter, Result}; + +#[derive(Debug)] +pub enum Error { + Parse, + Syntect(syntect::Error), +} + +impl Display for Error { + fn fmt(&self, f: &mut Formatter) -> Result { + match self { + Self::Parse => write!(f, "Parse error"), + Self::Syntect(e) => { + write!(f, "Syntect error: {e}") + } + } + } +}