got rid of HashMap unwrap

This commit is contained in:
Anton-4 2021-02-26 18:22:44 +01:00
parent c501529685
commit 7610ad38f8
4 changed files with 43 additions and 16 deletions

View File

@ -295,7 +295,7 @@ fn run_event_loop(file_path_opt: Option<&Path>) -> Result<(), Box<dyn Error>> {
)
.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<dyn Error>> {
&config,
&mut glyph_brush,
);
if let Err(e) = ast_render_res {
print_err(&e)
}
}
rects_arena.reset();

View File

@ -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<f32>,
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<f32>,
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<HighlightStyle, RgbaTup>,
font_size: f32,
) -> Vec<wgpu_glyph::Text<'a>> {
// TODO remove unwrap
) -> EdResult<Vec<wgpu_glyph::Text<'a>>> {
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()
)
}

View File

@ -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,

View File

@ -14,8 +14,9 @@ pub fn slice_get<T>(index: usize, slice: &[T]) -> EdResult<&<usize as SliceIndex
Ok(elt_ref)
}
pub fn map_get<K: ::std::fmt::Debug + std::hash::Hash + std::cmp::Eq, V>
(hash_map: &HashMap<K, V>, 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<K, V>, key: &K) -> EdResult<&'a V> {
let value =
hash_map.get(key).context(
@ -26,3 +27,4 @@ pub fn map_get<K: ::std::fmt::Debug + std::hash::Hash + std::cmp::Eq, V>
Ok(value)
}