casino 0.3.0

A casino built right into your terminal
Documentation
use std::{fs, path::Path};

use anyhow::{Context, Result};
use serde::{Deserialize, Serialize};

use crate::{cards::Shoe, money::Money};

#[derive(Deserialize, Debug, Serialize)]
pub struct State {
    pub bankroll: Money,
}

impl State {
    pub fn load(path: &Path) -> Result<Self> {
        let state_string = fs::read_to_string(path)?;
        let state: Self = toml::from_str(&state_string)
            .with_context(|| "Unable to parse Casino state from file")?;

        Ok(state)
    }

    pub fn save(&self, path: &Path) -> Result<()> {
        fs::create_dir_all(path.parent().unwrap())
            .with_context(|| "Couldn't create save directory")?;
        fs::write(
            path,
            toml::to_string(&self).expect("Couldn't serialize save data!"),
        )
        .with_context(|| "Failed to write state to path")
    }
}

#[derive(Deserialize, Debug, Serialize)]
pub struct BlackjackState {
    pub shoe: Shoe,
}