// Read a Ksudoku's XML save file. use serde_derive::{Deserialize, Serialize}; use serde_xml_rs::from_reader; // , from_str, to_string}; use serde_xml_rs::to_writer; use std::error::Error; use std::fs::File; #[derive(Debug, Serialize, Deserialize, PartialEq)] struct Graph { order: i32, #[serde(rename = "type")] game_type: String, #[serde(rename = "specific-type")] specific_type: String, } #[derive(Debug, Serialize, Deserialize, PartialEq)] struct Puzzle { graph: Graph, values: String, solution: String, } #[derive(Debug, Serialize, Deserialize, PartialEq)] struct Game { #[serde(rename = "had-help")] help: i16, #[serde(rename = "msecs-elapsed")] elapsed: u32, puzzle: Puzzle, } #[derive(Debug, Serialize, Deserialize, PartialEq)] struct Ksudoku { game: Game, } pub fn load_ksudoku(filename: std::path::PathBuf) -> Result> { let fh = File::open(filename)?; let puzzle: Ksudoku = from_reader(fh)?; Ok(puzzle.game.puzzle.values.clone()) } pub fn save_ksudoku(filename: std::path::PathBuf, puz: &String, sol: &String) -> Result<(), Box> { /* let pStr: String = puzzle.save_to_tld(start_ch: 'b', blank: '_'); let solStr: solution.save_to_tld(start_ch: 'b', blank: '_'); */ let ksudoko = Ksudoku { game: Game { help: 0, elapsed: 0, puzzle: Puzzle { graph: Graph { order: 9, game_type: String::from("sudoku"), specific_type: String::from("Plain"), }, values: puz.to_string(), solution: sol.to_string(), }, }, }; let fh = File::create(filename)?; let _res = to_writer(fh, &ksudoko)?; Ok(()) }