Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example implementation of variables #124

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ pub enum Value {
Str(String),
Ident(String),
Rgba8(Rgba8),
Var(String),
}

impl Value {
Expand Down Expand Up @@ -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",
Expand All @@ -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",
}
}
}
Expand Down Expand Up @@ -461,13 +470,15 @@ 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),
}
}
}

impl Parse for Value {
fn parser() -> Parser<Self> {
let str_val = quoted().map(Value::Str).label("<string>");
let var_val = variable().map(Value::Var).label("<variable>");
let rgba8_val = color().map(Value::Rgba8);
let u32_tuple_val = tuple::<u32>(natural(), natural()).map(|(x, y)| Value::U32Tuple(x, y));
let u32_val = natural::<u32>().map(Value::U32);
Expand All @@ -488,6 +499,7 @@ impl Parse for Value {
f64_val,
bool_val,
ident_val,
var_val,
str_val,
])
.label("<value>")
Expand Down
7 changes: 7 additions & 0 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ pub fn scale() -> Parser<u32> {
.map(|(_, scale)| scale)
}

pub fn variable() -> Parser<String> {
symbol('&')
.then(identifier())
.label("&<variable>")
.map(|(_, variable)| variable)
}

pub fn path() -> Parser<String> {
token()
.map(|input: String| {
Expand Down
20 changes: 13 additions & 7 deletions src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand Down