summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-04-03 11:22:04 +0200
committertroido <troido@protonmail.com>2020-04-03 11:22:04 +0200
commit0702ea943b801176649fee860715fed0f5348c8d (patch)
tree6e13028e79683c325c41d125921c0fe9a22d3386 /src
parentb597d2279db7beb0d619fac472481b7f707609c8 (diff)
allow lists as parameter types, sacrificing some type safety for expressability
Diffstat (limited to 'src')
-rw-r--r--src/assemblage.rs2
-rw-r--r--src/components/item.rs1
-rw-r--r--src/componentwrapper.rs10
-rw-r--r--src/parameter.rs40
4 files changed, 44 insertions, 9 deletions
diff --git a/src/assemblage.rs b/src/assemblage.rs
index 95988cc..0550617 100644
--- a/src/assemblage.rs
+++ b/src/assemblage.rs
@@ -133,7 +133,7 @@ impl Assemblage {
ComponentType::Flags,
hashmap!(
"flags".to_string() => ComponentParameter::Constant(
- Parameter::from_typed_json(ParameterType::Strings, flags).ok_or(aerr!("failed to parse flags"))?
+ Parameter::from_typed_json(ParameterType::List, flags).ok_or(aerr!("failed to parse flags"))?
)
)
));
diff --git a/src/components/item.rs b/src/components/item.rs
index 40be7f1..0cb2589 100644
--- a/src/components/item.rs
+++ b/src/components/item.rs
@@ -53,6 +53,7 @@ mod tests {
use super::*;
use crate::hashmap;
use super::super::equipment::*;
+ use serde_json::json;
#[test]
fn equip_from_json() {
diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs
index 898ab1b..0fb46d0 100644
--- a/src/componentwrapper.rs
+++ b/src/componentwrapper.rs
@@ -202,11 +202,17 @@ components!(
};
Equipment () {panic!("equipment from parameters not implemented")};
CreationTime (time: Int) {CreationTime{time: Timestamp(time)}};
- Flags (flags: Strings) {
+ Flags (flags: List) {
Flags(
flags
.iter()
- .map(|f| Flag::from_str(f))
+ .map(|param| {
+ if let Parameter::String(f) = param {
+ Flag::from_str(f)
+ } else {
+ None
+ }
+ })
.collect::<Option<HashSet<Flag>>>().ok_or(aerr!("invalid flag name"))?
)
};
diff --git a/src/parameter.rs b/src/parameter.rs
index a9ef898..3423f69 100644
--- a/src/parameter.rs
+++ b/src/parameter.rs
@@ -77,11 +77,15 @@ parameters!(
Some((Template::from_json(item.get(0)?).ok()?, item.get(1)?.as_f64()?))
).collect::<Option<Vec<(Template, f64)>>>()?)
({json!(v.iter().map(|(t, c)| (t.to_json(), *c)).collect::<Vec<(Value, f64)>>())});
- Strings (Vec<String>) strings, v
- (v.as_array()?.iter().map(|item|
- Some(item.as_str()?.to_string())
- ).collect::<Option<Vec<String>>>()?)
- ({json!(v)});
+ List (Vec<Parameter>) list, v
+ ({
+ v
+ .as_array()?
+ .iter()
+ .map(|item| Parameter::guess_from_json(item))
+ .collect::<Option<Vec<Parameter>>>()?
+ })
+ (panic!("can not serialise parameter list"));
);
@@ -126,7 +130,7 @@ mod tests {
use serde_json::json;
macro_rules! gfj { // guess from json
- ($j:expr) => {Parameter::guess_from_json(&json!($j)).unwrap()}
+ ($($j:tt)*) => {Parameter::guess_from_json(&json!($($j)*)).unwrap()}
}
#[test]
@@ -149,6 +153,8 @@ mod tests {
assert_eq!(gfj!(0.0), Parameter::Float(0.0));
assert_eq!(gfj!(-0.0), Parameter::Float(0.0));
assert_eq!(gfj!(true), Parameter::Bool(true));
+
+ assert_eq!(gfj!(["int", 3]), Parameter::Int(3));
}
#[test]
@@ -156,4 +162,26 @@ mod tests {
assert!(Parameter::guess_from_json(&json!([2, 5])).is_none());
assert!(Parameter::guess_from_json(&json!({"hello": "world"})).is_none());
}
+
+ #[test]
+ fn parse_list() {
+ assert_eq!(
+ gfj!(["list", [5, 3, 1, 2]]),
+ Parameter::List(vec![
+ Parameter::Int(5),
+ Parameter::Int(3),
+ Parameter::Int(1),
+ Parameter::Int(2)
+ ])
+ );
+ assert_eq!(
+ gfj!(["list", [5, 3.0, "Hello", true]]),
+ Parameter::List(vec![
+ Parameter::Int(5),
+ Parameter::Float(3.0),
+ Parameter::string("Hello"),
+ Parameter::Bool(true)
+ ])
+ );
+ }
}