Skip to content

Commit

Permalink
Implement keys func
Browse files Browse the repository at this point in the history
Implement values func
  • Loading branch information
mitghi committed Mar 25, 2023
1 parent e77ee54 commit 4613fe6
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,58 @@ impl Callable for MapFn {
}
}

struct Keys;
impl Callable for Keys {
fn call(
&mut self,
func: &Func,
value: &Value,
_ctx: Option<&mut Context<'_>>,
) -> Result<Value, Error> {
if func.args.len() > 0 {
return Err(Error::FuncEval("keys takes no argument".to_owned()));
}

match &value {
Value::Object(ref obj) => {
let keys: Vec<Value> = obj.keys().map(|v| Value::String(v.to_string())).collect();
return Ok(Value::Array(keys));
}
_ => {
return Err(Error::FuncEval(
"keys can be exec only on objects".to_owned(),
));
}
}
}
}

struct Values;
impl Callable for Values {
fn call(
&mut self,
func: &Func,
value: &Value,
_ctx: Option<&mut Context<'_>>,
) -> Result<Value, Error> {
if func.args.len() > 0 {
return Err(Error::FuncEval("values takes no argument".to_owned()));
}

match &value {
Value::Object(ref obj) => {
let values: Vec<Value> = obj.values().map(|v| v.clone()).collect();
return Ok(Value::Array(values));
}
_ => {
return Err(Error::FuncEval(
"values can be exec only on objects".to_owned(),
));
}
}
}
}

impl Default for FuncRegistry {
fn default() -> Self {
let mut output = FuncRegistry::new();
Expand All @@ -332,6 +384,8 @@ impl Default for FuncRegistry {
output.register("tail", Box::new(Tail));
output.register("all", Box::new(AllOnBoolean));
output.register("map", Box::new(MapFn));
output.register("keys", Box::new(Keys));
output.register("values", Box::new(Values));
output
}
}
Expand Down

0 comments on commit 4613fe6

Please sign in to comment.