diff --git a/src/tasks/mod.rs b/src/tasks/mod.rs index 2c05297..05b3956 100644 --- a/src/tasks/mod.rs +++ b/src/tasks/mod.rs @@ -5,14 +5,18 @@ use std::{ }; use crate::utils::parse::{Responses, Testcase, Testcases}; -use tasks01::poly2block::{self, poly2block}; +use tasks01::{ + block2poly::block2poly, + poly2block::{self, poly2block}, +}; +use anyhow::{anyhow, Result}; use serde::{Deserialize, Serialize}; use serde_json::{json, Value}; mod tasks01; -pub fn task_deploy(testcase: &Testcase) -> Result { +pub fn task_deploy(testcase: &Testcase) -> Result { /* * Function to automatially distribute task workloads * TODO: Add functionality to also pass semantics @@ -27,10 +31,12 @@ pub fn task_deploy(testcase: &Testcase) -> Result { let json = json!({"block" : result}); Ok(json) } - _ => Err(format!( - "Fatal error in task distribution. Data was: {:?}", - args - )), + "block2poly" => { + let result: Vec = block2poly(args)?; + let json = json!({"block" : result}); + Ok(json) + } + _ => Err(anyhow!("Fatal. No compatible action found")), } } diff --git a/src/tasks/tasks01/block2poly.rs b/src/tasks/tasks01/block2poly.rs index df0f164..d44fcb4 100644 --- a/src/tasks/tasks01/block2poly.rs +++ b/src/tasks/tasks01/block2poly.rs @@ -1,11 +1,17 @@ -use std::str::Bytes; +use std::{str::Bytes, string}; use crate::utils::poly; +use anyhow::Result; use base64::prelude::*; +use serde_json::Value; -fn block2poly(block: &str) -> Vec { +pub fn block2poly(val: &Value) -> Result> { // Convert JSON data in to a u128 - let decoded: Vec = BASE64_STANDARD.decode(block).unwrap(); + // TODO: Transfer decoding into own function? + eprintln!("Decoded is: {:?}", val["block"]); + let string: String = serde_json::from_value(val["block"].clone())?; + let decoded: Vec = BASE64_STANDARD.decode(string)?; + let mut bytes: [u8; 16] = [0u8; 16]; bytes.copy_from_slice(&decoded); let number: u128 = ::from_ne_bytes(bytes); @@ -14,39 +20,34 @@ fn block2poly(block: &str) -> Vec { for shift in 0..128 { //println!("{:?}", ((num >> shift) & 1)); - if (((number >> shift) & 1) == 1) { + if ((number >> shift) & 1) == 1 { println!("Shift success"); coefficients.push(shift); } } - //Legacy code. - // TODO: Remove - /* - let mut counter: u8 = 0; - let mut coefficients: Vec = vec![]; - for blk in decoded { - let indices: Vec = poly::get_bit_indices_from_byte(blk); - for index in indices { - coefficients.push(counter*8+index); - } - counter += 1; - } - */ - coefficients + Ok(coefficients) } #[cfg(test)] mod tests { + use serde_json::json; use std::str::FromStr; // Note this useful idiom: importing names from outer (for mod tests) scope. use super::*; #[test] - fn block2poly_task01() { - let block: &str = "ARIAAAAAAAAAAAAAAAAAgA=="; + fn block2poly_task01() -> Result<()> { + let block: Value = json!({"block" : "ARIAAAAAAAAAAAAAAAAAgA=="}); let coefficients: Vec = vec![0, 9, 12, 127]; - assert_eq!(block2poly(block), coefficients); + assert_eq!( + block2poly(&block)?, + coefficients, + "Coefficients were: {:?}", + block2poly(&block)? + ); + + Ok(()) } -} \ No newline at end of file +} diff --git a/src/tasks/tasks01/poly2block.rs b/src/tasks/tasks01/poly2block.rs index 5d64250..b9bacab 100644 --- a/src/tasks/tasks01/poly2block.rs +++ b/src/tasks/tasks01/poly2block.rs @@ -9,5 +9,6 @@ pub fn poly2block(args: &Value) -> String { .into_iter() .map(|x| x.as_u64().unwrap() as u8) .collect(); + BASE64_STANDARD.encode(poly::coefficient_to_binary(coefficients).to_ne_bytes()) } diff --git a/src/test_json/b2p_p2b_example.json b/src/test_json/b2p_p2b_example.json new file mode 100644 index 0000000..4eb968f --- /dev/null +++ b/src/test_json/b2p_p2b_example.json @@ -0,0 +1,23 @@ +{ + "testcases": { + "b856d760-023d-4b00-bad2-15d2b6da22fe": { + "action": "block2poly", + "arguments": { + "semantic": "xex", + "block": "ARIAAAAAAAAAAAAAAAAAgA==" + } + }, + "254eaee7-05fd-4e0d-8292-9b658a852245": { + "action": "poly2block", + "arguments": { + "semantic": "xex", + "coefficients": [ + 12, + 127, + 9, + 0 + ] + } + } + } +} diff --git a/src/test_json/block2poly_example.json b/src/test_json/block2poly_example.json new file mode 100644 index 0000000..4b4d541 --- /dev/null +++ b/src/test_json/block2poly_example.json @@ -0,0 +1,10 @@ +{ + "testcases": { + "b856d760-023d-4b00-bad2-15d2b6da22fe": { + "action": "block2poly", + "arguments": { + "semantic": "xex", + "block": "ARIAAAAAAAAAAAAAAAAAgA==" + } + } +} \ No newline at end of file