feat: initial serde parser working with custom structs
This commit is contained in:
parent
ba3975e7fd
commit
713994d848
3 changed files with 81 additions and 9 deletions
|
|
@ -1 +1,14 @@
|
||||||
|
use crate::utils::parse::Testcases;
|
||||||
|
|
||||||
mod tasks01;
|
mod tasks01;
|
||||||
|
|
||||||
|
pub fn task_distrubute(testcases: Testcases) {
|
||||||
|
for testcase in testcases.testcases {
|
||||||
|
//match testcase {}
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn task_deploy() {
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
|
|
||||||
25
src/test_json/parse_example.json
Normal file
25
src/test_json/parse_example.json
Normal file
|
|
@ -0,0 +1,25 @@
|
||||||
|
{
|
||||||
|
"testcases": {
|
||||||
|
"b856d760-023d-4b00-bad2-15d2b6da22fe": {
|
||||||
|
"action": "add_numbers",
|
||||||
|
"arguments": {
|
||||||
|
"number1": 123,
|
||||||
|
"number2": 234
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"254eaee7-05fd-4e0d-8292-9b658a852245": {
|
||||||
|
"action": "add_numbers",
|
||||||
|
"arguments": {
|
||||||
|
"number1": 333,
|
||||||
|
"number2": 444
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"affbf4fc-4d2a-41e3-afe0-a79e1d174781": {
|
||||||
|
"action": "subtract_numbers",
|
||||||
|
"arguments": {
|
||||||
|
"number1": 999,
|
||||||
|
"number2": 121212
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,14 +1,48 @@
|
||||||
use serde::{Deserialize, Serialize};
|
use std::collections::HashMap;
|
||||||
use serde_json::Value;
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
use serde::{Deserialize, Serialize};
|
||||||
|
use serde_json::{Result, Value};
|
||||||
|
|
||||||
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Testcases {
|
pub struct Testcases {
|
||||||
testcase: Vec<Testcase>,
|
pub testcases: HashMap<String, Testcase>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
pub struct Testcase {
|
pub struct Testcase {
|
||||||
uuid: String,
|
pub action: String,
|
||||||
action: String,
|
pub arguments: Value,
|
||||||
arguments: Value,
|
}
|
||||||
}
|
|
||||||
|
pub fn parse_json(json: String) -> Result<Testcases> {
|
||||||
|
let deserialised: Testcases = serde_json::from_str(&json)?;
|
||||||
|
Ok(deserialised)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
|
use serde_json::json;
|
||||||
|
|
||||||
|
// Note this useful idiom: importing names from outer (for mod tests) scope.
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_json_parsing() {
|
||||||
|
let json = fs::read_to_string("src/test_json/parse_example.json").unwrap();
|
||||||
|
let parsed = parse_json(json).unwrap();
|
||||||
|
assert!(
|
||||||
|
!parsed.testcases.is_empty(),
|
||||||
|
"Testcases struct was: {:?}",
|
||||||
|
parsed.testcases
|
||||||
|
);
|
||||||
|
assert!(
|
||||||
|
!parsed
|
||||||
|
.testcases
|
||||||
|
.contains_key("\"b856d760-023d-4b00-bad2-15d2b6da22fe\""),
|
||||||
|
"Testcases first element was: {:?}",
|
||||||
|
parsed.testcases
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue