Skip to content

Commit

Permalink
Dev #70
Browse files Browse the repository at this point in the history
  • Loading branch information
vulogov committed Sep 14, 2024
1 parent 517fd5e commit 1923405
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ repository = "https://github.com/vulogov/rust_dynamic"
bincode2 = "2.0.*"
chrono = "0.4.*"
dtoa = "1.0.*"
easy-error = "1.0.0"
itoa = "1.0.*"
nanoid = "0.4.*"
num = "0.4.*"
Expand Down
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SOURCES=$(wildcard src/*.rs)


all: $(SOURCES) Makefile
cargo build

rebuild:
make clean
make all

test:
cargo test

clean:
cargo clean
cargo update

all:
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ While rust_dynamic crate is not strive to provide a full-featured functional int
| Value.fmap() | Execute function to each element of the LIST or to the value and return new Value |
| Value.map_float() | Execute function to each FLOAT element of the LIST or to the value and return new Value |
| Value.push() | Ether add a new value to the list, or return a new Value |
| Value.push_inplace() | Push new Value to supported Value (LIST, QUEUE, FIFO, RESULT, METRIC) or return Error |
| Value.maybe() | Takes a function which is if returns true, Value.maybe() returns value, if false Value::none() |
| Value::left_right() | Takes a function which is if returns true, and a references on two Values. Value::left_right() returns clone of first value, if function return true, second othewise |
| Value::freduce() | Takes two parameters - function which takes two values and returning a single value and initial value, reducing dynamic value to a single value by applying function to all elements |
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub mod export;
pub mod types;
pub mod error;
pub mod push;
pub mod push_inplace;
pub mod pull;
pub mod pop;
pub mod hash;
Expand Down
62 changes: 62 additions & 0 deletions src/push_inplace.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
use crate::value::{Value};
use crate::metric::Metric;
use crate::types::*;
use easy_error::{Error, bail};

impl Value {
pub fn push_inplace(&mut self, value: Value) -> Result<&mut Value, Error> {
match self.dt {
LIST | RESULT => {
match &mut self.data {
Val::List(ref mut v) => {
let _ = v.push(value.clone());
}
_ => {
bail!("Invalid List format for push() operation");
}
}
return Ok(self);
}
QUEUE | FIFO => {
match &mut self.data {
Val::Queue(ref mut v) => {
let _ = v.push(value.clone());
}
_ => {
bail!("Invalid Queue/Fifo format for push() operation");
}
}
return Ok(self)
}
LAMBDA => {
match &mut self.data {
Val::Lambda(ref mut v) => {
let _ = v.push(value.clone());
}
_ => {
bail!("Invalid Lambda format for push() operation");
}
}
return Ok(self);
}
METRICS => {
if value.dt != FLOAT {
return Ok(self);
}
match &mut self.data {
Val::Metrics(ref mut v) => {
let _ = &v.push(Metric::new(value.cast_float().unwrap()));
let _ = &v.remove(0);
return Ok(self);
}
_ => {
bail!("Invalid Metric format for push() operation");
}
}
}
_ => {
bail!("This Value do not support push() operation");
}
}
}
}
7 changes: 7 additions & 0 deletions tests/push-test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,11 @@ mod tests {
let val = v.cast_list().unwrap();
assert_eq!(val[val.len()-1].cast_int().unwrap(), 42);
}

#[test]
fn test_push_list_push_inplace() {
let mut v = Value::list();
v.push_inplace(Value::from_int(42)).unwrap();
assert_eq!(v.len(), 1);
}
}

0 comments on commit 1923405

Please sign in to comment.