use std::fs::OpenOptions;
use std::io::{self, Write};
use std::path::Path;
use super::hand_history::{HandHistory, OpenHandHistoryWrapper};
pub fn write_hand<W: Write>(writer: &mut W, hand: HandHistory) -> io::Result<()> {
let wrapped = OpenHandHistoryWrapper { ohh: hand };
let mut buf = serde_json::to_vec(&wrapped)?;
buf.extend_from_slice(b"\n\n");
writer.write_all(&buf)
}
pub fn append_hand(path: &Path, hand: HandHistory) -> io::Result<()> {
let mut file = OpenOptions::new().create(true).append(true).open(path)?;
write_hand(&mut file, hand)
}
#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use tempfile::NamedTempFile;
#[test]
fn test_append_hand() {
let temp_file = NamedTempFile::new().unwrap();
let path = temp_file.path();
let hand = HandHistory {
spec_version: "1.4.7".to_string(),
site_name: "Test Site".to_string(),
network_name: "Test Network".to_string(),
internal_version: "1.0".to_string(),
tournament: false,
tournament_info: None,
game_number: "123".to_string(),
start_date_utc: None,
table_name: "Test Table".to_string(),
table_handle: None,
table_skin: None,
game_type: super::super::hand_history::GameType::Holdem,
bet_limit: None,
table_size: 9,
currency: "USD".to_string(),
dealer_seat: 1,
small_blind_amount: 1.0,
big_blind_amount: 2.0,
ante_amount: 0.0,
hero_player_id: None,
players: vec![],
rounds: vec![],
pots: vec![],
tournament_bounties: None,
};
append_hand(path, hand).unwrap();
let contents = fs::read_to_string(path).unwrap();
dbg!(&contents);
assert!(contents.contains("Test Site"));
assert!(contents.ends_with("\n\n"));
let parsed: OpenHandHistoryWrapper = serde_json::from_str(contents.trim()).unwrap();
assert_eq!(parsed.ohh.site_name, "Test Site");
}
}