Ver Fonte

Updated main.rs so it uses TileIndex.save

It should handle a File Not Found or Not Exists error, and anything else should be thrown as an error
david há 1 mês atrás
pai
commit
20b9714ea2
1 ficheiros alterados com 40 adições e 34 exclusões
  1. 40 34
      src/main.rs

+ 40 - 34
src/main.rs

@@ -6,47 +6,53 @@ mod server;
 use server::AppServer;
 */
 
-use std::{fs, path::PathBuf};
+use std::path::PathBuf;
 
 use rmmo::*;
 
 fn main() -> Result<(), anyhow::Error> {
     println!("Hello, World!");
-    if let Ok(index) = TileIndex::load(PathBuf::from("tiles.json")) {
-        println!("Located {} tiles", index.len());
-        let mut idx: usize = 0;
-        for t in index {
-            if let Some(desc) = &t.description {
-                println!("{}] \"{}\" = '{}' {} \"{}\"", idx, t.name, t.symbol, t.color, desc);
-            } else {
-                println!("{}] \"{}\" = '{}' {}", idx, t.name, t.symbol, t.color);
+    let r = TileIndex::load(PathBuf::from("tiles.json"));
+    match r {
+        Ok(index) => {
+            println!("Located {} tiles", index.len());
+            let mut idx: usize = 0;
+            for t in index {
+                if let Some(desc) = &t.description {
+                    println!("{:3}] \"{:25}\" = '{:1}' {:25} \"{}\"", idx, t.name, t.symbol, t.color, desc);
+                } else {
+                    println!("{:3}] \"{:25}\" = '{:1}' {:25}", idx, t.name, t.symbol, t.color);
+                }
+                idx += 1;
             }
-            idx += 1;
         }
-    } else {
-        let mut index = TileIndex::new();
-        // index.define(Tile::with_description("", "", "", ""));
-        index.define(Tile::with_description("void", "", "", "You can fly over it, or fall into it"));
-        index.define(Tile::with_description("player", "@", "bright green", "You"));
-        index.define(Tile::with_description("player-neutral", "@", "bright white", "Somebody else"));
-        index.define(Tile::with_description("player-enemy", "@", "bright red", "Somebody else, that's mean"));
-        index.define(Tile::with_description("player-ally", "@", "green", "Somebody else, that's nice"));
-        index.define(Tile::with_description("ground-stone", ".", "white", "A stone path"));
-        index.define(Tile::with_description("ground-grass", ".", "green", "A grassy path"));
-        index.define(Tile::with_description("wall-v-stone", "|", "white", "A stone wall"));
-        index.define(Tile::with_description("wall-h-stone", "-", "white", "A stone wall"));
-        index.define(Tile::with_description("door-c-wood", "+", "brown", "A closed door"));
-        index.define(Tile::with_description("door-o-wood", "-", "brown", "A open door, you can move thru it"));
-        index.define(Tile::with_description("water-shallow", "~", "cyan", "Shallow water, you can wade thru it"));
-        index.define(Tile::with_description("water-deep", "~", "bright blue", "Deep water, you can swim thru it"));
-        index.define(Tile::with_description("gold", "$", "bright yellow", "Gold, a common currency"));
-        index.define(Tile::with_description("gem-blue", "*", "bright cyan", "Sapphire, a rare currency"));
-        index.define(Tile::with_description("gem-red", "*", "red", "Ruby, a rare currency"));
-        index.define(Tile::with_description("gem-green", "*", "bright green", "Emerald, a rare currency"));
-        index.define(Tile::with_description("gem-magenta", "*", "bright magenta", "Amethyst, a rare currency"));
-        let content = serde_json::to_string_pretty(&index)?;
-        fs::write(PathBuf::from("tiles.json"), content)?;
-        println!("Created {} tiles", index.len());
+        Err(err) => {
+            if err.to_string() != "The system cannot find the file specified. (os error 2)".to_string() {
+                anyhow::bail!(err);
+            }
+            let mut index = TileIndex::new();
+            // index.define(Tile::with_description("", "", "", ""));
+            index.define(Tile::with_description("void", "", "", "You can fly over it, or fall into it"));
+            index.define(Tile::with_description("player", "@", "bright green", "You"));
+            index.define(Tile::with_description("player-neutral", "@", "bright white", "Somebody else"));
+            index.define(Tile::with_description("player-enemy", "@", "bright red", "Somebody else, that's mean"));
+            index.define(Tile::with_description("player-ally", "@", "green", "Somebody else, that's nice"));
+            index.define(Tile::with_description("ground-stone", ".", "white", "A stone path"));
+            index.define(Tile::with_description("ground-grass", ".", "green", "A grassy path"));
+            index.define(Tile::with_description("wall-v-stone", "|", "white", "A stone wall"));
+            index.define(Tile::with_description("wall-h-stone", "-", "white", "A stone wall"));
+            index.define(Tile::with_description("door-c-wood", "+", "brown", "A closed door"));
+            index.define(Tile::with_description("door-o-wood", "-", "brown", "A open door, you can move thru it"));
+            index.define(Tile::with_description("water-shallow", "~", "cyan", "Shallow water, you can wade thru it"));
+            index.define(Tile::with_description("water-deep", "~", "bright blue", "Deep water, you can swim thru it"));
+            index.define(Tile::with_description("gold", "$", "bright yellow", "Gold, a common currency"));
+            index.define(Tile::with_description("gem-blue", "*", "bright cyan", "Sapphire, a rare currency"));
+            index.define(Tile::with_description("gem-red", "*", "red", "Ruby, a rare currency"));
+            index.define(Tile::with_description("gem-green", "*", "bright green", "Emerald, a rare currency"));
+            index.define(Tile::with_description("gem-magenta", "*", "bright magenta", "Amethyst, a rare currency"));
+            index.save(PathBuf::from("tiles.json"))?;
+            println!("Created {} tiles", index.len());
+        }
     }
     Ok(())
 }