diff --git a/crates/eww/src/widgets/def_widget_macro.rs b/crates/eww/src/widgets/def_widget_macro.rs index 72ae7ea69..785cc2869 100644 --- a/crates/eww/src/widgets/def_widget_macro.rs +++ b/crates/eww/src/widgets/def_widget_macro.rs @@ -16,7 +16,7 @@ macro_rules! def_widget { // If an attribute is explicitly marked as optional (? appended to type) // the attribute will still show up here, as a `None` value. Otherwise, all values in this map // will be `Some`. - let attr_map: Result>> = try { + let attr_map: Result>> = try { ::maplit::hashmap! { $( eww_shared_util::AttrName(::std::stringify!($attr_name).to_owned()) => @@ -79,14 +79,14 @@ macro_rules! def_widget { (@value_depending_on_type $values:expr, $attr_name:ident : as_action $(? $(@ $optional:tt @)?)? $(= $default:expr)?) => { match $attr_name { - Some(yuck::config::attr_value::AttrValue::Action(action)) => Some(action.eval_exprs(&$values)?), + Some(yuck::config::action::AttrValue::Action(action)) => Some(action.eval_exprs(&$values)?), _ => None, } }; (@value_depending_on_type $values:expr, $attr_name:ident : $typecast_func:ident $(? $(@ $optional:tt @)?)? $(= $default:expr)?) => { match $attr_name { - Some(yuck::config::attr_value::AttrValue::SimplExpr(expr)) => Some(expr.eval(&$values)?.$typecast_func()?), + Some(yuck::config::action::AttrValue::SimplExpr(expr)) => Some(expr.eval(&$values)?.$typecast_func()?), _ => None, } }; @@ -103,7 +103,9 @@ macro_rules! def_widget { // The attribute has a default value (@get_value $args:ident, $name:expr, = $default:expr) => { - Some($args.widget_use.attrs.ast_optional::($name)?.clone().unwrap_or_else(|| simplexpr::SimplExpr::synth_literal($default))) + Some($args.widget_use.attrs.ast_optional::($name)? + .clone() + .unwrap_or_else(|| yuck::config::action::AttrValue::SimplExpr(simplexpr::SimplExpr::synth_literal($default)))) }; // The attribute is required - the prop will only be ran if this attribute is actually provided. diff --git a/crates/eww/src/widgets/widget_definitions.rs b/crates/eww/src/widgets/widget_definitions.rs index 54949e21b..4b04e7a6e 100644 --- a/crates/eww/src/widgets/widget_definitions.rs +++ b/crates/eww/src/widgets/widget_definitions.rs @@ -1,5 +1,5 @@ #![allow(clippy::option_map_unit_fn)] -use super::{build_widget::BuilderArgs, circular_progressbar::*, transform::*, run_command}; +use super::{build_widget::BuilderArgs, circular_progressbar::*, run_command, transform::*}; use crate::{ def_widget, enum_parse, error::DiagError, diff --git a/crates/yuck/src/config/action.rs b/crates/yuck/src/config/action.rs index d9c19152d..48427e78d 100644 --- a/crates/yuck/src/config/action.rs +++ b/crates/yuck/src/config/action.rs @@ -1,5 +1,7 @@ +use std::collections::HashMap; + use eww_shared_util::VarName; -use simplexpr::SimplExpr; +use simplexpr::{dynval::DynVal, eval::EvalError, SimplExpr}; pub static ACTION_NAMES: &[&str] = &["update"]; @@ -10,13 +12,32 @@ pub enum AttrValue { SimplExpr(SimplExpr), } -#[derive(Debug, Clone)] +impl AttrValue { + pub fn try_into_simplexpr(&self) -> Option<&SimplExpr> { + match self { + Self::SimplExpr(x) => Some(x), + _ => None, + } + } +} + +#[derive(Debug, Clone, Eq, PartialEq)] pub enum Action { - Update(Update), + Update(VarName, SimplExpr), + Noop, } -#[derive(Debug, Clone)] -pub struct Update { - pub varname: VarName, - pub value: SimplExpr, +impl Action { + pub fn eval_exprs(&self, values: &HashMap) -> Result { + Ok(match self { + Self::Update(varname, expr) => ResolvedAction::Update(varname.clone(), expr.eval(values)?), + Self::Noop => ResolvedAction::Noop, + }) + } +} + +#[derive(Debug, Clone, Eq, PartialEq)] +pub enum ResolvedAction { + Update(VarName, DynVal), + Noop, } diff --git a/crates/yuck/src/parser/from_ast.rs b/crates/yuck/src/parser/from_ast.rs index 1c2544b40..2138d3cec 100644 --- a/crates/yuck/src/parser/from_ast.rs +++ b/crates/yuck/src/parser/from_ast.rs @@ -2,11 +2,7 @@ use super::{ ast::{Ast, AstType}, ast_iterator::AstIterator, }; -use crate::{ - config::action::{AttrValue, Update}, - error::*, - parser, -}; +use crate::{config::action::AttrValue, error::*, parser}; use eww_shared_util::{AttrName, Span, VarName}; use itertools::Itertools; use simplexpr::{ast::SimplExpr, dynval::DynVal}; @@ -71,7 +67,7 @@ impl FromAst for Action { let (varname_span, varname) = iter.expect_symbol()?; let (value_span, value) = iter.expect_simplexpr()?; iter.expect_done()?; - Ok(Action::Update(Update { varname: VarName(varname), value })) + Ok(Action::Update(VarName(varname), value)) } _ => Err(AstError::UnknownAction(span, action)), } @@ -79,6 +75,9 @@ impl FromAst for Action { } impl FromAst for AttrValue { fn from_ast(e: Ast) -> AstResult { - todo!() + match &e { + Ast::List(..) => Ok(AttrValue::Action(Action::from_ast(e)?)), + _ => Ok(AttrValue::SimplExpr(SimplExpr::from_ast(e)?)), + } } }