implement error handler, return Error::Parse on source could not be processed

This commit is contained in:
yggverse 2024-12-05 01:53:47 +02:00
parent 3f1e8a9780
commit 3e2205015a
2 changed files with 46 additions and 35 deletions

View File

@ -1,3 +1,6 @@
pub mod error;
pub use error::Error;
use adw::StyleManager; use adw::StyleManager;
use gtk::{ use gtk::{
gdk::RGBA, gdk::RGBA,
@ -9,7 +12,6 @@ use syntect::{
easy::HighlightLines, easy::HighlightLines,
highlighting::{Color, FontStyle, ThemeSet}, highlighting::{Color, FontStyle, ThemeSet},
parsing::{SyntaxReference, SyntaxSet}, parsing::{SyntaxReference, SyntaxSet},
Error,
}; };
/* Default theme /* Default theme
@ -46,38 +48,26 @@ impl Syntax {
) -> Result<Vec<(TextTag, String)>, Error> { ) -> Result<Vec<(TextTag, String)>, Error> {
if let Some(value) = alt { if let Some(value) = alt {
if let Some(reference) = self.syntax_set.find_syntax_by_name(value) { 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) { 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) { 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) { 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)> { fn buffer(
// 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(
&self, &self,
source: &str, source: &str,
source_tag: &TextTag, source_tag: &TextTag,
@ -87,18 +77,19 @@ impl Syntax {
let mut buffer = Vec::new(); let mut buffer = Vec::new();
// Apply syntect decorator // Apply syntect decorator
let ranges = HighlightLines::new( let mut ranges = HighlightLines::new(
syntax_reference, syntax_reference,
&self.theme_set.themes[if StyleManager::default().is_dark() { &self.theme_set.themes[if StyleManager::default().is_dark() {
DEFAULT_THEME_DARK DEFAULT_THEME_DARK
} else { } else {
DEFAULT_THEME_LIGHT DEFAULT_THEME_LIGHT
}], // @TODO apply on env change }], // @TODO apply on env change
) );
.highlight_line(source, &self.syntax_set)?;
match ranges.highlight_line(source, &self.syntax_set) {
Ok(result) => {
// Build tags // Build tags
for (style, entity) in ranges { for (style, entity) in result {
// Create new tag preset from source // Create new tag preset from source
let tag = new_text_tag_from(source_tag); let tag = new_text_tag_from(source_tag);
@ -111,9 +102,11 @@ impl Syntax {
// Append // Append
buffer.push((tag, entity.to_string())); buffer.push((tag, entity.to_string()));
} }
Ok(buffer) Ok(buffer)
} }
Err(e) => Err(Error::Syntect(e)),
}
}
} }
// Tools // Tools

View File

@ -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}")
}
}
}
}