From 7610ad38f86b6f36cac051062dd8b3986ff5d13d Mon Sep 17 00:00:00 2001 From: Anton-4 <17049058+Anton-4@users.noreply.github.com> Date: Fri, 26 Feb 2021 18:22:44 +0100 Subject: [PATCH] got rid of HashMap unwrap --- editor/src/editor/main.rs | 6 +++- editor/src/editor/render_ast.rs | 45 ++++++++++++++++++++------- editor/src/editor/syntax_highlight.rs | 2 +- editor/src/editor/util.rs | 6 ++-- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/editor/src/editor/main.rs b/editor/src/editor/main.rs index 74829b5c5b..583610afb7 100644 --- a/editor/src/editor/main.rs +++ b/editor/src/editor/main.rs @@ -295,7 +295,7 @@ fn run_event_loop(file_path_opt: Option<&Path>) -> Result<(), Box> { ) .unwrap(); - super::render_ast::render_expr2( + let ast_render_res = super::render_ast::render_expr2( &ast_arena, &mut env, &expr2, @@ -304,6 +304,10 @@ fn run_event_loop(file_path_opt: Option<&Path>) -> Result<(), Box> { &config, &mut glyph_brush, ); + + if let Err(e) = ast_render_res { + print_err(&e) + } } rects_arena.reset(); diff --git a/editor/src/editor/render_ast.rs b/editor/src/editor/render_ast.rs index b55a6707b3..8d7312f3b1 100644 --- a/editor/src/editor/render_ast.rs +++ b/editor/src/editor/render_ast.rs @@ -1,5 +1,10 @@ use crate::lang::pool::PoolStr; -use crate::editor::syntax_highlight::HighlightStyle; +use crate::editor::{ + syntax_highlight::HighlightStyle, + util::map_get, + ed_error::EdResult, +}; + use crate::graphics::colors::RgbaTup; use crate::graphics::primitives::text as gr_text; use bumpalo::collections::String as BumpString; @@ -130,11 +135,11 @@ pub fn render_expr2<'a>( position: Vector2, config: &Config, glyph_brush: &mut GlyphBrush<()>, -) { +) -> EdResult<()> { // TODO formatting code let highlight_tups = highlight_expr2(arena, env, expr2); - queue_code_text_draw(&highlight_tups, size, position, config, glyph_brush); + queue_code_text_draw(&highlight_tups, size, position, config, glyph_brush) } pub fn queue_code_text_draw<'a>( @@ -143,7 +148,7 @@ pub fn queue_code_text_draw<'a>( position: Vector2, config: &Config, glyph_brush: &mut GlyphBrush<()>, -) { +) -> EdResult<()> { let area_bounds = (size.width as f32, size.height as f32); let layout = wgpu_glyph::Layout::default().h_align(wgpu_glyph::HorizontalAlign::Left); @@ -151,8 +156,9 @@ pub fn queue_code_text_draw<'a>( highlight_tups_to_glyph_text( &highlight_tups, &config.ed_theme.syntax_high_map, - config.code_font_size - ); + config.code_font_size, + + )?; let section = gr_text::section_from_glyph_text( glyph_text_vec, @@ -162,21 +168,36 @@ pub fn queue_code_text_draw<'a>( ); glyph_brush.queue(section.clone()); + + Ok(()) } fn highlight_tups_to_glyph_text<'a>( highlight_tups: &'a BumpVec<'a, (BumpString<'a>, HighlightStyle)>, syntax_theme: &HashMap, font_size: f32, -) -> Vec> { - // TODO remove unwrap +) -> EdResult>> { - highlight_tups + let arena = Bump::new(); + let mut colored_str_tups: BumpVec<(&BumpString, &RgbaTup)> = BumpVec::new_in(&arena); + + for (token_str, highlight_style) in highlight_tups.iter() { + let highlight_color_res = map_get(&syntax_theme, highlight_style); + + match highlight_color_res { + Ok(highlight_color) => colored_str_tups.push((token_str, highlight_color)), + Err(e) => return Err(e), + } + } + + Ok( + colored_str_tups .iter() - .map(|(token_str, highlight_style)| { - wgpu_glyph::Text::new(&token_str) - .with_color(colors::to_slice(*syntax_theme.get(highlight_style).unwrap())) + .map(|(token_str, highlight_color)| { + wgpu_glyph::Text::new(token_str) + .with_color(colors::to_slice(**highlight_color)) .with_scale(font_size) }) .collect() + ) } diff --git a/editor/src/editor/syntax_highlight.rs b/editor/src/editor/syntax_highlight.rs index 98dabdb55f..6c94bb13a0 100644 --- a/editor/src/editor/syntax_highlight.rs +++ b/editor/src/editor/syntax_highlight.rs @@ -2,7 +2,7 @@ use crate::graphics::colors as gr_colors; use gr_colors::{from_hsb, RgbaTup}; use std::collections::HashMap; -#[derive(Hash, Eq, PartialEq, Copy, Clone)] +#[derive(Hash, Eq, PartialEq, Copy, Clone, Debug)] pub enum HighlightStyle { Operator, // =+-<>... String, diff --git a/editor/src/editor/util.rs b/editor/src/editor/util.rs index 8ab8b4cc63..f34695b9cb 100644 --- a/editor/src/editor/util.rs +++ b/editor/src/editor/util.rs @@ -14,8 +14,9 @@ pub fn slice_get(index: usize, slice: &[T]) -> EdResult<& - (hash_map: &HashMap, key: &K) -> EdResult<&V> { +// replace HashMap method that returns Option with one that returns Result and proper Error +pub fn map_get<'a, K: ::std::fmt::Debug + std::hash::Hash + std::cmp::Eq, V> + (hash_map: &'a HashMap, key: &K) -> EdResult<&'a V> { let value = hash_map.get(key).context( @@ -26,3 +27,4 @@ pub fn map_get Ok(value) } +