Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: expose gamedata to json #95

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ jobs:
~/.cargo/git/db/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
# make sure we are on rust 1.79
# see https://github.com/tuffy/bitstream-io/commit/241eaed73b0cd814640ceaf01b8220e5c98927d6#commitcomment-143198388
# TODO remove this once github is on a newer version of rust
- name: Set up Rust
run: rustup update
- name: Build
run: cargo build --verbose
- name: Run tests
Expand Down
59 changes: 59 additions & 0 deletions src/vpx/jsonmodel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashMap;

use crate::vpx::collection::Collection;
use crate::vpx::custominfotags::CustomInfoTags;
use crate::vpx::gamedata::{GameData, GameDataJson};
use crate::vpx::tableinfo::TableInfo;

#[derive(Serialize, Deserialize)]
Expand Down Expand Up @@ -113,3 +114,61 @@ pub fn json_to_collections(json: serde_json::Value) -> Result<Vec<Collection>, s
}
Ok(collections)
}

pub fn game_data_to_json(game_data: &GameData) -> serde_json::Value {
let game_data_json = GameDataJson::from_game_data(game_data);
to_value(game_data_json).unwrap()
}

#[cfg(test)]
mod tests {
use super::*;
use crate::vpx::collection::Collection;
use crate::vpx::custominfotags::CustomInfoTags;
use crate::vpx::gamedata::GameData;
use crate::vpx::tableinfo::TableInfo;
use serde_json::Value;

#[test]
fn test_info_to_json() {
let table_info = TableInfo::default();
let custom_info_tags = CustomInfoTags::default();
let json = info_to_json(&table_info, &custom_info_tags);
let (table_info2, custom_info_tags2) = json_to_info(json, None).unwrap();
assert_eq!(table_info, table_info2);
assert_eq!(custom_info_tags, custom_info_tags2);
}

#[test]
fn test_collections_to_json() {
let collections = vec![
Collection {
name: "collection1".to_string(),
items: vec!["item1".to_string(), "item2".to_string()],
fire_events: true,
stop_single_events: false,
group_elements: true,
},
Collection {
name: "collection2".to_string(),
items: vec!["item3".to_string(), "item4".to_string()],
fire_events: false,
stop_single_events: true,
group_elements: false,
},
];
let json = collections_json(&collections);
let collections2 = json_to_collections(json).unwrap();
assert_eq!(collections, collections2);
}

#[test]
fn test_game_data_to_json() {
let game_data = GameData::default();
let json = game_data_to_json(&game_data);
// assert that we have a value of type object that at least contains "name": String("Table1")
let map = json.as_object().unwrap();
let name = map.get("name").unwrap();
assert_eq!(name, &Value::String("Table1".to_string()));
}
}