ksudoku.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Read a Ksudoku's XML save file.
  2. use std::fs::File;
  3. use std::io::BufReader;
  4. use xml::reader::{EventReader, XmlEvent};
  5. use xml::writer::XmlEvent as WrXmlEvent;
  6. use xml::EmitterConfig;
  7. use std::error::Error;
  8. pub fn load_ksudoku(filename: std::path::PathBuf) -> Result<String, Box<dyn Error>> {
  9. let fh = File::open(filename)?;
  10. let buffer = BufReader::new(fh);
  11. let parser = EventReader::new(buffer);
  12. // I'm interested in values and (maybe) solution
  13. let mut values: String = String::from("");
  14. let mut element_name: String = String::from("");
  15. for e in parser {
  16. match e {
  17. Ok(XmlEvent::StartElement { name, .. }) => {
  18. element_name = name.local_name.clone();
  19. if element_name == "graph" {
  20. // Check the attributes here
  21. // <graph specific-type="Plain" type="sudoku" order="9"/>
  22. // Verify these are correct / not some other puzzle type!
  23. }
  24. }
  25. Ok(XmlEvent::Characters(text)) => {
  26. if element_name == "values" {
  27. values = text.clone();
  28. }
  29. }
  30. Err(e) => {
  31. return Err(Box::new(e));
  32. }
  33. _ => {}
  34. }
  35. }
  36. Ok(values)
  37. }
  38. pub fn save_ksudoku(
  39. filename: std::path::PathBuf,
  40. puz: &String,
  41. sol: &String,
  42. ) -> Result<(), Box<dyn Error>> {
  43. let fh = File::create(filename)?;
  44. let mut writer = EmitterConfig::new().perform_indent(true).create_writer(fh);
  45. let mut event: WrXmlEvent = WrXmlEvent::start_element("ksudoku").into();
  46. writer.write(event)?;
  47. event = WrXmlEvent::start_element("game")
  48. .attr("had-help", "0")
  49. .attr("msecs-elapsed", "0")
  50. .into();
  51. writer.write(event)?;
  52. event = WrXmlEvent::start_element("puzzle").into();
  53. writer.write(event)?;
  54. event = WrXmlEvent::start_element("graph").attr("specific-type", "Plain")
  55. .attr("type", "sudoku").attr("order", "9").into();
  56. writer.write(event)?;
  57. event = WrXmlEvent::end_element().into();
  58. writer.write(event)?;
  59. // values (puzzle)
  60. event = WrXmlEvent::start_element("values").into();
  61. writer.write(event)?;
  62. event = WrXmlEvent::characters(puz).into();
  63. writer.write(event)?;
  64. event = WrXmlEvent::end_element().into();
  65. writer.write(event)?;
  66. // solution
  67. event = WrXmlEvent::start_element("solution").into();
  68. writer.write(event)?;
  69. event = WrXmlEvent::characters(sol).into();
  70. writer.write(event)?;
  71. event = WrXmlEvent::end_element().into();
  72. writer.write(event)?;
  73. event = WrXmlEvent::end_element().into();
  74. writer.write(event)?;
  75. event = WrXmlEvent::end_element().into();
  76. writer.write(event)?;
  77. event = WrXmlEvent::end_element().into();
  78. writer.write(event)?;
  79. /*
  80. let pStr: String = puzzle.save_to_tld(start_ch: 'b', blank: '_');
  81. let solStr: solution.save_to_tld(start_ch: 'b', blank: '_');
  82. let ksudoko = Ksudoku {
  83. game: Game {
  84. help: 0,
  85. elapsed: 0,
  86. puzzle: Puzzle {
  87. graph: Graph {
  88. order: 9,
  89. game_type: String::from("sudoku"),
  90. specific_type: String::from("Plain"),
  91. },
  92. values: puz.to_string(),
  93. solution: sol.to_string(),
  94. },
  95. },
  96. };
  97. let fh = File::create(filename)?;
  98. let _res = to_writer(fh, &ksudoko)?;
  99. */
  100. Ok(())
  101. }