summaryrefslogtreecommitdiff
path: root/src/componentparameter.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-02-06 21:20:09 +0100
committertroido <troido@protonmail.com>2020-02-06 21:20:09 +0100
commit45ed2cd201c79fae1c61a4f6f2982a9f5cfbceca (patch)
tree01036ea1ebe1502cfe0645b283dd9595a8b94fae /src/componentparameter.rs
parentc18970e8d1003a7b4b3b95b8ade07226bd235f0f (diff)
grass now also loaded from json
Diffstat (limited to 'src/componentparameter.rs')
-rw-r--r--src/componentparameter.rs27
1 files changed, 25 insertions, 2 deletions
diff --git a/src/componentparameter.rs b/src/componentparameter.rs
index a2f7b70..491486a 100644
--- a/src/componentparameter.rs
+++ b/src/componentparameter.rs
@@ -1,12 +1,14 @@
use std::collections::HashMap;
+use rand::Rng;
use serde_json::Value;
use crate::parameter::{Parameter, ParameterType};
#[derive(Debug, PartialEq)]
pub enum ComponentParameter {
Constant(Parameter),
- Argument(String)
+ Argument(String),
+ Random(Vec<ComponentParameter>)
}
impl ComponentParameter {
@@ -18,6 +20,10 @@ impl ComponentParameter {
Self::Argument(argname) => {
Some(arguments.get(argname.as_str())?.clone())
}
+ Self::Random(options) => {
+ let r = rand::thread_rng().gen_range(0, options.len());
+ options[r].evaluate(arguments)
+ }
}
}
@@ -32,6 +38,14 @@ impl ComponentParameter {
let argname = paramvalue.as_str().ok_or("argument parameter not a string")?.to_string();
Ok(Self::Argument(argname))
},
+ "random" => {
+ let optionvalues = paramvalue.as_array().ok_or("random argument not a a string")?;
+ let mut options = Vec::new();
+ for option in optionvalues {
+ options.push(Self::from_json(option)?)
+ }
+ Ok(Self::Random(options))
+ },
_ => Err("unknown compparam type")
}
}
@@ -40,7 +54,16 @@ impl ComponentParameter {
pub fn get_type(&self, arguments: &Vec<(String, ParameterType, Option<Parameter>)>) -> Result<ParameterType, &'static str>{
Ok(match self {
Self::Constant(param) => param.paramtype(),
- Self::Argument(argname) => arguments.iter().find(|(n, _t, _d)| n == argname).ok_or("unknown argument name")?.1
+ Self::Argument(argname) => arguments.iter().find(|(n, _t, _d)| n == argname).ok_or("unknown argument name")?.1,
+ Self::Random(options) => {
+ let typ: ParameterType = options.get(0).ok_or("random has no options")?.get_type(arguments)?;
+ for param in options {
+ if param.get_type(arguments)? != typ {
+ return Err("inconsistent parameter types");
+ }
+ }
+ typ
+ }
})
}
}