@ -4,7 +4,7 @@ use std::{
str ,
str ,
} ;
} ;
use anyhow ::{ anyhow , Context , Result } ;
use anyhow ::{ anyhow , Result } ;
use helix_core ::hashmap ;
use helix_core ::hashmap ;
use helix_loader ::merge_toml_values ;
use helix_loader ::merge_toml_values ;
use log ::warn ;
use log ::warn ;
@ -209,10 +209,8 @@ pub struct Theme {
impl From < Value > for Theme {
impl From < Value > for Theme {
fn from ( value : Value ) -> Self {
fn from ( value : Value ) -> Self {
let values : Result < HashMap < String , Value > > =
if let Value ::Table ( table ) = value {
toml ::from_str ( & value . to_string ( ) ) . context ( "Failed to load theme" ) ;
let ( styles , scopes , highlights ) = build_theme_values ( table ) ;
let ( styles , scopes , highlights ) = build_theme_values ( values ) ;
Self {
Self {
styles ,
styles ,
@ -220,6 +218,10 @@ impl From<Value> for Theme {
highlights ,
highlights ,
.. Default ::default ( )
.. Default ::default ( )
}
}
} else {
warn ! ( "Expected theme TOML value to be a table, found {:?}" , value ) ;
Default ::default ( )
}
}
}
}
}
@ -228,9 +230,9 @@ impl<'de> Deserialize<'de> for Theme {
where
where
D : Deserializer < ' de > ,
D : Deserializer < ' de > ,
{
{
let values = Hash Map::< String , Value > ::deserialize ( deserializer ) ? ;
let values = Map::< String , Value > ::deserialize ( deserializer ) ? ;
let ( styles , scopes , highlights ) = build_theme_values ( Ok ( values ) ) ;
let ( styles , scopes , highlights ) = build_theme_values ( values ) ;
Ok ( Self {
Ok ( Self {
styles ,
styles ,
@ -242,15 +244,14 @@ impl<'de> Deserialize<'de> for Theme {
}
}
fn build_theme_values (
fn build_theme_values (
values : Result < Hash Map< String , Value > > ,
mut values : Map< String , Value > ,
) -> ( HashMap < String , Style > , Vec < String > , Vec < Style > ) {
) -> ( HashMap < String , Style > , Vec < String > , Vec < Style > ) {
let mut styles = HashMap ::new ( ) ;
let mut styles = HashMap ::new ( ) ;
let mut scopes = Vec ::new ( ) ;
let mut scopes = Vec ::new ( ) ;
let mut highlights = Vec ::new ( ) ;
let mut highlights = Vec ::new ( ) ;
if let Ok ( mut colors ) = values {
// TODO: alert user of parsing failures in editor
// TODO: alert user of parsing failures in editor
let palette = color s
let palette = value s
. remove ( "palette" )
. remove ( "palette" )
. map ( | value | {
. map ( | value | {
ThemePalette ::try_from ( value ) . unwrap_or_else ( | err | {
ThemePalette ::try_from ( value ) . unwrap_or_else ( | err | {
@ -260,11 +261,11 @@ fn build_theme_values(
} )
} )
. unwrap_or_default ( ) ;
. unwrap_or_default ( ) ;
// remove inherits from value to prevent errors
// remove inherits from value to prevent errors
let _ = color s. remove ( "inherits" ) ;
let _ = value s. remove ( "inherits" ) ;
styles . reserve ( color s. len ( ) ) ;
styles . reserve ( value s. len ( ) ) ;
scopes . reserve ( color s. len ( ) ) ;
scopes . reserve ( value s. len ( ) ) ;
highlights . reserve ( color s. len ( ) ) ;
highlights . reserve ( value s. len ( ) ) ;
for ( name , style_value ) in color s {
for ( name , style_value ) in value s {
let mut style = Style ::default ( ) ;
let mut style = Style ::default ( ) ;
if let Err ( err ) = palette . parse_style ( & mut style , style_value ) {
if let Err ( err ) = palette . parse_style ( & mut style , style_value ) {
warn ! ( "{}" , err ) ;
warn ! ( "{}" , err ) ;
@ -275,7 +276,6 @@ fn build_theme_values(
scopes . push ( name ) ;
scopes . push ( name ) ;
highlights . push ( style ) ;
highlights . push ( style ) ;
}
}
}
( styles , scopes , highlights )
( styles , scopes , highlights )
}
}