Fix missing attribute error message and add more span data

This commit is contained in:
elkowar 2021-08-01 15:52:44 +02:00
parent afd0a47bf2
commit e24cc0ac16
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F
3 changed files with 16 additions and 6 deletions

View file

@ -14,6 +14,7 @@ macro_rules! builtin_vars {
name: VarName::from($name), name: VarName::from($name),
command: VarSource::Function($fun), command: VarSource::Function($fun),
interval: $interval, interval: $interval,
name_span: eww_shared_util::span::Span::DUMMY,
}) })
),* ),*
} }

View file

@ -17,7 +17,7 @@ use eww_shared_util::{AttrName, Span, Spanned, VarName};
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum AttrError { pub enum AttrError {
#[error("Missing required attribute {0}")] #[error("Missing required attribute {1}")]
MissingRequiredAttr(Span, AttrName), MissingRequiredAttr(Span, AttrName),
#[error("{1}")] #[error("{1}")]

View file

@ -10,7 +10,7 @@ use crate::{
from_ast::{FromAst, FromAstElementContent}, from_ast::{FromAst, FromAstElementContent},
}, },
}; };
use eww_shared_util::{AttrName, Span, VarName}; use eww_shared_util::{AttrName, Span, Spanned, VarName};
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)] #[derive(Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub enum ScriptVarDefinition { pub enum ScriptVarDefinition {
@ -19,6 +19,13 @@ pub enum ScriptVarDefinition {
} }
impl ScriptVarDefinition { impl ScriptVarDefinition {
pub fn name_span(&self) -> Span {
match self {
ScriptVarDefinition::Poll(x) => x.name_span,
ScriptVarDefinition::Listen(x) => x.name_span,
}
}
pub fn name(&self) -> &VarName { pub fn name(&self) -> &VarName {
match self { match self {
ScriptVarDefinition::Poll(x) => &x.name, ScriptVarDefinition::Poll(x) => &x.name,
@ -50,6 +57,7 @@ pub struct PollScriptVar {
pub name: VarName, pub name: VarName,
pub command: VarSource, pub command: VarSource,
pub interval: std::time::Duration, pub interval: std::time::Duration,
pub name_span: Span,
} }
impl FromAstElementContent for PollScriptVar { impl FromAstElementContent for PollScriptVar {
@ -58,12 +66,12 @@ 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) = iter.expect_symbol()?; let (name_span, name) = iter.expect_symbol()?;
let mut attrs = iter.expect_key_values()?; let mut attrs = iter.expect_key_values()?;
let interval = attrs.primitive_required::<DynVal, _>("interval")?.as_duration()?; let interval = attrs.primitive_required::<DynVal, _>("interval")?.as_duration()?;
let (script_span, script) = iter.expect_literal()?; let (script_span, script) = iter.expect_literal()?;
iter.expect_done()?; iter.expect_done()?;
Ok(Self { name: VarName(name), command: VarSource::Shell(script_span, script.to_string()), interval }) Ok(Self { name_span, name: VarName(name), command: VarSource::Shell(script_span, script.to_string()), interval })
} }
} }
@ -72,6 +80,7 @@ pub struct ListenScriptVar {
pub name: VarName, pub name: VarName,
pub command: String, pub command: String,
pub command_span: Span, pub command_span: Span,
pub name_span: Span,
} }
impl FromAstElementContent for ListenScriptVar { impl FromAstElementContent for ListenScriptVar {
fn get_element_name() -> &'static str { fn get_element_name() -> &'static str {
@ -79,9 +88,9 @@ 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 (_, 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: VarName(name), command: script.to_string(), command_span }) Ok(Self { name_span, name: VarName(name), command: script.to_string(), command_span })
} }
} }