ksudoku.rs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // Read a Ksudoku's XML save file.
  2. use serde_derive::{Deserialize, Serialize};
  3. use serde_xml_rs::from_reader; // , from_str, to_string};
  4. use serde_xml_rs::to_writer;
  5. use std::error::Error;
  6. use std::fs::File;
  7. #[derive(Debug, Serialize, Deserialize, PartialEq)]
  8. struct Graph {
  9. order: i32,
  10. #[serde(rename = "type")]
  11. game_type: String,
  12. #[serde(rename = "specific-type")]
  13. specific_type: String,
  14. }
  15. #[derive(Debug, Serialize, Deserialize, PartialEq)]
  16. struct Puzzle {
  17. graph: Graph,
  18. values: String,
  19. solution: String,
  20. }
  21. #[derive(Debug, Serialize, Deserialize, PartialEq)]
  22. struct Game {
  23. #[serde(rename = "had-help")]
  24. help: i16,
  25. #[serde(rename = "msecs-elapsed")]
  26. elapsed: u32,
  27. puzzle: Puzzle,
  28. }
  29. #[derive(Debug, Serialize, Deserialize, PartialEq)]
  30. struct Ksudoku {
  31. game: Game,
  32. }
  33. pub fn load_ksudoku(filename: std::path::PathBuf) -> Result<String, Box<dyn Error>> {
  34. let fh = File::open(filename)?;
  35. let puzzle: Ksudoku = from_reader(fh)?;
  36. Ok(puzzle.game.puzzle.values.clone())
  37. }
  38. pub fn save_ksudoku(filename: std::path::PathBuf, puz: &String, sol: &String) -> Result<(), Box<dyn Error>> {
  39. /*
  40. let pStr: String = puzzle.save_to_tld(start_ch: 'b', blank: '_');
  41. let solStr: solution.save_to_tld(start_ch: 'b', blank: '_');
  42. */
  43. let ksudoko = Ksudoku {
  44. game: Game {
  45. help: 0,
  46. elapsed: 0,
  47. puzzle: Puzzle {
  48. graph: Graph {
  49. order: 9,
  50. game_type: String::from("sudoku"),
  51. specific_type: String::from("Plain"),
  52. },
  53. values: puz.to_string(),
  54. solution: sol.to_string(),
  55. },
  56. },
  57. };
  58. let fh = File::create(filename)?;
  59. let _res = to_writer(fh, &ksudoko)?;
  60. Ok(())
  61. }