Improve error messages for multiple chidren in a widget and config loading errors

This commit is contained in:
elkowar 2021-08-14 11:36:39 +02:00
parent 279785f420
commit c0581e7769
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F
4 changed files with 22 additions and 4 deletions

View file

@ -67,7 +67,13 @@ impl EwwConfig {
}
pub fn get_window(&self, name: &String) -> Result<&EwwWindowDefinition> {
self.windows.get(name).with_context(|| format!("No window named '{}' exists in config", name))
self.windows.get(name).with_context(|| {
format!(
"No window named '{}' exists in config.\nThis may also be caused by your config failing to load properly, \
please check for any other errors in that case.",
name
)
})
}
pub fn get_script_var(&self, name: &VarName) -> Result<&ScriptVarDefinition> {

View file

@ -10,7 +10,7 @@ use crate::{
from_ast::{FromAst, FromAstElementContent},
},
};
use eww_shared_util::{AttrName, Span, VarName};
use eww_shared_util::{AttrName, Span, Spanned, VarName};
use super::widget_use::WidgetUse;
#[derive(Debug, PartialEq, Eq, Clone, serde::Serialize)]
@ -33,7 +33,7 @@ impl FromAstElementContent for WidgetDefinition {
.note(EXPECTED_WIDGET_DEF_FORMAT)?;
let expected_args = expected_args.into_iter().map(|x| x.as_symbol().map(AttrName)).collect::<AstResult<_>>()?;
let widget = iter.expect_any().note(EXPECTED_WIDGET_DEF_FORMAT).and_then(WidgetUse::from_ast)?;
iter.expect_done()?;
iter.expect_done().map_err(|e| FormFormatError::WidgetDefMultipleChildren(e.span()))?;
Ok(Self { name, expected_args, widget, span, args_span })
}
}

View file

@ -160,12 +160,16 @@ impl<T> AstResultExt<T> for AstResult<T> {
pub enum FormFormatError {
#[error("Widget definition missing argument list")]
WidgetDefArglistMissing(Span),
#[error("Widget definition has more than one child widget")]
WidgetDefMultipleChildren(Span),
}
impl Spanned for FormFormatError {
fn span(&self) -> Span {
match self {
FormFormatError::WidgetDefArglistMissing(span) => *span,
FormFormatError::WidgetDefMultipleChildren(span) => *span,
}
}
}

View file

@ -254,7 +254,15 @@ impl ToDiagnostic for FormFormatError {
FormFormatError::WidgetDefArglistMissing(span) => gen_diagnostic! {
msg = self,
label = span => "Insert the argument list (e.g.: `[]`) here",
note = "This list will in the future need to declare all the non-global variables / attributes used in this widget.\nThis is not yet neccessary, but is still considered good style.",
note = "This list will in the future need to declare all the non-global variables / attributes used in this widget.\n\
This is not yet neccessary, but is still considered good style.",
},
FormFormatError::WidgetDefMultipleChildren(span) => gen_diagnostic! {
msg = self,
label = span => "Found more than one child element here.",
note = "A widget-definition may only contain one child element.\n\
To include multiple elements, wrap these elements in a single container widget such as `box`.\n\
This is necessary as eww can't know how you want these elements to be layed out otherwise."
},
}
}