Fix error messages for script-var definitions
This commit is contained in:
parent
e24cc0ac16
commit
27ce9e798a
6 changed files with 35 additions and 23 deletions
|
@ -8,7 +8,7 @@ use gtk::{self, prelude::*, ImageExt};
|
||||||
use std::{cell::RefCell, collections::HashMap, rc::Rc, time::Duration};
|
use std::{cell::RefCell, collections::HashMap, rc::Rc, time::Duration};
|
||||||
use yuck::{
|
use yuck::{
|
||||||
config::validate::ValidationError,
|
config::validate::ValidationError,
|
||||||
error::{AstError, AstResult, ResultExt},
|
error::{AstError, AstResult, AstResultExt},
|
||||||
parser::from_ast::FromAst,
|
parser::from_ast::FromAst,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -3,7 +3,7 @@ use std::collections::HashMap;
|
||||||
use simplexpr::{dynval::DynVal, SimplExpr};
|
use simplexpr::{dynval::DynVal, SimplExpr};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error::{AstError, AstResult},
|
error::{AstError, AstResult, AstResultExt},
|
||||||
parser::{
|
parser::{
|
||||||
ast::Ast,
|
ast::Ast,
|
||||||
ast_iterator::AstIterator,
|
ast_iterator::AstIterator,
|
||||||
|
@ -66,12 +66,15 @@ impl FromAstElementContent for PollScriptVar {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_tail<I: Iterator<Item = Ast>>(span: Span, mut iter: AstIterator<I>) -> AstResult<Self> {
|
fn from_tail<I: Iterator<Item = Ast>>(span: Span, mut iter: AstIterator<I>) -> AstResult<Self> {
|
||||||
let (name_span, name) = iter.expect_symbol()?;
|
let result: AstResult<_> = try {
|
||||||
let mut attrs = iter.expect_key_values()?;
|
let (name_span, name) = iter.expect_symbol()?;
|
||||||
let interval = attrs.primitive_required::<DynVal, _>("interval")?.as_duration()?;
|
let mut attrs = iter.expect_key_values()?;
|
||||||
let (script_span, script) = iter.expect_literal()?;
|
let interval = attrs.primitive_required::<DynVal, _>("interval")?.as_duration()?;
|
||||||
iter.expect_done()?;
|
let (script_span, script) = iter.expect_literal()?;
|
||||||
Ok(Self { name_span, name: VarName(name), command: VarSource::Shell(script_span, script.to_string()), interval })
|
iter.expect_done()?;
|
||||||
|
Self { name_span, name: VarName(name), command: VarSource::Shell(script_span, script.to_string()), interval }
|
||||||
|
};
|
||||||
|
result.note(r#"Expected format: `(defpoll name :interval "10s" "echo 'a shell script'")`"#)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,9 +91,12 @@ impl FromAstElementContent for ListenScriptVar {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_tail<I: Iterator<Item = Ast>>(span: Span, mut iter: AstIterator<I>) -> AstResult<Self> {
|
fn from_tail<I: Iterator<Item = Ast>>(span: Span, mut iter: AstIterator<I>) -> AstResult<Self> {
|
||||||
|
let result: AstResult<_> = try {
|
||||||
let (name_span, name) = iter.expect_symbol()?;
|
let (name_span, name) = iter.expect_symbol()?;
|
||||||
let (command_span, script) = iter.expect_literal()?;
|
let (command_span, script) = iter.expect_literal()?;
|
||||||
iter.expect_done()?;
|
iter.expect_done()?;
|
||||||
Ok(Self { name_span, name: VarName(name), command: script.to_string(), command_span })
|
Self { name_span, name: VarName(name), command: script.to_string(), command_span }
|
||||||
|
};
|
||||||
|
result.note(r#"Expected format: `(deflisten name "tail -f /tmp/example")`"#)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,7 +14,7 @@ Config(
|
||||||
widget: WidgetUse(
|
widget: WidgetUse(
|
||||||
name: "foo",
|
name: "foo",
|
||||||
attrs: Attributes(
|
attrs: Attributes(
|
||||||
span: Span(51, 61, 62),
|
span: Span(51, 61, 0),
|
||||||
attrs: {
|
attrs: {
|
||||||
AttrName("arg"): AttrEntry(
|
AttrName("arg"): AttrEntry(
|
||||||
key_span: Span(52, 56, 0),
|
key_span: Span(52, 56, 0),
|
||||||
|
@ -52,7 +52,7 @@ Config(
|
||||||
widget: WidgetUse(
|
widget: WidgetUse(
|
||||||
name: "bar",
|
name: "bar",
|
||||||
attrs: Attributes(
|
attrs: Attributes(
|
||||||
span: Span(467, 478, 479),
|
span: Span(467, 478, 0),
|
||||||
attrs: {
|
attrs: {
|
||||||
AttrName("arg"): AttrEntry(
|
AttrName("arg"): AttrEntry(
|
||||||
key_span: Span(468, 472, 0),
|
key_span: Span(468, 472, 0),
|
||||||
|
@ -88,6 +88,7 @@ Config(
|
||||||
name: VarName("stuff"),
|
name: VarName("stuff"),
|
||||||
command: "tail -f stuff",
|
command: "tail -f stuff",
|
||||||
command_span: Span(168, 183, 0),
|
command_span: Span(168, 183, 0),
|
||||||
|
name_span: Span(162, 167, 0),
|
||||||
)),
|
)),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
|
@ -2,14 +2,11 @@ use std::collections::HashMap;
|
||||||
|
|
||||||
use simplexpr::{dynval::DynVal, SimplExpr};
|
use simplexpr::{dynval::DynVal, SimplExpr};
|
||||||
|
|
||||||
use crate::{
|
use crate::{error::{AstResult, AstResultExt}, parser::{
|
||||||
error::AstResult,
|
|
||||||
parser::{
|
|
||||||
ast::Ast,
|
ast::Ast,
|
||||||
ast_iterator::AstIterator,
|
ast_iterator::AstIterator,
|
||||||
from_ast::{FromAst, FromAstElementContent},
|
from_ast::{FromAst, FromAstElementContent},
|
||||||
},
|
}};
|
||||||
};
|
|
||||||
use eww_shared_util::{AttrName, Span, VarName};
|
use eww_shared_util::{AttrName, Span, VarName};
|
||||||
|
|
||||||
#[derive(Debug, PartialEq, Eq, Clone, serde::Serialize)]
|
#[derive(Debug, PartialEq, Eq, Clone, serde::Serialize)]
|
||||||
|
@ -25,9 +22,12 @@ impl FromAstElementContent for VarDefinition {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_tail<I: Iterator<Item = Ast>>(span: Span, mut iter: AstIterator<I>) -> AstResult<Self> {
|
fn from_tail<I: Iterator<Item = Ast>>(span: Span, mut iter: AstIterator<I>) -> AstResult<Self> {
|
||||||
let (_, name) = iter.expect_symbol()?;
|
let result: AstResult<_> = try {
|
||||||
let (_, initial_value) = iter.expect_literal()?;
|
let (_, name) = iter.expect_symbol()?;
|
||||||
iter.expect_done()?;
|
let (_, initial_value) = iter.expect_literal()?;
|
||||||
Ok(Self { name: VarName(name), initial_value, span })
|
iter.expect_done()?;
|
||||||
|
Self { name: VarName(name), initial_value, span }
|
||||||
|
};
|
||||||
|
result.note(r#"Expected format: `(defvar name "initial-value")`"#)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -122,12 +122,17 @@ impl<T> OptionAstErrorExt<T> for Option<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait ResultExt<T> {
|
pub trait AstResultExt<T> {
|
||||||
fn context_label(self, label_span: Span, context: &str) -> AstResult<T>;
|
fn context_label(self, label_span: Span, context: &str) -> AstResult<T>;
|
||||||
|
fn note(self, note: &str) -> AstResult<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> ResultExt<T> for AstResult<T> {
|
impl<T> AstResultExt<T> for AstResult<T> {
|
||||||
fn context_label(self, label_span: Span, context: &str) -> AstResult<T> {
|
fn context_label(self, label_span: Span, context: &str) -> AstResult<T> {
|
||||||
self.map_err(|e| AstError::ErrorContext { label_span, context: context.to_string(), main_err: Box::new(e) })
|
self.map_err(|e| AstError::ErrorContext { label_span, context: context.to_string(), main_err: Box::new(e) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn note(self, note: &str) -> AstResult<T> {
|
||||||
|
self.map_err(|e| e.note(note))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -98,7 +98,7 @@ impl<I: Iterator<Item = Ast>> Iterator for AstIterator<I> {
|
||||||
/// Parse consecutive `:keyword value` pairs from an expression iterator into an [Attributes].
|
/// Parse consecutive `:keyword value` pairs from an expression iterator into an [Attributes].
|
||||||
fn parse_key_values(iter: &mut AstIterator<impl Iterator<Item = Ast>>) -> AstResult<Attributes> {
|
fn parse_key_values(iter: &mut AstIterator<impl Iterator<Item = Ast>>) -> AstResult<Attributes> {
|
||||||
let mut data = HashMap::new();
|
let mut data = HashMap::new();
|
||||||
let mut attrs_span = Span(iter.remaining_span.0, iter.remaining_span.0, iter.remaining_span.1);
|
let mut attrs_span = iter.remaining_span.point_span();
|
||||||
loop {
|
loop {
|
||||||
match iter.next() {
|
match iter.next() {
|
||||||
Some(Ast::Keyword(key_span, kw)) => match iter.next() {
|
Some(Ast::Keyword(key_span, kw)) => match iter.next() {
|
||||||
|
|
Loading…
Add table
Reference in a new issue