From c5e7b5a4dbb8759b85c5d9926c4dfc48ffb8f04b Mon Sep 17 00:00:00 2001 From: Mabry Cervin Date: Thu, 23 Sep 2021 21:05:09 -0400 Subject: [PATCH] Example implementation of variables --- src/cmd.rs | 12 ++++++++++++ src/parser.rs | 7 +++++++ src/session.rs | 20 +++++++++++++------- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/src/cmd.rs b/src/cmd.rs index 463feb1..66c7efb 100644 --- a/src/cmd.rs +++ b/src/cmd.rs @@ -377,6 +377,7 @@ pub enum Value { Str(String), Ident(String), Rgba8(Rgba8), + Var(String), } impl Value { @@ -408,6 +409,13 @@ impl Value { panic!("expected {:?} to be a `Rgba8`", self); } + pub fn to_var(&self) -> &str { + if let Value::Var(var) = self { + return &var; + } + panic!("expected {:?} to be a `variable`", self); + } + pub fn description(&self) -> &'static str { match self { Self::Bool(_) => "on / off", @@ -418,6 +426,7 @@ impl Value { Self::Str(_) => "string, eg. \"fnord\"", Self::Rgba8(_) => "color, eg. #ffff00", Self::Ident(_) => "identifier, eg. fnord", + Self::Var(_) => "variable, eg. &grid", } } } @@ -461,6 +470,7 @@ impl fmt::Display for Value { Value::Str(s) => s.fmt(f), Value::Rgba8(c) => c.fmt(f), Value::Ident(i) => i.fmt(f), + Value::Var(v) => v.fmt(f), } } } @@ -468,6 +478,7 @@ impl fmt::Display for Value { impl Parse for Value { fn parser() -> Parser { let str_val = quoted().map(Value::Str).label(""); + let var_val = variable().map(Value::Var).label(""); let rgba8_val = color().map(Value::Rgba8); let u32_tuple_val = tuple::(natural(), natural()).map(|(x, y)| Value::U32Tuple(x, y)); let u32_val = natural::().map(Value::U32); @@ -488,6 +499,7 @@ impl Parse for Value { f64_val, bool_val, ident_val, + var_val, str_val, ]) .label("") diff --git a/src/parser.rs b/src/parser.rs index 05f1368..3767a4b 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -45,6 +45,13 @@ pub fn scale() -> Parser { .map(|(_, scale)| scale) } +pub fn variable() -> Parser { + symbol('&') + .then(identifier()) + .label("&") + .map(|(_, variable)| variable) +} + pub fn path() -> Parser { token() .map(|input: String| { diff --git a/src/session.rs b/src/session.rs index cbbbcf1..918a23d 100644 --- a/src/session.rs +++ b/src/session.rs @@ -2834,13 +2834,19 @@ impl Session { ); return; } - match self.settings.set(k, v.clone()) { - Err(e) => { - self.message(format!("Error: {}", e), MessageType::Error); - } - Ok(ref old) => { - if old != v { - self.setting_changed(k, old, v); + let val = match v { + Value::Var(_) => self.settings.get(v.to_var()).map(|r| r.clone()), + _ => Some(v.clone()), + }; + if let Some(ref v) = val { + match self.settings.set(k, v.clone()) { + Err(e) => { + self.message(format!("Error: {}", e), MessageType::Error); + } + Ok(ref old) => { + if old != v { + self.setting_changed(k, old, v); + } } } }